mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-30 17:37:09 +02:00
fix(v2): ignore imports when h1 heading parsing (#4646)
This commit is contained in:
parent
5088a1e48f
commit
4835222c16
2 changed files with 37 additions and 2 deletions
|
@ -183,6 +183,41 @@ describe('parseMarkdownContentTitle', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('Should parse markdown h1 title placed after import declarations', () => {
|
||||||
|
const markdown = dedent`
|
||||||
|
import Component from '@site/src/components/Component';
|
||||||
|
import Component from '@site/src/components/Component'
|
||||||
|
import './styles.css';
|
||||||
|
|
||||||
|
# Markdown Title
|
||||||
|
|
||||||
|
Lorem Ipsum
|
||||||
|
|
||||||
|
`;
|
||||||
|
expect(parseMarkdownContentTitle(markdown)).toEqual({
|
||||||
|
content: `import Component from '@site/src/components/Component';\nimport Component from '@site/src/components/Component'\nimport './styles.css';\n\n\n\nLorem Ipsum`,
|
||||||
|
contentTitle: 'Markdown Title',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Should parse markdown h1 alternate title placed after import declarations', () => {
|
||||||
|
const markdown = dedent`
|
||||||
|
import Component from '@site/src/components/Component';
|
||||||
|
import Component from '@site/src/components/Component'
|
||||||
|
import './styles.css';
|
||||||
|
|
||||||
|
Markdown Title
|
||||||
|
==============
|
||||||
|
|
||||||
|
Lorem Ipsum
|
||||||
|
|
||||||
|
`;
|
||||||
|
expect(parseMarkdownContentTitle(markdown)).toEqual({
|
||||||
|
content: `import Component from '@site/src/components/Component';\nimport Component from '@site/src/components/Component'\nimport './styles.css';\n\nLorem Ipsum`,
|
||||||
|
contentTitle: 'Markdown Title',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('Should parse title-only', () => {
|
test('Should parse title-only', () => {
|
||||||
const markdown = '# Document With Only A Title ';
|
const markdown = '# Document With Only A Title ';
|
||||||
expect(parseMarkdownContentTitle(markdown)).toEqual({
|
expect(parseMarkdownContentTitle(markdown)).toEqual({
|
||||||
|
|
|
@ -86,10 +86,10 @@ export function parseMarkdownContentTitle(
|
||||||
|
|
||||||
const content = contentUntrimmed.trim();
|
const content = contentUntrimmed.trim();
|
||||||
|
|
||||||
const regularTitleMatch = /^(?<pattern>#\s*(?<title>[^#\n]*)+[ \t]*#?\n*?)/g.exec(
|
const regularTitleMatch = /^(?:import\s.*(from.*)?;?|\n)*?(?<pattern>#\s*(?<title>[^#\n]*)+[ \t]*#?\n*?)/g.exec(
|
||||||
content,
|
content,
|
||||||
);
|
);
|
||||||
const alternateTitleMatch = /^(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/g.exec(
|
const alternateTitleMatch = /^(?:import\s.*(from.*)?;?|\n)*?(?<pattern>\s*(?<title>[^\n]*)\s*\n[=]+)/g.exec(
|
||||||
content,
|
content,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue