fix(utils): parse Markdown headings with CRLF line break (#7043)

This commit is contained in:
Joshua Chen 2022-03-28 21:59:29 +08:00 committed by GitHub
parent 5fb09a2946
commit 4f4f503633
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View file

@ -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`

View file

@ -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,
);