From 4fd8f4e6f759b5cfa25400b3a1451b49d9c5f627 Mon Sep 17 00:00:00 2001 From: sebastien <lorber.sebastien@gmail.com> Date: Mon, 19 Aug 2024 18:47:56 +0200 Subject: [PATCH] cleanup mdx loader this.query --- packages/docusaurus-mdx-loader/src/loader.ts | 29 ++++++++----------- .../docusaurus-mdx-loader/src/processor.ts | 24 +++++++-------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/packages/docusaurus-mdx-loader/src/loader.ts b/packages/docusaurus-mdx-loader/src/loader.ts index 7118c38e747c..e6e41fde0090 100644 --- a/packages/docusaurus-mdx-loader/src/loader.ts +++ b/packages/docusaurus-mdx-loader/src/loader.ts @@ -140,12 +140,11 @@ export async function mdxLoader( const compilerName = getWebpackLoaderCompilerName(this); const callback = this.async(); const filePath = this.resourcePath; - const reqOptions: Options = this.getOptions(); - const {query} = this; + const options: Options = this.getOptions(); - ensureMarkdownConfig(reqOptions); + ensureMarkdownConfig(options); - const {frontMatter} = await reqOptions.markdownConfig.parseFrontMatter({ + const {frontMatter} = await options.markdownConfig.parseFrontMatter({ filePath, fileContent, defaultParseFrontMatter: DEFAULT_PARSE_FRONT_MATTER, @@ -155,16 +154,15 @@ export async function mdxLoader( const preprocessedContent = preprocessor({ fileContent, filePath, - admonitions: reqOptions.admonitions, - markdownConfig: reqOptions.markdownConfig, + admonitions: options.admonitions, + markdownConfig: options.markdownConfig, }); const hasFrontMatter = Object.keys(frontMatter).length > 0; const processor = await createProcessorCached({ filePath, - reqOptions, - query, + options, mdxFrontMatter, }); @@ -203,14 +201,14 @@ export async function mdxLoader( // MDX partials are MDX files starting with _ or in a folder starting with _ // Partial are not expected to have associated metadata files or front matter - const isMDXPartial = reqOptions.isMDXPartial?.(filePath); + const isMDXPartial = options.isMDXPartial?.(filePath); if (isMDXPartial && hasFrontMatter) { const errorMessage = `Docusaurus MDX partial files should not contain front matter. Those partial files use the _ prefix as a convention by default, but this is configurable. File at ${filePath} contains front matter that will be ignored: ${JSON.stringify(frontMatter, null, 2)}`; - if (!reqOptions.isMDXPartialFrontMatterWarningDisabled) { + if (!options.isMDXPartialFrontMatterWarningDisabled) { const shouldError = process.env.NODE_ENV === 'test' || process.env.CI; if (shouldError) { return callback(new Error(errorMessage)); @@ -222,11 +220,8 @@ ${JSON.stringify(frontMatter, null, 2)}`; function getMetadataPath(): string | undefined { if (!isMDXPartial) { // Read metadata for this MDX and export it. - if ( - reqOptions.metadataPath && - typeof reqOptions.metadataPath === 'function' - ) { - return reqOptions.metadataPath(filePath); + if (options.metadataPath && typeof options.metadataPath === 'function') { + return options.metadataPath(filePath); } } return undefined; @@ -246,8 +241,8 @@ ${JSON.stringify(frontMatter, null, 2)}`; : undefined; const assets = - reqOptions.createAssets && metadata - ? reqOptions.createAssets({frontMatter, metadata}) + options.createAssets && metadata + ? options.createAssets({frontMatter, metadata}) : undefined; // TODO use remark plugins to insert extra exports instead of string concat? diff --git a/packages/docusaurus-mdx-loader/src/processor.ts b/packages/docusaurus-mdx-loader/src/processor.ts index 00f2a3035bd2..1a89d2f6eed7 100644 --- a/packages/docusaurus-mdx-loader/src/processor.ts +++ b/packages/docusaurus-mdx-loader/src/processor.ts @@ -229,31 +229,29 @@ type ProcessorsCacheEntry = { const ProcessorsCache = new Map<string | Options, ProcessorsCacheEntry>(); async function createProcessorsCacheEntry({ - query, - reqOptions, + options, }: { - query: string | Options; - reqOptions: Options; + options: Options; }): Promise<ProcessorsCacheEntry> { const {createProcessorSync} = await createProcessorFactory(); - const compilers = ProcessorsCache.get(query); + const compilers = ProcessorsCache.get(options); if (compilers) { return compilers; } const compilerCacheEntry: ProcessorsCacheEntry = { mdProcessor: createProcessorSync({ - options: reqOptions, + options, format: 'md', }), mdxProcessor: createProcessorSync({ - options: reqOptions, + options, format: 'mdx', }), }; - ProcessorsCache.set(query, compilerCacheEntry); + ProcessorsCache.set(options, compilerCacheEntry); return compilerCacheEntry; } @@ -261,20 +259,18 @@ async function createProcessorsCacheEntry({ export async function createProcessorCached({ filePath, mdxFrontMatter, - query, - reqOptions, + options, }: { filePath: string; mdxFrontMatter: MDXFrontMatter; - query: string | Options; - reqOptions: Options; + options: Options; }): Promise<SimpleProcessor> { - const compilers = await createProcessorsCacheEntry({query, reqOptions}); + const compilers = await createProcessorsCacheEntry({options}); const format = getFormat({ filePath, frontMatterFormat: mdxFrontMatter.format, - markdownConfigFormat: reqOptions.markdownConfig.format, + markdownConfigFormat: options.markdownConfig.format, }); return format === 'md' ? compilers.mdProcessor : compilers.mdxProcessor;