Repository navigation
fix(mdx-loader): support explicit heading IDs when mdx1Compat.comments is enabled #12932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,6 +61,16 @@ function getCommentHeadingId(heading: Heading): string | undefined { | |
| } | ||
| } | ||
|
|
||
| // HTML comment parsed by @slorber/remark-comment when mdx1Compat.comments is enabled | ||
| if ((lastChild?.type as string) === 'comment') { | ||
| const commentContent = ( | ||
| lastChild as unknown as {commentValue?: string} | ||
| ).commentValue; | ||
| if (commentContent) { | ||
| return getCommentContentHeadingId(commentContent); | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
|
|
@@ -85,23 +95,30 @@ function extractClassicSyntaxHeadingId(heading: Heading, headingText: string) { | |
| const parsedHeading = parseMarkdownHeadingId(headingText, 'classic'); | ||
| // Remove the heading text from its id (legacy syntax) | ||
| if (parsedHeading.id) { | ||
| // When there's an id, it is always in the last child node | ||
| const lastNode = heading.children.at(-1) as Text; | ||
| if (heading.children.length > 1) { | ||
| const lastNodeText = parseMarkdownHeadingId( | ||
| lastNode.value, | ||
| 'classic', | ||
| ).text; | ||
| // When the last part contains text + id, remove the id | ||
| if (lastNodeText) { | ||
| lastNode.value = lastNodeText; | ||
| } | ||
| // When last part contains only the id: completely remove that node | ||
| else { | ||
| heading.children.pop(); | ||
| // When there's an id, locate the last text node containing the classic syntax | ||
| const targetNode = heading.children | ||
| .filter((node): node is Text => node.type === 'text') | ||
| .at(-1); | ||
| if (targetNode) { | ||
| if (heading.children.length > 1) { | ||
| const targetNodeText = parseMarkdownHeadingId( | ||
| targetNode.value, | ||
| 'classic', | ||
| ).text; | ||
| // When the last part contains text + id, remove the id | ||
| if (targetNodeText) { | ||
| targetNode.value = targetNodeText; | ||
| } | ||
| // When last part contains only the id: completely remove that node | ||
| else { | ||
| const index = heading.children.indexOf(targetNode); | ||
| if (index !== -1) { | ||
| heading.children.splice(index, 1); | ||
| } | ||
| } | ||
| } else { | ||
| targetNode.value = parsedHeading.text; | ||
| } | ||
|
Comment on lines
+98
to
121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } else { | ||
| lastNode.value = parsedHeading.text; | ||
| } | ||
| return parsedHeading.id; | ||
| } | ||
|
|
@@ -114,7 +131,7 @@ const plugin: Plugin<PluginOptions[], Root> = function plugin({ | |
| return (root) => { | ||
| function getHeadingText(heading: Heading) { | ||
| const headingTextNodes = heading.children.filter( | ||
| ({type}) => !['html', 'jsx'].includes(type), | ||
| ({type}) => !['html', 'jsx', 'comment'].includes(type), | ||
| ); | ||
| return toString(headingTextNodes.length > 0 ? headingTextNodes : heading); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: meta/docusaurus
Length of output: 30012
Script executed:
Repository: meta/docusaurus
Length of output: 9564
Web query:
CommonMark 0.31.2 inline parsing adjacent text nodes emphasis whitespace text node example heading AST mdast remarkResult:
Script executed:
Repository: meta/docusaurus
Length of output: 18101
Script executed:
Repository: meta/docusaurus
Length of output: 13735
Web query:
github syntax-tree mdast-util-to-string 4.0.0 source toString array concatenate text childrenResult:
Trim whitespace before a removed classic ID.
If an earlier remark plugin splits the separator and
{#id}into separate text nodes, removing the ID leaves trailing whitespace in the heading AST. Trim the preceding text node when removing the ID-only node.