Skip to content

fix(utils): strip MDX export blocks before heading detection in parseMarkdownContentTitle - #12332

Draft
okxint wants to merge 1 commit into
facebook:mainfrom
okxint:fix/markdown-export-before-title-not-stripped
Draft

fix(utils): strip MDX export blocks before heading detection in parseMarkdownContentTitle#12332
okxint wants to merge 1 commit into
facebook:mainfrom
okxint:fix/markdown-export-before-title-not-stripped

Conversation

@okxint

@okxint okxint commented Aug 1, 2026

Copy link
Copy Markdown

parseMarkdownContentTitle strips leading MDX import blocks via a regex before looking for # h1, but does not strip export declarations (e.g. export function Author() {...} or export const meta = ...). When an MDX file has an export before the heading, the stripped content still starts with export ..., the title regex finds no match at position 0, and Docusaurus falls back to the site name for <title> and the sidebar.

Fix: add export as an alternative alongside import in the existing regex. The change is minimal — import\s becomes (?:import|export)\s — and the existing import-stripping behaviour is preserved exactly.

Fixes #12331

@meta-cla

meta-cla Bot commented Aug 1, 2026

Copy link
Copy Markdown

Hi @okxint!

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

Copy link
Copy Markdown

[V2]

Built without sensitive environment variables

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

@SethFalco SethFalco left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Note, I'm not a maintainer, just the author of the original issue. I'm weighting in with my review but when another maintainer chimes in, their feedback should get precedence.)

The fix itself looks acceptable to me.

In the JSDoc it explicitly stated before that the function "[ignores] import/export declarations", which wasn't true before. This PR is what makes the documentation accurate!

I'd expect to see at least one new test cases added too in packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts:

  it('parses markdown h1 title placed after export declarations and remove it', () => {
    const markdown = dedent`
          export function Uwu() {
            return <>uwu</>;
          }

          # Markdown Title

          <Uwu/> Ipsum

        `;

    expect(
      parseMarkdownContentTitle(markdown, {removeContentTitle: true}),
    ).toEqual({
      content: markdown.replace('# Markdown Title\n', ''),
      contentTitle: 'Markdown Title',
    });
  });

While reviewing locally, I had added this test case as well, but I see it would fail as the title parsing won't actually render any JSX that may appear in headings. The title is set to <Uwu/> Title literally. As the doc says, the function "won't be best effort", so in this PR at least, I don't think any changes to fix this would be worthwhile. Just making note of it and leaving the test case for future reference. I personally don't have a use case to put JSX in headings anyway, so happy to pretend I didn't see that.

  it('parses markdown h1 title placed after multiple export declarations used in h1 and and remove it', () => {
    const markdown = dedent`
          export function Uwu() {
            return <>uwu</>;
          };

          # <Uwu/> Title

          Lorem Ipsum

        `;

    expect(
      parseMarkdownContentTitle(markdown, {removeContentTitle: true}),
    ).toEqual({
      content: markdown.replace('# <Uwu/> Title\n', ''),
      contentTitle: 'uwu Title',
    });
  });

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.

MDX doesn't detect title if a React component is defined before <h1>

2 participants