From afd73417b628847949db0149bce4b28e43db8dae Mon Sep 17 00:00:00 2001 From: Will Boyd Date: Wed, 14 Nov 2018 17:53:15 -0500 Subject: [PATCH] Better detection and preservation of codepen embeds --- index.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) 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