From 8048dbd70b7a574963e893e254eb33aba0bb5895 Mon Sep 17 00:00:00 2001 From: Swizec Teller Date: Tue, 18 May 2021 07:22:05 -0700 Subject: [PATCH 1/2] Use custom transformers first --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 2aec2b0e..651194eb 100644 --- a/src/index.js +++ b/src/index.js @@ -23,7 +23,7 @@ export default async ( { cache, markdownAST }, { customTransformers = [], services = {} } = {} ) => { - const transformers = [...defaultTransformers, ...customTransformers]; + const transformers = [...customTransformers, ...defaultTransformers]; const transformations = []; visit(markdownAST, 'paragraph', (paragraphNode) => { From e8efd0db81db6792ebb96b9486eebbe055996c5e Mon Sep 17 00:00:00 2001 From: Swizec Teller Date: Tue, 18 May 2021 07:43:26 -0700 Subject: [PATCH 2/2] Ensure deterministic builds when multiple transformers match --- src/index.js | 68 ++++++++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/src/index.js b/src/index.js index 651194eb..a167dc54 100644 --- a/src/index.js +++ b/src/index.js @@ -50,37 +50,43 @@ export default async ( return; } - transformers - .filter(({ shouldTransform }) => shouldTransform(urlString)) - .forEach(({ getHTML, name = '' }) => { - transformations.push(async () => { - try { - let html = await cache.get(urlString); - - if (!html) { - html = await getHTML(urlString, services[name] || {}); - await cache.set(urlString, html); - } - - // if nothing's returned from getHTML, then no modifications are needed - if (!html) return; - - // convert the HTML string into an AST - const htmlElement = htmlToHast(html); - - // set the paragraphNode.data with the necessary properties - paragraphNode.data = { - hName: htmlElement.tagName, - hProperties: htmlElement.properties, - hChildren: htmlElement.children, - }; - } catch (error) { - error.message = `The following error appeared while processing '${urlString}':\n\n${error.message}`; - - throw error; - } - }); - }); + const transformer = transformers.find(({ shouldTransform }) => + shouldTransform(urlString) + ); + + if (!transformer) { + return; + } + + const { getHTML, name = '' } = transformer; + + transformations.push(async () => { + try { + let html = await cache.get(urlString); + + if (!html) { + html = await getHTML(urlString, services[name] || {}); + await cache.set(urlString, html); + } + + // if nothing's returned from getHTML, then no modifications are needed + if (!html) return; + + // convert the HTML string into an AST + const htmlElement = htmlToHast(html); + + // set the paragraphNode.data with the necessary properties + paragraphNode.data = { + hName: htmlElement.tagName, + hProperties: htmlElement.properties, + hChildren: htmlElement.children, + }; + } catch (error) { + error.message = `The following error appeared while processing '${urlString}':\n\n${error.message}`; + + throw error; + } + }); }); await Promise.all(transformations.map((t) => t()));