mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-03 20:27:20 +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
|
@ -278,6 +278,53 @@ describe('parseMarkdownContentTitle', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Should parse markdown h1 title placed after various import declarations', () => {
|
||||||
|
const markdown = `
|
||||||
|
import DefaultComponent from '@site/src/components/Component1';
|
||||||
|
import DefaultComponent2 from '../relative/path/Component2';
|
||||||
|
import * as EntireComponent from './relative/path/Component3';
|
||||||
|
|
||||||
|
import { Component4 } from "double-quote-module-name";
|
||||||
|
import { Component51, Component52, \n Component53, \n\t\t Component54 } from "double-quote-module-name";
|
||||||
|
import { Component6 as AliasComponent6 } from "module-name";
|
||||||
|
import DefaultComponent8, { DefaultComponent81 ,\nDefaultComponent82 } from "module-name";
|
||||||
|
import DefaultComponent9, * as EntireComponent9 from "module-name";
|
||||||
|
import {Component71,\nComponent72 as AliasComponent72,\nComponent73\n} \nfrom "module-name";
|
||||||
|
|
||||||
|
import './styles.css';
|
||||||
|
import _ from 'underscore';
|
||||||
|
import "module-name"
|
||||||
|
|
||||||
|
# Markdown Title
|
||||||
|
|
||||||
|
Lorem Ipsum
|
||||||
|
`;
|
||||||
|
|
||||||
|
expect(parseMarkdownContentTitle(markdown)).toEqual({
|
||||||
|
content: `
|
||||||
|
import DefaultComponent from '@site/src/components/Component1';
|
||||||
|
import DefaultComponent2 from '../relative/path/Component2';
|
||||||
|
import * as EntireComponent from './relative/path/Component3';
|
||||||
|
|
||||||
|
import { Component4 } from "double-quote-module-name";
|
||||||
|
import { Component51, Component52, \n Component53, \n\t\t Component54 } from "double-quote-module-name";
|
||||||
|
import { Component6 as AliasComponent6 } from "module-name";
|
||||||
|
import DefaultComponent8, { DefaultComponent81 ,\nDefaultComponent82 } from "module-name";
|
||||||
|
import DefaultComponent9, * as EntireComponent9 from "module-name";
|
||||||
|
import {Component71,\nComponent72 as AliasComponent72,\nComponent73\n} \nfrom "module-name";
|
||||||
|
|
||||||
|
import './styles.css';
|
||||||
|
import _ from 'underscore';
|
||||||
|
import "module-name"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Lorem Ipsum
|
||||||
|
`.trim(),
|
||||||
|
contentTitle: 'Markdown Title',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('Should parse markdown h1 alternate title placed after import declarations', () => {
|
test('Should parse markdown h1 alternate title placed after import declarations', () => {
|
||||||
const markdown = dedent`
|
const markdown = dedent`
|
||||||
import Component from '@site/src/components/Component';
|
import Component from '@site/src/components/Component';
|
||||||
|
|
|
@ -86,12 +86,20 @@ export function parseMarkdownContentTitle(
|
||||||
|
|
||||||
const content = contentUntrimmed.trim();
|
const content = contentUntrimmed.trim();
|
||||||
|
|
||||||
const regularTitleMatch = /^(?:import\s+\S+(\s+from\s+\S+)?;?|\n)*?(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>({#*[\w-]+})|#)?\n*?)/g.exec(
|
const IMPORT_STATEMENT = /import\s+(([\w*{}\s\n,]+)from\s+)?["'\s]([@\w/_.-]+)["'\s];?|\n/
|
||||||
content,
|
.source;
|
||||||
);
|
const REGULAR_TITLE = /(?<pattern>#\s*(?<title>[^#\n{]*)+[ \t]*(?<suffix>({#*[\w-]+})|#)?\n*?)/
|
||||||
const alternateTitleMatch = /^(?:import\s+\S+(\s+from\s+\S+)?;?|\n)*?(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/g.exec(
|
.source;
|
||||||
content,
|
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 titleMatch = regularTitleMatch ?? alternateTitleMatch;
|
||||||
const {pattern, title} = titleMatch?.groups ?? {};
|
const {pattern, title} = titleMatch?.groups ?? {};
|
||||||
|
|
Loading…
Add table
Reference in a new issue