From 83124522f7cf9240107574d4c9183c69454877bf Mon Sep 17 00:00:00 2001 From: Hrithik Saini <49943287+hrithiksaini99@users.noreply.github.com> Date: Wed, 4 Nov 2026 12:48:35 +0530 Subject: [PATCH 1/2] fix(utils): do not drop trailing hash in heading title unless preceded by space --- .../src/__tests__/markdownUtils.test.ts | 56 +++++++++++++++++++ .../docusaurus-utils/src/markdownUtils.ts | 3 +- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts index 34648991ba35..a96233b9f134 100644 --- a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts @@ -438,6 +438,62 @@ Lorem Ipsum }); }); + it('parses markdown h1 title ending with # when not preceded by space (e.g. C#)', () => { + const markdown = dedent` + + # Learn C# + + Lorem Ipsum + + `; + expect(parseMarkdownContentTitle(markdown)).toEqual({ + content: markdown, + contentTitle: 'Learn C#', + }); + }); + + it('parses markdown h1 title ending with # and trailing ATX closing marker', () => { + const markdown = dedent` + + # Learn C# # + + Lorem Ipsum + + `; + expect(parseMarkdownContentTitle(markdown)).toEqual({ + content: markdown, + contentTitle: 'Learn C#', + }); + }); + + it('parses markdown h1 title ending with # and custom anchor-id', () => { + const markdown = dedent` + + # Learn C# {#learn-c-sharp} + + Lorem Ipsum + + `; + expect(parseMarkdownContentTitle(markdown)).toEqual({ + content: markdown, + contentTitle: 'Learn C#', + }); + }); + + it('parses markdown h1 title ending with attached # (e.g. foo#)', () => { + const markdown = dedent` + + # foo# + + Lorem Ipsum + + `; + expect(parseMarkdownContentTitle(markdown)).toEqual({ + content: markdown, + contentTitle: 'foo#', + }); + }); + 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..ff85ad42543d 100644 --- a/packages/docusaurus-utils/src/markdownUtils.ts +++ b/packages/docusaurus-utils/src/markdownUtils.ts @@ -334,7 +334,8 @@ export function parseMarkdownContentTitle( contentTitle: toTextContentTitle( regularTitleMatch .groups!.title!.trim() - .replace(/\s*(?:\{#*[\w-]+\}|#+)$/, ''), + .replace(/\s*\{#*[\w-]+\}$/, '') + .replace(/\s+#+$/, ''), ).trim(), }; } From 89d11401010944d7138facbd7066845826fb75d0 Mon Sep 17 00:00:00 2001 From: Hrithik Saini <49943287+hrithiksaini99@users.noreply.github.com> Date: Wed, 4 Nov 2026 13:14:30 +0530 Subject: [PATCH 2/2] fix(utils): handle all-hash heading titles when stripping closing markers --- .../src/__tests__/markdownUtils.test.ts | 14 ++++++++++++++ packages/docusaurus-utils/src/markdownUtils.ts | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts index a96233b9f134..11f5b2942745 100644 --- a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts @@ -494,6 +494,20 @@ Lorem Ipsum }); }); + it('parses empty markdown h1 title with closing # marker only', () => { + const markdown = dedent` + + # # + + Lorem Ipsum + + `; + expect(parseMarkdownContentTitle(markdown)).toEqual({ + content: markdown, + 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 ff85ad42543d..980e4d1aee58 100644 --- a/packages/docusaurus-utils/src/markdownUtils.ts +++ b/packages/docusaurus-utils/src/markdownUtils.ts @@ -335,7 +335,7 @@ export function parseMarkdownContentTitle( regularTitleMatch .groups!.title!.trim() .replace(/\s*\{#*[\w-]+\}$/, '') - .replace(/\s+#+$/, ''), + .replace(/(?:^|\s+)#+$/, ''), ).trim(), }; }