diff --git a/index.js b/index.js index 6a52bf1..c0fdd7f 100644 --- a/index.js +++ b/index.js @@ -142,21 +142,34 @@ function initTurndownService() { codeBlockStyle: 'fenced' }); + // 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 gists, codepens, etc.) turndownService.addRule('script', { filter: 'script', replacement: (content, node) => { + let before = '\n\n'; + if (node.getAttribute('src').endsWith('codepen.io/assets/embed/ei.js')) { + // keep codepen