fix(mdx-loader): Ignore contentTitle coming after Markdown thematicBreak (#9999)

This commit is contained in:
Sébastien Lorber 2024-03-29 15:07:53 +01:00 committed by GitHub
parent 821247142e
commit 1a5fe5c412
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 9 deletions

View file

@ -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*
`);

View file

@ -34,7 +34,9 @@ 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) => {
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) {
@ -47,6 +49,11 @@ const plugin: Plugin = function plugin(
if (headingNode.depth >= 1) {
return EXIT;
}
}
// We only handle contentTitle when it's above the first thematic break
if (node.type === 'thematicBreak') {
return EXIT;
}
return undefined;
});
};