From baf405991ab6132209eb9615caa796c83a77405c Mon Sep 17 00:00:00 2001 From: Jibin7Jose Date: Sun, 2 Aug 2026 12:35:25 +0530 Subject: [PATCH] fix(utils): allow MDX to detect title when a component is exported before h1 --- .../src/__tests__/markdownUtils.test.ts | 20 +++++++++++++++++++ .../docusaurus-utils/src/markdownUtils.ts | 8 ++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts index a81e8cfff103..f74b3b1b3233 100644 --- a/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/markdownUtils.test.ts @@ -572,6 +572,26 @@ Lorem Ipsum }); }); + it('parses markdown h1 title placed after export declarations', () => { + const markdown = dedent` + export function Author() { + return ( + Author + ); + } + + # Markdown Title + + Lorem Ipsum + + `; + + expect(parseMarkdownContentTitle(markdown)).toEqual({ + content: markdown, + contentTitle: 'Markdown Title', + }); + }); + it('parses markdown h1 title placed after multiple import declarations', () => { const markdown = dedent` import Component1 from '@site/src/components/Component1'; diff --git a/packages/docusaurus-utils/src/markdownUtils.ts b/packages/docusaurus-utils/src/markdownUtils.ts index 216d0122e7ad..09b5edcf8ef2 100644 --- a/packages/docusaurus-utils/src/markdownUtils.ts +++ b/packages/docusaurus-utils/src/markdownUtils.ts @@ -255,11 +255,11 @@ export function parseMarkdownContentTitle( const removeContentTitleOption = options?.removeContentTitle ?? false; const content = contentUntrimmed.trim(); - // We only need to detect import statements that will be parsed by MDX as - // `import` nodes, as broken syntax can't render anyways. That means any block - // that has `import` at the very beginning and surrounded by empty lines. + // We only need to detect import/export statements that will be parsed by MDX as + // `import` or `export` nodes, as broken syntax can't render anyways. That means any block + // that has `import` or `export` at the very beginning and surrounded by empty lines. const contentWithoutImport = content - .replace(/^(?:import\s(?:.|\r?\n(?!\r?\n))*(?:\r?\n){2,})*/, '') + .replace(/^(?:(?:import|export)\s(?:.|\r?\n(?!\r?\n))*(?:\r?\n){2,})*/, '') .trim(); const regularTitleMatch = /^#[ \t]+(?[^ \t].*)(?:\r?\n|$)/.exec(