mirror of
https://github.com/10h30/wordpress-export-to-markdown.git
synced 2026-06-05 15:09:59 +09:00
Merge pull request #108 from lonekorean/pre-code-blocks
<pre> code blocks
This commit is contained in:
@@ -79,6 +79,18 @@ function initTurndownService() {
|
||||
}
|
||||
});
|
||||
|
||||
// convert <pre> into a code block with language when appropriate
|
||||
turndownService.addRule('pre', {
|
||||
filter: node => {
|
||||
// a <pre> with <code> inside will already render nicely, so don't interfere
|
||||
return node.nodeName === 'PRE' && !node.querySelector('code');
|
||||
},
|
||||
replacement: (content, node) => {
|
||||
const language = node.getAttribute('data-wetm-language') || '';
|
||||
return '\n\n```' + language + '\n' + node.textContent + '\n```\n\n';
|
||||
}
|
||||
});
|
||||
|
||||
return turndownService;
|
||||
}
|
||||
|
||||
@@ -100,6 +112,10 @@ function getPostContent(post, turndownService, config) {
|
||||
// by escaping angle brackets (will be unescaped during turndown conversion)
|
||||
content = content.replace(/<(!--more( .*)?--)>/, '<$1>');
|
||||
|
||||
// some WordPress plugins specify a code language in an HTML comment above a
|
||||
// <pre> block, save it to a data attribute so the "pre" rule can use it
|
||||
content = content.replace(/(<!-- wp:.+? \{"language":"(.+?)"\} -->\r?\n<pre )/g, '$1data-wetm-language="$2" ');
|
||||
|
||||
// use turndown to convert HTML to Markdown
|
||||
content = turndownService.turndown(content);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user