diff --git a/src/translator.js b/src/translator.js index ecf4b9c..7fa6348 100644 --- a/src/translator.js +++ b/src/translator.js @@ -44,15 +44,6 @@ function initTurndownService() { } }); - // preserve enlighter code blocks - turndownService.addRule('enlighter', { - filter: (node) => node.nodeName === 'PRE' && node.classList.contains('EnlighterJSRAW'), - replacement: (content, node) => { - const language = node.getAttribute('data-enlighter-language') ?? ''; - return '\n' + '```' + language + '\n' + content + '\n' + '```' + '\n'; - } - }); - // iframe boolean attributes do not need to be set to empty string turndownService.addRule('iframe', { filter: 'iframe', @@ -88,17 +79,15 @@ function initTurndownService() { } }); - turndownService.addRule("pre", { - filter: (node, options) => { - return ( - options.codeBlockStyle === "fenced" && - node.nodeName === "PRE" && - node.firstChild - ); + // convert
into a code block with language when appropriate
+ turndownService.addRule('pre', {
+ filter: node => {
+ // a with inside will already render nicely, so don't interfere
+ return node.nodeName === 'PRE' && !node.querySelector('code');
},
replacement: (content, node) => {
- const code = node.textContent;
- return "\n" + "```" + "\n" + code + "\n" + "```" + "\n";
+ const language = node.getAttribute('data-wetm-language') || '';
+ return '\n\n```' + language + '\n' + node.textContent + '\n```\n\n';
}
});
@@ -123,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
+ // block, save it to a data attribute so the "pre" rule can use it
+ content = content.replace(/(\r?\n