Skip to content

Commit

Permalink
refactor(mdx-loader): remove useless usage of mdx loader this.query (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber authored Aug 19, 2024
1 parent 6652ed1 commit bb49992
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
29 changes: 12 additions & 17 deletions packages/docusaurus-mdx-loader/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
});

Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand All @@ -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?
Expand Down
24 changes: 10 additions & 14 deletions packages/docusaurus-mdx-loader/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,52 +229,48 @@ 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;
}

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;
Expand Down

0 comments on commit bb49992

Please sign in to comment.