mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-16 00:06:11 +02:00
fix(v2): markdown title parser should ignore all forms of MDX import statements (#4735)
Co-authored-by: Nam Hoang Le <nam.hoang.le@mgm-tp.com>
This commit is contained in:
parent
8c39826496
commit
404b4dba8b
2 changed files with 61 additions and 6 deletions
|
@ -86,12 +86,20 @@ export function parseMarkdownContentTitle(
|
|||
|
||||
const content = contentUntrimmed.trim();
|
||||
|
||||
const regularTitleMatch = /^(?:import\s+\S+(\s+from\s+\S+)?;?|\n)*?(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>({#*[\w-]+})|#)?\n*?)/g.exec(
|
||||
content,
|
||||
);
|
||||
const alternateTitleMatch = /^(?:import\s+\S+(\s+from\s+\S+)?;?|\n)*?(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/g.exec(
|
||||
content,
|
||||
);
|
||||
const IMPORT_STATEMENT = /import\s+(([\w*{}\s\n,]+)from\s+)?["'\s]([@\w/_.-]+)["'\s];?|\n/
|
||||
.source;
|
||||
const REGULAR_TITLE = /(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>({#*[\w-]+})|#)?\n*?)/
|
||||
.source;
|
||||
const ALTERNATE_TITLE = /(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/.source;
|
||||
|
||||
const regularTitleMatch = new RegExp(
|
||||
`^(?:${IMPORT_STATEMENT})*?${REGULAR_TITLE}`,
|
||||
'g',
|
||||
).exec(content);
|
||||
const alternateTitleMatch = new RegExp(
|
||||
`^(?:${IMPORT_STATEMENT})*?${ALTERNATE_TITLE}`,
|
||||
'g',
|
||||
).exec(content);
|
||||
|
||||
const titleMatch = regularTitleMatch ?? alternateTitleMatch;
|
||||
const {pattern, title} = titleMatch?.groups ?? {};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue