From fdbcc4f19705edbfb5ee9fa008e674bc65792af8 Mon Sep 17 00:00:00 2001 From: Ganesh1729-ui Date: Sun, 22 Nov 2026 06:49:58 +0530 Subject: [PATCH 1/4] fix(utils): preserve trailing hash in markdown content title without preceding whitespace --- .../src/__tests__/markdownUtils.test.ts | 47 +++++++++++++++++++ .../docusaurus-utils/src/markdownUtils.ts | 3 +- 2 files changed, 49 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..8473a0448c9b 100644 --- a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts @@ -438,6 +438,53 @@ Lorem Ipsum }); }); + it('parses markdown h1 title preserving trailing hash without preceding whitespace', () => { + expect( + parseMarkdownContentTitle(dedent` + # Learn C# + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # Learn C# + + Lorem Ipsum + `, + contentTitle: 'Learn C#', + }); + + expect( + parseMarkdownContentTitle(dedent` + # F# {#fsharp-intro} + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # F# {#fsharp-intro} + + Lorem Ipsum + `, + contentTitle: 'F#', + }); + + expect( + parseMarkdownContentTitle(dedent` + # Learn C# # + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # Learn C# # + + Lorem Ipsum + `, + contentTitle: 'Learn C#', + }); + }); + 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 3a82a52cbb6e..5d5580c4596d 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 1743f6ca2bb965345a52f8fc86784b0a4d70f1c0 Mon Sep 17 00:00:00 2001 From: Ganesh1729-ui Date: Sun, 22 Nov 2026 07:22:31 +0530 Subject: [PATCH 2/4] fix(utils): strip closing heading markers with space/tab and before custom id --- .../src/__tests__/markdownUtils.test.ts | 30 +++++++++++++++++++ .../docusaurus-utils/src/markdownUtils.ts | 3 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts index 8473a0448c9b..bc0b7bb2f6e4 100644 --- a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts @@ -483,6 +483,36 @@ Lorem Ipsum `, contentTitle: 'Learn C#', }); + + expect( + parseMarkdownContentTitle(dedent` + # Title {#custom-id} # + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # Title {#custom-id} # + + Lorem Ipsum + `, + contentTitle: 'Title', + }); + + expect( + parseMarkdownContentTitle(dedent` + # Learn C\u00A0# + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # Learn C\u00A0# + + Lorem Ipsum + `, + contentTitle: 'Learn C\u00A0#', + }); }); it('parses markdown h1 title at the top followed by h2 title', () => { diff --git a/packages/docusaurus-utils/src/markdownUtils.ts b/packages/docusaurus-utils/src/markdownUtils.ts index 5d5580c4596d..75d67b95f982 100644 --- a/packages/docusaurus-utils/src/markdownUtils.ts +++ b/packages/docusaurus-utils/src/markdownUtils.ts @@ -334,8 +334,9 @@ export function parseMarkdownContentTitle( contentTitle: toTextContentTitle( regularTitleMatch .groups!.title!.trim() + .replace(/(?:[ \t]+|^)#+$/, '') .replace(/\s*\{#*[\w-]+\}$/, '') - .replace(/(?:\s+|^)#+$/, ''), + .replace(/(?:[ \t]+|^)#+$/, ''), ).trim(), }; } From 65c42a56ffaa12fb6dbaf1036d31505a034413d5 Mon Sep 17 00:00:00 2001 From: Ganesh1729-ui Date: Sun, 22 Nov 2026 19:04:47 +0530 Subject: [PATCH 3/4] fix(utils): preserve non-breaking space before title hash and conditionalize post-id hash stripping --- .../src/__tests__/markdownUtils.test.ts | 30 +++++++++++++++++++ .../docusaurus-utils/src/markdownUtils.ts | 15 +++++----- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts index bc0b7bb2f6e4..50d8c9f8e1b0 100644 --- a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts @@ -513,6 +513,36 @@ Lorem Ipsum `, contentTitle: 'Learn C\u00A0#', }); + + expect( + parseMarkdownContentTitle(dedent` + # \u00A0# + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # \u00A0# + + Lorem Ipsum + `, + contentTitle: '\u00A0#', + }); + + expect( + parseMarkdownContentTitle(dedent` + # Title # # + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # Title # # + + Lorem Ipsum + `, + contentTitle: 'Title #', + }); }); it('parses markdown h1 title at the top followed by h2 title', () => { diff --git a/packages/docusaurus-utils/src/markdownUtils.ts b/packages/docusaurus-utils/src/markdownUtils.ts index 75d67b95f982..37881fee46cc 100644 --- a/packages/docusaurus-utils/src/markdownUtils.ts +++ b/packages/docusaurus-utils/src/markdownUtils.ts @@ -329,15 +329,16 @@ export function parseMarkdownContentTitle( ? content.replace(titleMatch[0]!, '') : content; if (regularTitleMatch) { + let title = regularTitleMatch + .groups!.title!.replace(/^[ \t]+|[ \t]+$/g, '') + .replace(/(?:[ \t]+|^)#+$/, ''); + const titleWithoutId = title.replace(/\s*\{#*[\w-]+\}$/, ''); + if (titleWithoutId !== title) { + title = titleWithoutId.replace(/(?:[ \t]+|^)#+$/, ''); + } return { content: newContent.trim(), - contentTitle: toTextContentTitle( - regularTitleMatch - .groups!.title!.trim() - .replace(/(?:[ \t]+|^)#+$/, '') - .replace(/\s*\{#*[\w-]+\}$/, '') - .replace(/(?:[ \t]+|^)#+$/, ''), - ).trim(), + contentTitle: toTextContentTitle(title).replace(/^[ \t]+|[ \t]+$/g, ''), }; } return { From 3f7ce812d255dfa0deed0d635709a442d4b43950 Mon Sep 17 00:00:00 2001 From: Ganesh1729-ui Date: Sun, 22 Nov 2026 23:05:37 +0530 Subject: [PATCH 4/4] fix(utils): preserve trailing hash before custom heading id --- .../src/__tests__/markdownUtils.test.ts | 15 +++++++++++++++ packages/docusaurus-utils/src/markdownUtils.ts | 5 +---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts index 50d8c9f8e1b0..8a77d2bc8cea 100644 --- a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts @@ -543,6 +543,21 @@ Lorem Ipsum `, contentTitle: 'Title #', }); + + expect( + parseMarkdownContentTitle(dedent` + # Part # {#id} + + Lorem Ipsum + `), + ).toEqual({ + content: dedent` + # Part # {#id} + + Lorem Ipsum + `, + contentTitle: 'Part #', + }); }); it('parses markdown h1 title at the top followed by h2 title', () => { diff --git a/packages/docusaurus-utils/src/markdownUtils.ts b/packages/docusaurus-utils/src/markdownUtils.ts index 37881fee46cc..ab6e55fdab13 100644 --- a/packages/docusaurus-utils/src/markdownUtils.ts +++ b/packages/docusaurus-utils/src/markdownUtils.ts @@ -332,10 +332,7 @@ export function parseMarkdownContentTitle( let title = regularTitleMatch .groups!.title!.replace(/^[ \t]+|[ \t]+$/g, '') .replace(/(?:[ \t]+|^)#+$/, ''); - const titleWithoutId = title.replace(/\s*\{#*[\w-]+\}$/, ''); - if (titleWithoutId !== title) { - title = titleWithoutId.replace(/(?:[ \t]+|^)#+$/, ''); - } + title = title.replace(/\s*\{#*[\w-]+\}$/, ''); return { content: newContent.trim(), contentTitle: toTextContentTitle(title).replace(/^[ \t]+|[ \t]+$/g, ''),