Skip to content

Commit

Permalink
fix(remark-toc): support display emphasis and strong in toc
Browse files Browse the repository at this point in the history
  • Loading branch information
Plasticine-Yang committed Jan 5, 2023
1 parent 6b8d80a commit 0c2dd70
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/island/src/node/plugin-mdx/remarkPlugins/toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface TocItem {
}

interface ChildNode {
type: 'link' | 'text' | 'inlineCode';
type: 'link' | 'text' | 'inlineCode' | 'emphasis' | 'strong';
value: string;
children?: ChildNode[];
}
Expand Down Expand Up @@ -42,10 +42,21 @@ export const remarkPluginToc: Plugin<[], Root> = () => {
if (node.depth > 1 && node.depth < 5) {
const originText = node.children
.map((child: ChildNode) => {
if (child.type === 'link') {
return child.children?.map((item) => item.value).join('');
} else {
return child.value;
switch (child.type) {
// child with value
case 'text':
case 'inlineCode':
return child.value;

// child without value, but can get value from children property
case 'emphasis':
case 'strong':
case 'link':
return child.children?.map((c) => c.value).join('') || '';

// child without value and can not get value from children property
default:
return '';
}
})
.join('');
Expand Down

0 comments on commit 0c2dd70

Please sign in to comment.