Skip to content

Commit 7151555

Browse files
authored
feat(cli): write-heading-ids CLI now supports the --syntax option (facebook#11777)
1 parent 63ccba8 commit 7151555

10 files changed

Lines changed: 1046 additions & 471 deletions

File tree

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,18 @@ function extractCommentId(heading: Heading) {
7878
return undefined;
7979
}
8080

81-
// Try to find an explicit id in the heading text (legacy {#id} syntax)
82-
function extractLegacySyntaxId(heading: Heading, headingText: string) {
83-
const parsedHeading = parseMarkdownHeadingId(headingText);
81+
// Try to find an explicit id in the heading text (classic {#id} syntax)
82+
function extractClassicSyntaxHeadingId(heading: Heading, headingText: string) {
83+
const parsedHeading = parseMarkdownHeadingId(headingText, 'classic');
8484
// Remove the heading text from its id (legacy syntax)
8585
if (parsedHeading.id) {
8686
// When there's an id, it is always in the last child node
8787
const lastNode = heading.children.at(-1) as Text;
8888
if (heading.children.length > 1) {
89-
const lastNodeText = parseMarkdownHeadingId(lastNode.value).text;
89+
const lastNodeText = parseMarkdownHeadingId(
90+
lastNode.value,
91+
'classic',
92+
).text;
9093
// When the last part contains text + id, remove the id
9194
if (lastNodeText) {
9295
lastNode.value = lastNodeText;
@@ -135,7 +138,7 @@ const plugin: Plugin<PluginOptions[], Root> = function plugin({
135138
function extractIdFromText() {
136139
const headingText = getHeadingText(heading);
137140
return (
138-
extractLegacySyntaxId(heading, headingText) ??
141+
extractClassicSyntaxHeadingId(heading, headingText) ??
139142
slugs.slug(headingText, {maintainCase: anchorsMaintainCase})
140143
);
141144
}

0 commit comments

Comments
 (0)