fix(mdx-loader): make headings containing links properly formatted in ToC (#6712)

This commit is contained in:
Joshua Chen 2022-02-18 10:57:08 +08:00 committed by GitHub
parent bbc0562d67
commit 692680d1d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,17 +8,14 @@
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':
@ -31,9 +28,10 @@ export function toValue(node: StaticPhrasingContent | Heading): string {
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':
return stringifyContent(node);
default: default:
} }
}
return toString(node); return toString(node);
} }