diff --git a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts index 34648991ba35..646604f1a1fb 100644 --- a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts @@ -438,6 +438,113 @@ Lorem Ipsum }); }); + it('preserves trailing # in title without preceding space (e.g. C# or F#)', () => { + expect( + parseMarkdownContentTitle(dedent` + # Learn C# + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # Learn C# + + Lorem Ipsum + `, + contentTitle: 'Learn C#', + }); + + expect( + parseMarkdownContentTitle(dedent` + # F# + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # F# + + Lorem Ipsum + `, + contentTitle: 'F#', + }); + + expect( + parseMarkdownContentTitle(dedent` + # Learn C# {#learn-csharp} + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # Learn C# {#learn-csharp} + + Lorem Ipsum + `, + contentTitle: 'Learn C#', + }); + + expect( + parseMarkdownContentTitle(dedent` + # Learn C# # + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # Learn C# # + + Lorem Ipsum + `, + contentTitle: 'Learn C#', + }); + + expect( + parseMarkdownContentTitle(dedent` + # Learn C# ## {#learn-csharp} + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # Learn C# ## {#learn-csharp} + + Lorem Ipsum + `, + contentTitle: 'Learn C# ##', + }); + + expect( + parseMarkdownContentTitle(dedent` + # # + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # # + + Lorem Ipsum + `, + contentTitle: '', + }); + + expect( + parseMarkdownContentTitle(dedent` + # ## + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # ## + + Lorem Ipsum + `, + contentTitle: '', + }); + }); + it('parses markdown h1 title at the top followed by h2 title', () => { const markdown = dedent` diff --git a/packages/docusaurus-utils/src/markdownUtils.ts b/packages/docusaurus-utils/src/markdownUtils.ts index 76f824f33ebd..be67c4e305e6 100644 --- a/packages/docusaurus-utils/src/markdownUtils.ts +++ b/packages/docusaurus-utils/src/markdownUtils.ts @@ -334,7 +334,7 @@ export function parseMarkdownContentTitle( contentTitle: toTextContentTitle( regularTitleMatch .groups!.title!.trim() - .replace(/\s*(?:\{#*[\w-]+\}|#+)$/, ''), + .replace(/(?:\s*\{#*[\w-]+\}|(?:^|\s+)#+)$/, ''), ).trim(), }; }