From 0c2dd70e5421a7e11d4c07d638e884ef09530e0c Mon Sep 17 00:00:00 2001 From: plasticine9750 <975036719@qq.com> Date: Thu, 5 Jan 2023 19:03:28 +0800 Subject: [PATCH] fix(remark-toc): support display emphasis and strong in toc --- .../src/node/plugin-mdx/remarkPlugins/toc.ts | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/island/src/node/plugin-mdx/remarkPlugins/toc.ts b/packages/island/src/node/plugin-mdx/remarkPlugins/toc.ts index b4951bb0..64a455a0 100644 --- a/packages/island/src/node/plugin-mdx/remarkPlugins/toc.ts +++ b/packages/island/src/node/plugin-mdx/remarkPlugins/toc.ts @@ -14,7 +14,7 @@ interface TocItem { } interface ChildNode { - type: 'link' | 'text' | 'inlineCode'; + type: 'link' | 'text' | 'inlineCode' | 'emphasis' | 'strong'; value: string; children?: ChildNode[]; } @@ -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('');