mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-04 03:42:34 +02:00
fix(mdx-loader): make headings containing links properly formatted in ToC (#6712)
This commit is contained in:
parent
bbc0562d67
commit
692680d1d1
1 changed files with 19 additions and 21 deletions
|
@ -8,31 +8,29 @@
|
||||||
import escapeHtml from 'escape-html';
|
import escapeHtml from 'escape-html';
|
||||||
import toString from 'mdast-util-to-string';
|
import toString from 'mdast-util-to-string';
|
||||||
import type {Parent} from 'unist';
|
import type {Parent} from 'unist';
|
||||||
import type {StaticPhrasingContent, Heading} from 'mdast';
|
import type {PhrasingContent, Heading} from 'mdast';
|
||||||
|
|
||||||
export function stringifyContent(node: Parent): string {
|
export function stringifyContent(node: Parent): string {
|
||||||
return ((node.children || []) as StaticPhrasingContent[])
|
return ((node.children || []) as PhrasingContent[]).map(toValue).join('');
|
||||||
.map(toValue)
|
|
||||||
.join('');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toValue(node: StaticPhrasingContent | Heading): string {
|
export function toValue(node: PhrasingContent | Heading): string {
|
||||||
if (node && node.type) {
|
switch (node?.type) {
|
||||||
switch (node.type) {
|
case 'text':
|
||||||
case 'text':
|
return escapeHtml(node.value);
|
||||||
return escapeHtml(node.value);
|
case 'heading':
|
||||||
case 'heading':
|
return stringifyContent(node);
|
||||||
return stringifyContent(node);
|
case 'inlineCode':
|
||||||
case 'inlineCode':
|
return `<code>${escapeHtml(node.value)}</code>`;
|
||||||
return `<code>${escapeHtml(node.value)}</code>`;
|
case 'emphasis':
|
||||||
case 'emphasis':
|
return `<em>${stringifyContent(node)}</em>`;
|
||||||
return `<em>${stringifyContent(node)}</em>`;
|
case 'strong':
|
||||||
case 'strong':
|
return `<strong>${stringifyContent(node)}</strong>`;
|
||||||
return `<strong>${stringifyContent(node)}</strong>`;
|
case 'delete':
|
||||||
case 'delete':
|
return `<del>${stringifyContent(node)}</del>`;
|
||||||
return `<del>${stringifyContent(node)}</del>`;
|
case 'link':
|
||||||
default:
|
return stringifyContent(node);
|
||||||
}
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
return toString(node);
|
return toString(node);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue