From c1f592b0173785ff7ae85c8ed36a7303a44eb5cd Mon Sep 17 00:00:00 2001 From: sebastienlorber Date: Mon, 9 Oct 2023 13:01:25 +0200 Subject: [PATCH] use vfile to expose parsed frontMatter to all Remark plugins --- packages/docusaurus-mdx-loader/package.json | 1 + packages/docusaurus-mdx-loader/src/loader.ts | 6 ++++- .../docusaurus-mdx-loader/src/processor.ts | 26 ++++++++++++------- yarn.lock | 2 +- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/docusaurus-mdx-loader/package.json b/packages/docusaurus-mdx-loader/package.json index 37981d277da8..3d77b5f63546 100644 --- a/packages/docusaurus-mdx-loader/package.json +++ b/packages/docusaurus-mdx-loader/package.json @@ -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": { diff --git a/packages/docusaurus-mdx-loader/src/loader.ts b/packages/docusaurus-mdx-loader/src/loader.ts index 797d17d974a6..519b5da3b9cd 100644 --- a/packages/docusaurus-mdx-loader/src/loader.ts +++ b/packages/docusaurus-mdx-loader/src/loader.ts @@ -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; diff --git a/packages/docusaurus-mdx-loader/src/processor.ts b/packages/docusaurus-mdx-loader/src/processor.ts index 5b22ef235a01..b7d500c64a0d 100644 --- a/packages/docusaurus-mdx-loader/src/processor.ts +++ b/packages/docusaurus-mdx-loader/src/processor.ts @@ -36,9 +36,11 @@ type SimpleProcessor = { process: ({ content, filePath, + frontMatter, }: { content: string; filePath: string; + frontMatter: {[key: string]: unknown}; }) => Promise; }; @@ -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! @@ -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, + })); + }, }; } diff --git a/yarn.lock b/yarn.lock index 4183a484f472..000b0a8adfc0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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==