Skip to content

Commit

Permalink
fix(utils): parse Markdown headings with CRLF line break
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Mar 28, 2022
1 parent 85a79fd commit e228a7b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 16 additions & 0 deletions packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,22 @@ Lorem Ipsum
});
});

it('parses markdown h1 title with CRLF break', () => {
const markdown = `# Markdown Title\r\n\r\nLorem Ipsum`;
expect(parseMarkdownContentTitle(markdown)).toEqual({
content: markdown,
contentTitle: 'Markdown Title',
});
});

it('parses markdown h1 setext title with CRLF break', () => {
const markdown = `Markdown Title\r\n=====\r\n\r\nLorem Ipsum`;
expect(parseMarkdownContentTitle(markdown)).toEqual({
content: markdown,
contentTitle: 'Markdown Title',
});
});

it('parses markdown h1 title at the top (atx style with closing #)', () => {
const markdown = dedent`
Expand Down
6 changes: 3 additions & 3 deletions packages/docusaurus-utils/src/markdownUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ export function parseMarkdownContentTitle(
// `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.
const contentWithoutImport = content
.replace(/^(?:import\s(?:.|\n(?!\n))*\n{2,})*/, '')
.replace(/^(?:import\s(?:.|\r?\n(?!\r?\n))*(?:\r?\n){2,})*/, '')
.trim();

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

Expand Down

0 comments on commit e228a7b

Please sign in to comment.