Skip to content

fix(content-docs): detect h1 title when export declaration precedes it - #12365

Open
harriiinnii wants to merge 1 commit into
facebook:mainfrom
harriiinnii:fix/mdx-title-detection-with-export-before-h1
Open

fix(content-docs): detect h1 title when export declaration precedes it#12365
harriiinnii wants to merge 1 commit into
facebook:mainfrom
harriiinnii:fix/mdx-title-detection-with-export-before-h1

Conversation

@harriiinnii

Copy link
Copy Markdown

Summary\n\nWhen an MDX file has an export declaration (e.g. export function Author()) before the # h1 heading, parseMarkdownContentTitle failed to detect the title. The page ended up using the site name instead of the h1 content.\n\n## Problem\n\nparseMarkdownContentTitle strips preamble declarations before searching for the h1, using this regex:\n\nts\n// Before\nconst contentWithoutImport = content\n .replace(/^(?:import\\s(?:.|\\r?\\n(?!\\r?\\n))*(?:\\r?\\n){2,})*/, '')\n .trim();\n\n\nThe regex only handled import statements. When an export block (like a component definition) appeared before the heading, it was left in place, causing the title search to fail against the exported function body instead of the actual heading.\n\nExample that fails before this fix:\n\nmdx\nimport PKG from '../../package.json';\n\nexport function Author() {\n return <a href={`mailto:${PKG.author.email}`}>{PKG.author.name}</a>;\n}\n\n# Privacy Policy\n\nFor any questions, please email <Author />.\n\n\n## Solution\n\nExtend the alternation from import\\s to (?:import|export)\\s so that export blocks followed by a blank line are also stripped before the heading search:\n\nts\n// After\nconst contentWithoutImport = content\n .replace(/^(?:(?:import|export)\\s(?:.|\\r?\\n(?!\\r?\\n))*(?:\\r?\\n){2,})*/, '')\n .trim();\n\n\nFixes #12331

When an MDX file has an export declaration (e.g. `export function Foo`)
before the h1 heading, parseMarkdownContentTitle failed to detect the
title. The regex that strips preamble declarations before searching for
the h1 only handled `import` statements, not `export` ones.

Changing the regex alternation from `import\s` to `(?:import|export)\s`
causes export blocks followed by a blank line to be skipped the same way
import blocks are, so the title search correctly reaches the h1.

Fixes facebook#12331
@meta-cla

meta-cla Bot commented Aug 17, 2026

Copy link
Copy Markdown

Hi @harriiinnii!

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 17, 2026

Copy link
Copy Markdown

[V2]

Built without sensitive environment variables

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

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.

1 participant