Skip to content

fix(utils): correct auto-generated description when H1 heading text contains # (e.g. C#)#12308

Open
DhineshPonnarasan wants to merge 3 commits into
facebook:mainfrom
DhineshPonnarasan:fix/h1-csharp-excerpt
Open

fix(utils): correct auto-generated description when H1 heading text contains # (e.g. C#)#12308
DhineshPonnarasan wants to merge 3 commits into
facebook:mainfrom
DhineshPonnarasan:fix/h1-csharp-excerpt

Conversation

@DhineshPonnarasan

@DhineshPonnarasan DhineshPonnarasan commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #12305 — auto-generated meta description is incorrect when an H1 heading contains # in its text (e.g., C#, F#).

Root Cause

In createExcerpt(), the H1-stripping regex /^#[^#]+#?/gm uses [^#]+ (one or more non-hash characters) followed by an optional #?. The [^#] character class was originally intended as a sentinel to prevent matching ## H2+ headings, but it also acts as a delimiter that stops at any # in the heading text.

For # C# Programming Guide:

  • #[^#]+ matches # C
  • #? consumes the # (from C#)
  • Replace removes # C#, leaving Programming Guide as the excerpt

Why this is the smallest safe fix

The regex is changed from /^#[^#]+#?/gm to /^#(?!#).*/gm:

  • ^# matches the opening hash
  • (?!#) negative lookahead distinguishes H1 from H2+ (equivalent to ##)
  • .* consumes the entire line, including any # characters in the text

The negative lookahead (?!#) is the correct semantic primitive — it says "match H1 but not H2+" without introducing a character class that collides with heading text content. No other part of the pipeline changes, and no dependencies are added.

Tests added

Test What it guards
H1 with C# The reported bug
H1 with F# Same class of issue
H1 with trailing ATX # # C# Programming Guide # also affected
H2+ with C# Regression: H2 extraction must remain unchanged
Normal H1 Regression: no-hash headings still fully removed

All 275 existing tests continue to pass.

AI disclosure: This PR was authored with the assistance of an AI coding agent, under my direction and review.

…#12120)

The example artifacts were regenerated for the v3.10.1 maintenance
release, which reverted the engine field from '>=24.14' back to
'>=20.0' and left the sandbox node version at '18'.

Update the examples to reflect current maintainer intent:
- Bump engines.node from '>=20.0' to '>=24.14'
  (reapplies PR facebook#11914 change overwritten by PR facebook#11985)
- Bump sandbox.config.json node from '18' to '24'
  (syncs with generateExamples.js updated in PR facebook#12080)
@meta-cla meta-cla Bot added the CLA Signed Signed Facebook CLA label Jul 22, 2026
@netlify

netlify Bot commented Jul 22, 2026

Copy link
Copy Markdown

[V2]

Built without sensitive environment variables

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

QR Code

Use your smartphone camera to open QR code link.

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

…ontains # (e.g. C#)

## Root Cause

The H1 heading regex /^#[^#]+#?/gm used [^#]+ (one or more non-hash chars)
followed by an optional #?. When heading text contained # (e.g., C#),
the [^#]+ matched up to the first # inside the text, and #? consumed it.

For # C# Programming Guide:
- #[^#]+ matched # C
- #? consumed the # (from C#)
- Leftover:  Programming Guide → became the excerpt

## Fix

Changed regex to /^#(?!#).*/gm:
- ^# matches the opening hash
- (?!#) negative lookahead ensures this is H1 (not ## H2+)
- .* consumes the entire line, including any # characters in the text

The negative lookahead (?!#) is the correct way to distinguish H1 from H2+
without relying on [^#] as a delimiter for heading text content.

## Tests Added

- H1 with C# in heading text
- H1 with F# in heading text
- H1 with trailing ATX closing marker (# C# Programming Guide #)
- H2+ with C# in heading text (regression: H2 extraction unchanged)
- Normal H1 (no hash in text) still correctly stripped

All 275 existing tests continue to pass.

Fixes facebook#12305
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.

Auto-generated meta description is incorrect when an H1 heading contains # in its text (e.g. C#, F#)

1 participant