Skip to content

fix(utils): preserve trailing hash in markdown title when not preceded by whitespace - #13055

Open
TanmayNileshShinde wants to merge 1 commit into
meta:mainfrom
TanmayNileshShinde:fix/heading-trailing-hash
Open

TanmayNileshShinde wants to merge 1 commit into
meta:mainfrom
TanmayNileshShinde:fix/heading-trailing-hash

Conversation

@TanmayNileshShinde

@TanmayNileshShinde TanmayNileshShinde commented Nov 15, 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

When a Markdown document has an ATX heading with a word ending in # (such as # Learn C# or # F# Guide) and no front matter title, parseMarkdownContentTitle incorrectly truncated the title to "Learn C" or "F".

Root Cause

In packages/docusaurus-utils/src/markdownUtils.ts, heading title cleanup previously used:

.replace(/\s*(?:\{#*[\w-]+\}|#+)$/, '')

Because \s* matches zero whitespace, |#+$ matched trailing # attached directly to words.

Per CommonMark Spec §4.2 (ATX headings):

The closing sequence of # characters must be preceded by at least one space or tab.

By separating anchor ID removal (\s*\{#*[\w-]+\}$) and requiring preceding whitespace for closing hashes (\s+#+$), trailing # characters in names like C# and F# are preserved, while ATX closing markers (e.g. # Learn C# #) and custom anchor IDs ({#custom-id}) are still properly stripped.

Fixes #12644.

Test Plan

Added unit tests in packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts:

  • # Learn C# -> "Learn C#"
  • # Learn C# # -> "Learn C#"
  • # Learn C# {#my-custom-id} -> "Learn C#"

Ran Vitest:

pnpm --filter @docusaurus/utils exec vitest run src/__tests__/markdownUtils.test.ts

All 95 unit tests pass.

Related issues/PRs

Fixes #12644

Summary by CodeRabbit

  • Bug Fixes
    • Markdown page titles containing C# are now parsed correctly when followed by closing heading markers, an anchor ID, or both. The original Markdown content remains unchanged.

…d by whitespace

Per CommonMark Spec Section 4.2 (ATX headings), a closing sequence of # characters must be preceded by at least one space or tab. Previously, parseMarkdownContentTitle stripped trailing hashes without requiring preceding whitespace, causing titles like "
@meta-cla

meta-cla Bot commented Nov 15, 2026

Copy link
Copy Markdown

Hi @TanmayNileshShinde!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The title parser now removes trailing custom heading IDs and closing heading markers in separate steps. Three tests cover C# headings without a closing marker, with a closing marker, and with a custom ID.

Changes: Markdown title parsing

Layer / File(s) Summary
Title cleanup and regression tests
packages/docusaurus-utils/src/markdownUtils.ts, packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts
The parser removes a trailing custom heading ID before removing trailing heading markers. Tests check the parsed title for three C# heading forms and confirm that the Markdown content remains unchanged.

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

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

Suggested labels: pr: bug fix

Merge Risk: Minimal · up to 25395

Headings such as "Learn C#" now keep their trailing hash in page titles, while closing heading markers and custom IDs are still removed. The change is small and tested, and no merge-blocking risk is identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title says the fix preserves a trailing hash in a Markdown title when it is not preceded by whitespace. The changeset does this. The markdownUtils.ts change splits the trailing heading marker and …
Linked Issues check Passed Issue #12644 requires that an H1 such as # Learn C# without a front matter title yields the title Learn C#. The reviewed head changes the title cleanup in parseMarkdownContentTitle (packages/doc…
Out of Scope Changes check Passed The whole-PR diff against the merge base touches two files. markdownUtils.ts changes only the title-cleanup replacement in parseMarkdownContentTitle, which is the behavior named in #12644. markdownUti…
Full details: Title check

Explanation

The title says the fix preserves a trailing hash in a Markdown title when it is not preceded by whitespace. The changeset does this. The markdownUtils.ts change splits the trailing heading marker and custom ID removal into separate replacements, so a trailing # attached to a word such as C# is kept. The added tests cover Learn C#, Learn C# #, and Learn C# with a custom ID. This matches the linked issue about truncated titles like Learn C. The title is specific and describes the main behavior change.

Full details: Linked Issues check

Explanation

Issue #12644 requires that an H1 such as # Learn C# without a front matter title yields the title Learn C#. The reviewed head changes the title cleanup in parseMarkdownContentTitle (packages/docusaurus-utils/src/markdownUtils.ts). The old single replacement /\s*(?:\{#*[\w-]+\}|#+)$/ also stripped a # attached to a word. The new code runs .replace(/\s*\{#*[\w-]+\}$/, '') and then .replace(/\s+#+$/, ''). The second pattern requires whitespace before the closing hashes, as CommonMark requires. Tracing the cases: Learn C# keeps C#. Learn C# # becomes Learn C#. Learn C# {#my-custom-id} becomes Learn C#. The PR adds unit tests for these three inputs in markdownUtils.test.ts. The tests assert the expected contentTitle and that content is unchanged. The change meets the coding requirement. The tests were not executed during this assessment. The result rests on source tracing of the regex behavior.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base touches two files. markdownUtils.ts changes only the title-cleanup replacement in parseMarkdownContentTitle, which is the behavior named in #12644. markdownUtils.test.ts adds three tests for the same function covering C#, C# #, and C# {#my-custom-id}. Both changes connect directly to the linked issue. No unrelated changes were found in the diff.

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

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: 32084851-b0c6-40b9-bd0c-f157a37e8853
Commits

Reviewing files that changed from the base of the PR and between 4c4ec4a and 2539526.

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.

@netlify

netlify Bot commented Nov 15, 2026

Copy link
Copy Markdown

✅ [V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit 2539526
🔍 Latest deploy log https://app.netlify.com/projects/docusaurus-2/deploys/6af95ebfacf61628cdf7c4e5
😎 Deploy Preview https://deploy-preview-13055--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

None yet

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