fix(content-docs): detect h1 title when export declaration precedes it - #12365
fix(content-docs): detect h1 title when export declaration precedes it#12365harriiinnii wants to merge 1 commit into
Conversation
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
|
Hi @harriiinnii! 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. |
Summary\n\nWhen an MDX file has an
exportdeclaration (e.g.export function Author()) before the# h1heading,parseMarkdownContentTitlefailed to detect the title. The page ended up using the site name instead of the h1 content.\n\n## Problem\n\nparseMarkdownContentTitlestrips 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 handledimportstatements. When anexportblock (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 fromimport\\sto(?:import|export)\\sso thatexportblocks 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