Skip to content

Commit

Permalink
Fix: Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sandypockets committed Dec 16, 2023
1 parent 618aed5 commit 66fcf5d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 38 deletions.
2 changes: 1 addition & 1 deletion dist/index.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.esm.js

Large diffs are not rendered by default.

16 changes: 0 additions & 16 deletions examples/nuxt-tailwind/helpers/getAllPosts.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "epic-remark",
"version": "0.1.10",
"version": "0.1.11",
"description": "Epic Remark is an all-in-one markdown to HTML processor built on top of remark",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
Expand Down
36 changes: 17 additions & 19 deletions src/processMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@ export default async function processMarkdown(markdownContent, options = {}) {
};

const wrapConfig = { ...defaultWrapConfig, ...(options.wrapConfig || {}) };
let frontMatterData = null;
let frontMatterData = {};
let tableOfContents = null;
let readingTime = null;

const processor = remark()
.use(frontmatter, ['yaml'])
.use(() => tree => {
frontMatterData = extractFrontMatter(tree);
})
.use(html)
.use(gfm)
.use(remarkRehype, { allowDangerousHtml: true })
.use(wrapElements, wrapConfig);
.use(frontmatter, ['yaml'])
.use(() => tree => {
const yamlNode = tree.children.find(node => node.type === 'yaml');
if (yamlNode) {
frontMatterData = yaml.parse(yamlNode.value);
}
})
.use(html)
.use(gfm)
.use(remarkRehype, { allowDangerousHtml: true })
.use(wrapElements, wrapConfig);

if (options.addHeadingIds) {
processor.use(addHeadingIds);
Expand All @@ -47,10 +50,10 @@ export default async function processMarkdown(markdownContent, options = {}) {
const tocNode = addTableOfContents(tree, options.insertTocDirectly);
if (tocNode) {
tableOfContents = await remark()
.use(() => () => tocNode)
.use(rehypeStringify)
.process('')
.then(file => file.toString());
.use(() => () => tocNode)
.use(rehypeStringify)
.process('')
.then(file => file.toString());
}
});
}
Expand All @@ -63,7 +66,7 @@ export default async function processMarkdown(markdownContent, options = {}) {
processor.use(embed).use(rehypeRaw);
}

let processedContent = await processor.use(rehypeStringify).process(markdownContent);
const processedContent = await processor.use(rehypeStringify).process(markdownContent);
const wrappedContentHtml = wrapWithDiv(processedContent.toString(), 'markdown');

return {
Expand All @@ -73,8 +76,3 @@ export default async function processMarkdown(markdownContent, options = {}) {
readingTime: readingTime,
};
}

function extractFrontMatter(tree) {
const frontMatterNode = tree.children.find(node => node.type === 'yaml');
return frontMatterNode ? yaml.parse(frontMatterNode.value) : {};
}

0 comments on commit 66fcf5d

Please sign in to comment.