Skip to content

fix(utils): handle '#' inside heading text when creating excerpt - #12352

Open
rexblade58 wants to merge 1 commit into
facebook:mainfrom
rexblade58:fix/excerpt-hash-heading
Open

fix(utils): handle '#' inside heading text when creating excerpt#12352
rexblade58 wants to merge 1 commit into
facebook:mainfrom
rexblade58:fix/excerpt-hash-heading

Conversation

@rexblade58

Copy link
Copy Markdown

Description

Fixes #12305

When a doc page has no explicit front matter description, Docusaurus generates one from the page content. For pages whose first H1 heading contains a hash character (e.g. C# or F#), the generated description was a fragment of the heading instead of the first paragraph.

Before:

# C# Programming Guide

This paragraph should become the description.

produced <meta name="description" content="Programming Guide">

After:
<meta name="description" content="This paragraph should become the description.">

Root cause

Two regexes in createExcerpt() used a negated character class [^#] that stopped at any interior #, interpreting it as a closing ATX marker:

  1. /^#[^#]+#?/gm — matched # C# (leading # + C + the # in C#), leaving Programming Guide behind
  2. /^#{1,6}\s*(?<text>[^#]*?)\s*#{0,6}/gm — the lazy [^#]*? also stopped at the interior #

Fix

  • Single-# title removal now matches the whole line (/^#.*$/gm), so headings containing C#/F# are skipped entirely like any other H1
  • The ATX stripper now allows # in the heading text (.*?) and only strips an optional trailing run of hashes anchored at the end of the line (#*$)

Tests

Added regression tests covering:

  • # C# Programming Guide → paragraph used
  • # F# Programming Guide # (trailing closing hash) → paragraph used
  • # Java Programming Guide (control case, unchanged)
  • ## C# Setup Guide (H2, unchanged per issue)
  • hash characters preserved in plain paragraph text

The excerpt auto-generated from a page's first H1 heading was incorrect
when the heading text contained a hash character (e.g. "C#", "F#").

- "# C# Programming Guide" produced "Programming Guide" instead of
  skipping the heading and using the first paragraph
- The old regexes used a negated character class that stopped at any
  interior hash, treating it as a closing ATX marker

Two regexes in createExcerpt() are fixed:

1. Single-hash title removal now matches the whole line so headings
   containing "C#"/"F#" are skipped entirely like any other H1

2. The ATX heading marker stripper now allows hash characters inside
   the heading text and only strips an optional trailing run of hashes
   anchored at the end of the line

Also add regression tests for C#/F# headings, trailing closing hashes,
and hash characters in plain paragraph text.

Closes facebook#12305
@meta-cla

meta-cla Bot commented Aug 8, 2026

Copy link
Copy Markdown

Hi @rexblade58!

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!

@netlify

netlify Bot commented Aug 8, 2026

Copy link
Copy Markdown

[V2]

Built without sensitive environment variables

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

@rexblade58

Copy link
Copy Markdown
Author

Following up on the fix - also found a related issue (#12331) where MDX export declarations before the H1 break title detection. Preparing a second PR for that.

@meta-cla

meta-cla Bot commented Aug 8, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed Signed Facebook CLA label Aug 8, 2026
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