Skip to content

fix(utils): do not strip trailing # without preceding whitespace in markdown headings - #12835

Open
saiyam-jain-dev wants to merge 1 commit into
meta:mainfrom
saiyam-jain-dev:fix/preserve-trailing-hash-in-title
Open

saiyam-jain-dev wants to merge 1 commit into
meta:mainfrom
saiyam-jain-dev:fix/preserve-trailing-hash-in-title

Conversation

@saiyam-jain-dev

@saiyam-jain-dev saiyam-jain-dev commented Nov 3, 2026 •

Copy link
Copy Markdown

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • If this is a code change: I have written unit tests and/or added dogfooding pages to fully verify the new behavior.
  • If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

Fixes #12644.

When an H1 heading contains a trailing # as part of a word or programming language name (such as # Learn C# or # Getting Started with F#), parseMarkdownContentTitle stripped the trailing #, producing 'Learn C'.

In CommonMark ATX heading syntax (Section 4.2), an ATX heading's closing sequence of # characters must be preceded by at least one space or tab. The previous regex /\s*(?:\{#*[\w-]+\}|#+)$/ permitted 0 preceding spaces (\s*), unintentionally matching # directly attached to words.

By updating the regex to /(?:\s*\{#*[\w-]+\}|\s+#+)$/:

  • Headings with ATX closing # (e.g., # Title # or # Title ###) and custom IDs (# Title {#my-id}) are still trimmed as expected.
  • Words ending with # without preceding whitespace (e.g., C#, F#, Issue#) retain their #.

Note: In accordance with Docusaurus contributing guidelines, this PR was prepared with AI assistance.

Test Plan

  • Added unit tests in packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts:
    • does not strip trailing # without preceding whitespace from markdown h1 title (# Learn C# -> Learn C#)
    • preserves trailing # without preceding space when custom id is present (# Learn C# {#learn-csharp} -> Learn C#)
  • Verified that existing ATX closing # tests (# Markdown Title # -> Markdown Title) and custom id tests continue to pass.

Related issues/PRs

Fixes #12644

Summary by CodeRabbit

  • Bug Fixes
    • Markdown titles ending in C# now retain the hash, including when followed by a custom heading ID or closing hashes.
    • Parsing titles no longer alters the original Markdown content.

…arkdown headings

Fixes meta#12644. Prevents trimming trailing # in language names like C# and F# where the # is not preceded by whitespace as required by CommonMark ATX closing sequences.
@meta-cla meta-cla Bot added the CLA Signed Signed Facebook CLA label Nov 3, 2026
@coderabbitai

coderabbitai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: dde5f2fc-f1ba-44b4-88b1-8c0f1635ce19
Commits

Reviewing files that changed from the base of the PR and between 2a89055 and 76c344c.

Files selected for processing: 2
  • packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
  • packages/docusaurus-utils/src/markdownUtils.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

parseMarkdownContentTitle cleanup now handles trailing custom heading IDs and closing hashes. Tests verify that titles ending in C# retain the hash, with or without a custom heading ID, and that the Markdown content remains unchanged.

Changes: Markdown title parsing

Layer / File(s) Summary
Heading title cleanup and tests
packages/docusaurus-utils/src/markdownUtils.ts, packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
Title cleanup handles trailing custom heading IDs and closing hashes. Tests check that C# remains in the parsed title, with and without a custom ID, and that the source content is unchanged.

Estimated code review effort: 2 (Simple) | ~5 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

  • meta/docusaurus#12829: Changes closing-hash handling in parseMarkdownContentTitle and adds tests for headings such as # Learn C#.

Suggested labels: pr: bug fix

Merge Risk: Minimal · up to 76c34

No actionable merge-blocking risk is established for the heading-title parsing change. It appears mergeable subject to normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main fix: preserving trailing hash characters when they are not preceded by whitespace in Markdown headings.
Linked Issues check Passed Issue #12644 requires a heading such as # Learn C# to produce Learn C# as the inferred page title. The regex in packages/docusaurus-utils/src/markdownUtils.ts now removes trailing ATX closing ha…
Out of Scope Changes check Passed The diff changes only the title-trimming regex and adds focused unit tests for the linked issue behavior. The changes preserve existing ATX closing-hash and custom-ID handling. No unrelated implementa…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Linked Issues check

Explanation

Issue #12644 requires a heading such as # Learn C# to produce Learn C# as the inferred page title. The regex in packages/docusaurus-utils/src/markdownUtils.ts now removes trailing ATX closing hashes only after whitespace. It still removes custom IDs and valid closing sequences. New tests in markdownUtils.test.ts cover C# with and without a custom ID. The implementation satisfies the linked issue behavior.

Full details: Out of Scope Changes check

Explanation

The diff changes only the title-trimming regex and adds focused unit tests for the linked issue behavior. The changes preserve existing ATX closing-hash and custom-ID handling. No unrelated implementation or test changes are present.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 2 files.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

@netlify

netlify Bot commented Nov 3, 2026

Copy link
Copy Markdown

✅ [V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit 76c344c
🔍 Latest deploy log https://app.netlify.com/projects/docusaurus-2/deploys/6ae924b1a090ecc5fbaad00b
😎 Deploy Preview https://deploy-preview-12835--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Signed Facebook CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Page title drops a trailing # from the heading, e.g. # Learn C# becomes "Learn C" (AI-assisted)

1 participant