Skip to content

Commit 1a5fe5c

Browse files
authored
fix(mdx-loader): Ignore contentTitle coming after Markdown thematicBreak (#9999)
1 parent 8212471 commit 1a5fe5c

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

packages/docusaurus-mdx-loader/src/remark/contentTitle/__tests__/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ some **markdown** *content*
6565
6666
# contentTitle 1
6767
68+
some **markdown** *content*
69+
`);
70+
71+
expect(result.data.contentTitle).toBeUndefined();
72+
});
73+
74+
it('ignore contentTitle if after thematic break', async () => {
75+
const result = await process(`
76+
77+
Hey
78+
79+
---
80+
81+
# contentTitle 1
82+
6883
some **markdown** *content*
6984
`);
7085

packages/docusaurus-mdx-loader/src/remark/contentTitle/index.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,24 @@ const plugin: Plugin = function plugin(
3434
const {toString} = await import('mdast-util-to-string');
3535
const {visit, EXIT} = await import('unist-util-visit');
3636

37-
visit(root, 'heading', (headingNode: Heading, index, parent) => {
38-
if (headingNode.depth === 1) {
39-
vfile.data.contentTitle = toString(headingNode);
40-
if (removeContentTitle) {
41-
// @ts-expect-error: TODO how to fix?
42-
parent!.children.splice(index, 1);
37+
visit(root, ['heading', 'thematicBreak'], (node, index, parent) => {
38+
if (node.type === 'heading') {
39+
const headingNode = node as Heading;
40+
if (headingNode.depth === 1) {
41+
vfile.data.contentTitle = toString(headingNode);
42+
if (removeContentTitle) {
43+
// @ts-expect-error: TODO how to fix?
44+
parent!.children.splice(index, 1);
45+
}
46+
return EXIT; // We only handle the very first heading
47+
}
48+
// We only handle contentTitle if it's the very first heading found
49+
if (headingNode.depth >= 1) {
50+
return EXIT;
4351
}
44-
return EXIT; // We only handle the very first heading
4552
}
46-
// We only handle contentTitle if it's the very first heading found
47-
if (headingNode.depth >= 1) {
53+
// We only handle contentTitle when it's above the first thematic break
54+
if (node.type === 'thematicBreak') {
4855
return EXIT;
4956
}
5057
return undefined;

0 commit comments

Comments
 (0)