diff --git a/src/translator.js b/src/translator.js index 7df6e69..615781a 100644 --- a/src/translator.js +++ b/src/translator.js @@ -55,14 +55,27 @@ function initTurndownService() { } }); - // preserve
and
-- the extra newlines between tags are - // necessary because of how markdown is being used inside HTML tags - turndownService.addRule('fig', { - filter: ['figure', 'figcaption'], + // preserve
when it contains a
+ turndownService.addRule('figure', { + filter: 'figure', replacement: (content, node) => { - const tagName = node.nodeName.toLowerCase(); - const result = '\n\n<' + tagName + '>\n\n' + content + '\n\n\n\n'; - return result.replace('\n\n\n\n', '\n\n'); // reduce quadruple newlines + if (node.querySelector('figcaption')) { + // extra newlines are necessary for markdown and HTML to render correctly together + const result = '\n\n
\n\n' + content + '\n\n
\n\n'; + return result.replace('\n\n\n\n', '\n\n'); // collapse quadruple newlines + } else { + // does not contain
, do not preserve + return content; + } + } + }); + + // preserve
+ turndownService.addRule('figcaption', { + filter: 'figcaption', + replacement: (content, node) => { + // extra newlines are necessary for markdown and HTML to render correctly together + return '\n\n
\n\n' + content + '\n\n
\n\n'; } });