fix(utils): handle '#' inside heading text when creating excerpt - #12352
fix(utils): handle '#' inside heading text when creating excerpt#12352rexblade58 wants to merge 1 commit into
Conversation
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
|
Hi @rexblade58! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
✅ [V2]Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
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. |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
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#orF#), 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:/^#[^#]+#?/gm— matched# C#(leading#+C+ the#inC#), leavingProgramming Guidebehind/^#{1,6}\s*(?<text>[^#]*?)\s*#{0,6}/gm— the lazy[^#]*?also stopped at the interior#Fix
/^#.*$/gm), so headings containingC#/F#are skipped entirely like any other H1#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)