From 1a5fe5c4124223ec2bc3683f54d8d190c1358f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lorber?= Date: Fri, 29 Mar 2024 15:07:53 +0100 Subject: [PATCH] fix(mdx-loader): Ignore contentTitle coming after Markdown thematicBreak (#9999) --- .../contentTitle/__tests__/index.test.ts | 15 +++++++++++ .../src/remark/contentTitle/index.ts | 25 ++++++++++++------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/packages/docusaurus-mdx-loader/src/remark/contentTitle/__tests__/index.test.ts b/packages/docusaurus-mdx-loader/src/remark/contentTitle/__tests__/index.test.ts index 439b9d46634e..54d111754833 100644 --- a/packages/docusaurus-mdx-loader/src/remark/contentTitle/__tests__/index.test.ts +++ b/packages/docusaurus-mdx-loader/src/remark/contentTitle/__tests__/index.test.ts @@ -65,6 +65,21 @@ some **markdown** *content* # contentTitle 1 +some **markdown** *content* + `); + + expect(result.data.contentTitle).toBeUndefined(); + }); + + it('ignore contentTitle if after thematic break', async () => { + const result = await process(` + +Hey + +--- + +# contentTitle 1 + some **markdown** *content* `); diff --git a/packages/docusaurus-mdx-loader/src/remark/contentTitle/index.ts b/packages/docusaurus-mdx-loader/src/remark/contentTitle/index.ts index 89c66a7e650b..65ad91f82528 100644 --- a/packages/docusaurus-mdx-loader/src/remark/contentTitle/index.ts +++ b/packages/docusaurus-mdx-loader/src/remark/contentTitle/index.ts @@ -34,17 +34,24 @@ const plugin: Plugin = function plugin( const {toString} = await import('mdast-util-to-string'); const {visit, EXIT} = await import('unist-util-visit'); - visit(root, 'heading', (headingNode: Heading, index, parent) => { - if (headingNode.depth === 1) { - vfile.data.contentTitle = toString(headingNode); - if (removeContentTitle) { - // @ts-expect-error: TODO how to fix? - parent!.children.splice(index, 1); + visit(root, ['heading', 'thematicBreak'], (node, index, parent) => { + if (node.type === 'heading') { + const headingNode = node as Heading; + if (headingNode.depth === 1) { + vfile.data.contentTitle = toString(headingNode); + if (removeContentTitle) { + // @ts-expect-error: TODO how to fix? + parent!.children.splice(index, 1); + } + return EXIT; // We only handle the very first heading + } + // We only handle contentTitle if it's the very first heading found + if (headingNode.depth >= 1) { + return EXIT; } - return EXIT; // We only handle the very first heading } - // We only handle contentTitle if it's the very first heading found - if (headingNode.depth >= 1) { + // We only handle contentTitle when it's above the first thematic break + if (node.type === 'thematicBreak') { return EXIT; } return undefined;