Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,26 @@ Lorem Ipsum
});
});

it('parses markdown h1 title placed after export declarations', () => {
const markdown = dedent`
export function Author() {
return (
<a href="mailto:author@example.com">Author</a>
);
}

# Markdown Title

Lorem Ipsum

`;

expect(parseMarkdownContentTitle(markdown)).toEqual({
content: markdown,
contentTitle: 'Markdown Title',
});
});

it('parses markdown h1 title placed after multiple import declarations', () => {
const markdown = dedent`
import Component1 from '@site/src/components/Component1';
Expand Down
8 changes: 4 additions & 4 deletions packages/docusaurus-utils/src/markdownUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,11 @@ export function parseMarkdownContentTitle(
const removeContentTitleOption = options?.removeContentTitle ?? false;

const content = contentUntrimmed.trim();
// We only need to detect import statements that will be parsed by MDX as
// `import` nodes, as broken syntax can't render anyways. That means any block
// that has `import` at the very beginning and surrounded by empty lines.
// We only need to detect import/export statements that will be parsed by MDX as
// `import` or `export` nodes, as broken syntax can't render anyways. That means any block
// that has `import` or `export` at the very beginning and surrounded by empty lines.
const contentWithoutImport = content
.replace(/^(?:import\s(?:.|\r?\n(?!\r?\n))*(?:\r?\n){2,})*/, '')
.replace(/^(?:(?:import|export)\s(?:.|\r?\n(?!\r?\n))*(?:\r?\n){2,})*/, '')
.trim();

const regularTitleMatch = /^#[ \t]+(?<title>[^ \t].*)(?:\r?\n|$)/.exec(
Expand Down