Skip to content

Commit

Permalink
fix(mdx-loader): make headings containing links properly formatted in…
Browse files Browse the repository at this point in the history
… ToC (#6712)
  • Loading branch information
Josh-Cena authored Feb 18, 2022
1 parent bbc0562 commit 692680d
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions packages/docusaurus-mdx-loader/src/remark/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,29 @@
import escapeHtml from 'escape-html';
import toString from 'mdast-util-to-string';
import type {Parent} from 'unist';
import type {StaticPhrasingContent, Heading} from 'mdast';
import type {PhrasingContent, Heading} from 'mdast';

export function stringifyContent(node: Parent): string {
return ((node.children || []) as StaticPhrasingContent[])
.map(toValue)
.join('');
return ((node.children || []) as PhrasingContent[]).map(toValue).join('');
}

export function toValue(node: StaticPhrasingContent | Heading): string {
if (node && node.type) {
switch (node.type) {
case 'text':
return escapeHtml(node.value);
case 'heading':
return stringifyContent(node);
case 'inlineCode':
return `<code>${escapeHtml(node.value)}</code>`;
case 'emphasis':
return `<em>${stringifyContent(node)}</em>`;
case 'strong':
return `<strong>${stringifyContent(node)}</strong>`;
case 'delete':
return `<del>${stringifyContent(node)}</del>`;
default:
}
export function toValue(node: PhrasingContent | Heading): string {
switch (node?.type) {
case 'text':
return escapeHtml(node.value);
case 'heading':
return stringifyContent(node);
case 'inlineCode':
return `<code>${escapeHtml(node.value)}</code>`;
case 'emphasis':
return `<em>${stringifyContent(node)}</em>`;
case 'strong':
return `<strong>${stringifyContent(node)}</strong>`;
case 'delete':
return `<del>${stringifyContent(node)}</del>`;
case 'link':
return stringifyContent(node);
default:
}

return toString(node);
Expand Down

0 comments on commit 692680d

Please sign in to comment.