import { Anchor } from 'antd'; import type { AnchorLinkItemProps } from 'antd/es/anchor/Anchor'; import React, { useEffect, useState } from 'react'; interface MarkdownTocProps { content: string; } const MarkdownToc: React.FC = ({ content }) => { const [items, setItems] = useState([]); useEffect(() => { const generateTocItems = () => { const headings = document.querySelectorAll( '.wmde-markdown h2, .wmde-markdown h3', ); const tocItems: AnchorLinkItemProps[] = []; let currentH2Item: AnchorLinkItemProps | null = null; headings.forEach((heading) => { const title = heading.textContent || ''; const id = heading.id; const isH2 = heading.tagName.toLowerCase() === 'h2'; if (id && title) { const item: AnchorLinkItemProps = { key: id, href: `#${id}`, title, }; if (isH2) { currentH2Item = item; tocItems.push(item); } else { if (currentH2Item) { if (!currentH2Item.children) { currentH2Item.children = []; } currentH2Item.children.push(item); } else { tocItems.push(item); } } } }); setItems(tocItems.slice(1)); }; setTimeout(generateTocItems, 100); }, [content]); return (
); }; export default MarkdownToc;