From 7206c51936f4496a2366ad83adda0ca41c5d9664 Mon Sep 17 00:00:00 2001 From: Anjali Mishra <246823347+anjalimishra-0007@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:56:23 +0530 Subject: [PATCH] fix(utils): correctly ignore tilde and longer fenced code blocks in writeMarkdownHeadingId Fixes #13061 Signed-off-by: Anjali Mishra <246823347+anjalimishra-0007@users.noreply.github.com> --- .../__tests__/markdownHeadingIdUtils.test.ts | 76 +++++++++++++++++++ .../src/markdownHeadingIdUtils.ts | 70 ++++++++++++++++- 2 files changed, 142 insertions(+), 4 deletions(-) diff --git a/packages/docusaurus-utils/src/__tests__/markdownHeadingIdUtils.test.ts b/packages/docusaurus-utils/src/__tests__/markdownHeadingIdUtils.test.ts index 5ddf2bda081d..721d464992fe 100644 --- a/packages/docusaurus-utils/src/__tests__/markdownHeadingIdUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/markdownHeadingIdUtils.test.ts @@ -549,6 +549,44 @@ describe('writeMarkdownHeadingId', () => { ### def {/* #def */} `); }); + + it('ignores headings inside tilde and longer fenced code blocks and does not reserve ids inside them', () => { + expect( + write(dedent` + # Fenced code reproduction + + ~~~md + ## Reusable API {#reusable-api} + ~~~ + + ## Reusable API + + \`\`\`\`markdown + \`\`\` + ## Nested API + \`\`\` + \`\`\`\` + + ## Nested API + `), + ).toEqual(dedent` + # Fenced code reproduction + + ~~~md + ## Reusable API {#reusable-api} + ~~~ + + ## Reusable API {#reusable-api} + + \`\`\`\`markdown + \`\`\` + ## Nested API + \`\`\` + \`\`\`\` + + ## Nested API {#nested-api} + `); + }); }); describe('mdx-comment syntax', () => { @@ -755,5 +793,43 @@ describe('writeMarkdownHeadingId', () => { ### def {#def} `); }); + + it('ignores headings inside tilde and longer fenced code blocks and does not reserve ids inside them', () => { + expect( + write(dedent` + # Fenced code reproduction + + ~~~md + ## Reusable API {#reusable-api} + ~~~ + + ## Reusable API + + \`\`\`\`markdown + \`\`\` + ## Nested API + \`\`\` + \`\`\`\` + + ## Nested API + `), + ).toEqual(dedent` + # Fenced code reproduction + + ~~~md + ## Reusable API {#reusable-api} + ~~~ + + ## Reusable API {/* #reusable-api */} + + \`\`\`\`markdown + \`\`\` + ## Nested API + \`\`\` + \`\`\`\` + + ## Nested API {/* #nested-api */} + `); + }); }); }); diff --git a/packages/docusaurus-utils/src/markdownHeadingIdUtils.ts b/packages/docusaurus-utils/src/markdownHeadingIdUtils.ts index 9d49abffcaba..b04cfb76aba1 100644 --- a/packages/docusaurus-utils/src/markdownHeadingIdUtils.ts +++ b/packages/docusaurus-utils/src/markdownHeadingIdUtils.ts @@ -118,6 +118,47 @@ function addHeadingId( return `${headingHashes}${headingContent} ${headingIdSuffix}`; } +function parseOpeningCodeFence( + line: string, +): {char: string; length: number} | null { + const trimmed = line.replace(/^ {0,3}/, ''); + const char = trimmed[0]; + if (char !== '`' && char !== '~') { + return null; + } + let length = 0; + while (length < trimmed.length && trimmed[length] === char) { + length += 1; + } + if (length < 3) { + return null; + } + const rest = trimmed.slice(length); + if (char === '`' && rest.includes('`')) { + return null; + } + return {char, length}; +} + +function isClosingCodeFence( + line: string, + openFence: {char: string; length: number}, +): boolean { + const trimmed = line.replace(/^ {0,3}/, ''); + if (!trimmed.startsWith(openFence.char)) { + return false; + } + let length = 0; + while (length < trimmed.length && trimmed[length] === openFence.char) { + length += 1; + } + if (length < openFence.length) { + return false; + } + const rest = trimmed.slice(length).trim(); + return rest === ''; +} + export type WriteHeadingIDOptions = SluggerOptions & { /** The target syntax to use for heading IDs. */ syntax?: HeadingIdSyntax; @@ -166,7 +207,20 @@ export function writeMarkdownHeadingId( // If we can't overwrite existing slugs, make sure other headings don't // generate colliding slugs by first marking these slugs as occupied if (!overwrite) { + let openFence: {char: string; length: number} | null = null; lines.forEach((line) => { + if (!openFence) { + const fence = parseOpeningCodeFence(line); + if (fence) { + openFence = fence; + return; + } + } else { + if (isClosingCodeFence(line, openFence)) { + openFence = null; + } + return; + } const parsedHeading = parseHeadingIdAnySyntax(line); if (parsedHeading.id) { slugger.slug(parsedHeading.id); @@ -174,15 +228,23 @@ export function writeMarkdownHeadingId( }); } - let inCode = false; + let openFence: {char: string; length: number} | null = null; return lines .map((line) => { - if (line.startsWith('```')) { - inCode = !inCode; + if (!openFence) { + const fence = parseOpeningCodeFence(line); + if (fence) { + openFence = fence; + return line; + } + } else { + if (isClosingCodeFence(line, openFence)) { + openFence = null; + } return line; } // Ignore h1 headings, as we don't create anchor links for those - if (inCode || !line.startsWith('##')) { + if (!line.startsWith('##')) { return line; } const parsedHeading = parseHeadingIdAnySyntax(line);