mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
fix(mdx-loader): get correct error line numbers, handle front matter + contentTitle with remark (#9386)
This commit is contained in:
parent
35441b38e4
commit
d86aa0da5f
7 changed files with 617 additions and 24 deletions
|
@ -9,7 +9,6 @@ import fs from 'fs-extra';
|
|||
import logger from '@docusaurus/logger';
|
||||
import {
|
||||
parseFrontMatter,
|
||||
parseMarkdownContentTitle,
|
||||
escapePath,
|
||||
getFileLoaderUtils,
|
||||
} from '@docusaurus/utils';
|
||||
|
@ -122,6 +121,15 @@ function ensureMarkdownConfig(reqOptions: Options) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* data.contentTitle is set by the remark contentTitle plugin
|
||||
*/
|
||||
function extractContentTitleData(data: {
|
||||
[key: string]: unknown;
|
||||
}): string | undefined {
|
||||
return data.contentTitle as string | undefined;
|
||||
}
|
||||
|
||||
export async function mdxLoader(
|
||||
this: LoaderContext<Options>,
|
||||
fileString: string,
|
||||
|
@ -132,18 +140,11 @@ export async function mdxLoader(
|
|||
const {query} = this;
|
||||
ensureMarkdownConfig(reqOptions);
|
||||
|
||||
const {frontMatter, content: contentWithTitle} = parseFrontMatter(fileString);
|
||||
const {frontMatter} = parseFrontMatter(fileString);
|
||||
const mdxFrontMatter = validateMDXFrontMatter(frontMatter.mdx);
|
||||
|
||||
const {content: contentUnprocessed, contentTitle} = parseMarkdownContentTitle(
|
||||
contentWithTitle,
|
||||
{
|
||||
removeContentTitle: reqOptions.removeContentTitle,
|
||||
},
|
||||
);
|
||||
|
||||
const content = preprocessor({
|
||||
fileContent: contentUnprocessed,
|
||||
const preprocessedContent = preprocessor({
|
||||
fileContent: fileString,
|
||||
filePath,
|
||||
admonitions: reqOptions.admonitions,
|
||||
markdownConfig: reqOptions.markdownConfig,
|
||||
|
@ -158,9 +159,13 @@ export async function mdxLoader(
|
|||
mdxFrontMatter,
|
||||
});
|
||||
|
||||
let result: string;
|
||||
let result: {content: string; data: {[key: string]: unknown}};
|
||||
try {
|
||||
result = await processor.process({content, filePath});
|
||||
result = await processor.process({
|
||||
content: preprocessedContent,
|
||||
filePath,
|
||||
frontMatter,
|
||||
});
|
||||
} catch (errorUnknown) {
|
||||
const error = errorUnknown as Error;
|
||||
|
||||
|
@ -184,6 +189,8 @@ export async function mdxLoader(
|
|||
);
|
||||
}
|
||||
|
||||
const contentTitle = extractContentTitleData(result.data);
|
||||
|
||||
// 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);
|
||||
|
@ -244,7 +251,7 @@ ${assets ? `export const assets = ${createAssetsExportCode(assets)};` : ''}
|
|||
|
||||
const code = `
|
||||
${exportsCode}
|
||||
${result}
|
||||
${result.content}
|
||||
`;
|
||||
|
||||
return callback(null, code);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue