const turndown = require('turndown'); const turndownPluginGfm = require('turndown-plugin-gfm'); function initTurndownService() { const turndownService = new turndown({ headingStyle: 'atx', bulletListMarker: '-', codeBlockStyle: 'fenced' }); turndownService.use(turndownPluginGfm.tables); // preserve embedded tweets turndownService.addRule('tweet', { filter: node => node.nodeName === 'BLOCKQUOTE' && node.getAttribute('class') === 'twitter-tweet', replacement: (content, node) => '\n\n' + node.outerHTML }); // preserve embedded codepens turndownService.addRule('codepen', { filter: node => { // codepen embed snippets have changed over the years // but this series of checks should find the commonalities return ( ['P', 'DIV'].includes(node.nodeName) && node.attributes['data-slug-hash'] && node.getAttribute('class') === 'codepen' ); }, replacement: (content, node) => '\n\n' + node.outerHTML }); // preserve embedded scripts (for tweets, codepens, gists, etc.) turndownService.addRule('script', { filter: 'script', replacement: (content, node) => { let before = '\n\n'; if (node.previousSibling && node.previousSibling.nodeName !== '#text') { // keep twitter and codepen