Skip to content

Commit

Permalink
use vfile to expose parsed frontMatter to all Remark plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Oct 9, 2023
1 parent f727350 commit c1f592b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/docusaurus-mdx-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"unified": "^10.1.2",
"unist-util-visit": "^2.0.3",
"url-loader": "^4.1.1",
"vfile": "^5.3.7",
"webpack": "^5.88.1"
},
"devDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion packages/docusaurus-mdx-loader/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,11 @@ export async function mdxLoader(

let result: {content: string; data: {[key: string]: unknown}};
try {
result = await processor.process({content: preprocessedContent, filePath});
result = await processor.process({
content: preprocessedContent,
filePath,
frontMatter,
});
} catch (errorUnknown) {
const error = errorUnknown as Error;

Expand Down
26 changes: 16 additions & 10 deletions packages/docusaurus-mdx-loader/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ type SimpleProcessor = {
process: ({
content,
filePath,
frontMatter,
}: {
content: string;
filePath: string;
frontMatter: {[key: string]: unknown};
}) => Promise<SimpleProcessorResult>;
};

Expand Down Expand Up @@ -83,6 +85,7 @@ async function createProcessorFactory() {
// TODO using fork until PR merged: https://github.com/leebyron/remark-comment/pull/3
const {default: comment} = await import('@slorber/remark-comment');
const {default: directive} = await import('remark-directive');
const {VFile} = await import('vfile');

// /!\ this method is synchronous on purpose
// Using async code here can create cache entry race conditions!
Expand Down Expand Up @@ -164,16 +167,19 @@ async function createProcessorFactory() {
});

return {
process: async ({content, filePath}) =>
mdxProcessor
.process({
value: content,
path: filePath,
})
.then((vfile) => ({
content: vfile.toString(),
data: vfile.data,
})),
process: async ({content, filePath, frontMatter}) => {
const vfile = new VFile({
value: content,
path: filePath,
data: {
frontMatter,
},
});
return mdxProcessor.process(vfile).then((result) => ({
content: result.toString(),
data: result.data,
}));
},
};
}

Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17237,7 +17237,7 @@ vfile@^4.0.0:
unist-util-stringify-position "^2.0.0"
vfile-message "^2.0.0"

vfile@^5.0.0:
vfile@^5.0.0, vfile@^5.3.7:
version "5.3.7"
resolved "https://registry.yarnpkg.com/vfile/-/vfile-5.3.7.tgz#de0677e6683e3380fafc46544cfe603118826ab7"
integrity sha512-r7qlzkgErKjobAmyNIkkSpizsFPYiUPuJb5pNW1RB4JcYVZhs4lIbVqk8XPk033CV/1z8ss5pkax8SuhGpcG8g==
Expand Down

0 comments on commit c1f592b

Please # to comment.