diff --git a/src/translator.js b/src/translator.js index 85557d2..615781a 100644 --- a/src/translator.js +++ b/src/translator.js @@ -44,15 +44,41 @@ function initTurndownService() { } }); - // preserve iframes (common for embedded audio/video) + // iframe boolean attributes do not need to be set to empty string turndownService.addRule('iframe', { filter: 'iframe', replacement: (content, node) => { - const html = node.outerHTML.replace('allowfullscreen=""', 'allowfullscreen'); + const html = node.outerHTML + .replace('allowfullscreen=""', 'allowfullscreen') + .replace('allowpaymentrequest=""', 'allowpaymentrequest'); return '\n\n' + html + '\n\n'; } }); + // preserve
when it contains a
+ turndownService.addRule('figure', { + filter: 'figure', + replacement: (content, node) => { + 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'; + } + }); + return turndownService; } @@ -70,11 +96,6 @@ function getPostContent(post, turndownService, config) { content = content.replace(/(]*src=").*?([^/"]+\.(?:gif|jpe?g|png))("[^>]*>)/gi, '$1images/$2$3'); } - // this is a hack to make