Skip to content

Commit 370ede5

Browse files
committed
修复移动端没有目录也会展示 toc 按钮的问题
1 parent 4c48eed commit 370ede5

7 files changed

Lines changed: 55 additions & 40 deletions

File tree

packages/pure/components/pages/TOC.astro

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ const { headings, class: className, ...props } = Astro.props
1515
const toc = generateToc(headings)
1616
---
1717

18-
<toc-heading class={className} {...props}>
19-
<h2 class='font-semibold'>TABLE OF CONTENTS</h2>
20-
<ul class='mt-4 text-card-foreground'>
21-
{toc.map((heading) => <TOCHeading heading={heading} />)}
22-
</ul>
23-
</toc-heading>
18+
{toc.length > 0 && (
19+
<toc-heading class={className} {...props}>
20+
<h2 class='font-semibold'>TABLE OF CONTENTS</h2>
21+
<ul class='mt-4 text-card-foreground'>
22+
{toc.map((heading) => <TOCHeading heading={heading} />)}
23+
</ul>
24+
</toc-heading>
25+
)}
2426

2527
<script>
2628
// This script tag is useful only if you want to display the TOC alongside the blog post...
3.89 MB
Loading

src/content/blog/coding/bloom_filter.mdx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ tags:
77
- algorithm
88
- bloom-filter
99
- probability
10-
- ai-generate
1110
language: '中文'
11+
ai-model:
12+
- gemini-2.5-pro
13+
heroImage:
14+
src: ./attachments/bloom_filter.png
1215
---
16+
17+
18+
19+

src/layouts/BlogPost.astro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const primaryColor = data.heroImage?.color ?? 'hsl(var(--primary) / var(--un-tex
5353
meta={{ articleDate, description, ogImage: socialImage, title }}
5454
highlightColor={primaryColor}
5555
back='/blog'
56+
hasToc={!!headings.length}
5657
>
5758
{/* 大屏幕:手风琴目录专用 slot,支持贴边布局 (仅在大屏幕显示) */}
5859
{!!headings.length && headings.length > 4 && (

src/layouts/CommonPage.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface Props {
1818
const { title, headings, info, ...props } = Astro.props
1919
---
2020

21-
<PageLayout meta={{ title }} {...props}>
21+
<PageLayout meta={{ title }} {...props} hasToc={!!headings?.length}>
2222
{headings?.length && <TOC headings={headings} slot='sidebar' />}
2323

2424
<Fragment slot='header'>

src/layouts/ContentLayout.astro

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ interface Props {
1414
meta: SiteMeta
1515
highlightColor?: string
1616
back?: string
17+
hasToc?: boolean
1718
}
1819
19-
const { meta, highlightColor, back = '/', ...props } = Astro.props
20+
const { meta, highlightColor, back = '/', hasToc = false, ...props } = Astro.props
2021
---
2122

2223
<PageLayout {meta} {highlightColor} {...props}>
@@ -76,14 +77,16 @@ const { meta, highlightColor, back = '/', ...props } = Astro.props
7677
</div>
7778

7879
<BackToTop header='content-header' content='content'>
79-
<button
80-
slot='other-icons'
81-
aria-label='Toggle sidebar'
82-
class='size-10 flex items-center justify-center rounded-full border-2 border-transparent bg-muted text-muted-foreground duration-300 hover:border-border/75 md:hidden sm:size-12'
83-
id='sidebar-btn'
84-
>
85-
<Icon name='list' />
86-
</button>
80+
{hasToc && (
81+
<button
82+
slot='other-icons'
83+
aria-label='Toggle sidebar'
84+
class='size-10 flex items-center justify-center rounded-full border-2 border-transparent bg-muted text-muted-foreground duration-300 hover:border-border/75 md:hidden sm:size-12'
85+
id='sidebar-btn'
86+
>
87+
<Icon name='list' />
88+
</button>
89+
)}
8790
</BackToTop>
8891
</PageLayout>
8992

src/pages/docs/DocsContents.astro

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,31 @@ type Props = {
3030
const { title = true, class: className, ...props } = Astro.props
3131
---
3232

33-
<docs-toc class={cn('not-prose', className)} {...props}>
34-
{title && <h2 class='text-foreground font-semibold'>DOCS</h2>}
35-
<ul class='mt-4 flex flex-col gap-y-5'>
36-
{
37-
Object.entries(docCategories).map(([id, title]: [string, string]) => (
38-
<li>
39-
<h3 class='text-muted-foreground text-xs tracking-widest uppercase'>{title}</h3>
40-
<ul class='mt-2 flex flex-col'>
41-
{docsByCate[id]
42-
.sort((a, b) => a.data.order - b.data.order)
43-
.map((doc) => (
44-
<li class='docs-item flex relative ms-2 px-3 py-1 text-foreground/75 transition-all rounded-2xl'>
45-
<a class='flex-1 hover:text-foreground' href={`/docs/${doc.id}`}>
46-
{doc.data.title}
47-
</a>
48-
</li>
49-
))}
50-
</ul>
51-
</li>
52-
))
53-
}
54-
</ul>
55-
</docs-toc>
33+
{docsCollection.length > 0 && (
34+
<docs-toc class={cn('not-prose', className)} {...props}>
35+
{title && <h2 class='text-foreground font-semibold'>DOCS</h2>}
36+
<ul class='mt-4 flex flex-col gap-y-5'>
37+
{
38+
Object.entries(docCategories).map(([id, title]: [string, string]) => (
39+
<li>
40+
<h3 class='text-muted-foreground text-xs tracking-widest uppercase'>{title}</h3>
41+
<ul class='mt-2 flex flex-col'>
42+
{docsByCate[id]
43+
.sort((a, b) => a.data.order - b.data.order)
44+
.map((doc) => (
45+
<li class='docs-item flex relative ms-2 px-3 py-1 text-foreground/75 transition-all rounded-2xl'>
46+
<a class='flex-1 hover:text-foreground' href={`/docs/${doc.id}`}>
47+
{doc.data.title}
48+
</a>
49+
</li>
50+
))}
51+
</ul>
52+
</li>
53+
))
54+
}
55+
</ul>
56+
</docs-toc>
57+
)}
5658

5759
<style>
5860
docs-toc .docs-item::before {

0 commit comments

Comments
 (0)