Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 38 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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()));
Expand Down