diff --git a/src/components/ArticleSidebar.astro b/src/components/ArticleSidebar.astro index eb2305a..8317afa 100644 --- a/src/components/ArticleSidebar.astro +++ b/src/components/ArticleSidebar.astro @@ -7,6 +7,11 @@ type TocItem = { id: string; }; +type TocSection = { + heading: TocItem; + children: TocItem[]; +}; + interface Props { post: BlogPost; toc: TocItem[]; @@ -15,6 +20,16 @@ interface Props { const { post, toc, relatedPosts } = Astro.props; const avatar = 'https://free.picui.cn/free/2025/08/10/689845496a283.png'; +const topDepth = toc.length > 0 ? Math.min(...toc.map((item) => item.depth)) : 1; +const tocSections = toc.reduce((sections, item) => { + if (item.depth <= topDepth || sections.length === 0) { + sections.push({ heading: item, children: [] }); + } else { + sections[sections.length - 1].children.push(item); + } + + return sections; +}, []); ---