Skip to content

Commit 70ca325

Browse files
authored
Merge pull request #30 from 4byte-dev/feat/article-tod-title
feat: add article's title to toc
2 parents 68a603c + ad44f0c commit 70ca325

4 files changed

Lines changed: 34 additions & 6 deletions

File tree

src/components/TableOfContents.astro

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
interface Props {
33
headings: Array<{ depth: number; slug: string; text: string }>
44
label: string
5+
title?: string
56
}
67
7-
const { headings, label } = Astro.props
8+
const { headings, label, title } = Astro.props
89
910
const tocHeadings = headings.filter((h) => h.depth === 2 || h.depth === 3)
1011
---
@@ -23,6 +24,17 @@ const tocHeadings = headings.filter((h) => h.depth === 2 || h.depth === 3)
2324
style="top: 0; left: -9px;"
2425
/>
2526
<ul id="toc-list" class="space-y-1 text-sm border-l border-transparent">
27+
{title && (
28+
<li data-depth={1}>
29+
<a
30+
href="#title"
31+
data-slug="title"
32+
class="toc-link block py-1 transition-colors duration-200 text-foreground dark:text-foreground-dark font-medium"
33+
>
34+
{title}
35+
</a>
36+
</li>
37+
)}
2638
{tocHeadings.map((h) => (
2739
<li data-depth={h.depth}>
2840
<a
@@ -70,7 +82,7 @@ const tocHeadings = headings.filter((h) => h.depth === 2 || h.depth === 3)
7082
if (top === undefined) return
7183

7284
const depth = depths.get(slug) || 2
73-
const indent = depth === 3 ? 12 : 0
85+
const indent = depth >= 3 ? 12 : 0
7486

7587
indicator.style.top = `${top}px`
7688
indicator.style.left = `${-9 + indent}px`

src/pages/articles/[slug].astro

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ const onThisPageLabel = t(labels, 'article.onThisPage')
118118
{article.data.category}
119119
</a>
120120
</div>
121-
<h1 class="text-3xl font-bold tracking-tight text-foreground dark:text-foreground-dark mb-4">
121+
<h1
122+
id="title"
123+
class="text-3xl font-bold tracking-tight text-foreground dark:text-foreground-dark mb-4 scroll-mt-60"
124+
>
122125
{article.data.title}
123126
</h1>
124127
<div
@@ -153,6 +156,6 @@ const onThisPageLabel = t(labels, 'article.onThisPage')
153156
<ShareButtons title={article.data.title} url={articleUrl} labels={labels} />
154157
</div>
155158

156-
<TableOfContents headings={tocHeadings} label={onThisPageLabel} />
159+
<TableOfContents headings={tocHeadings} label={onThisPageLabel} title={article.data.title} />
157160
</div>
158161
</DocsLayout>

src/pages/en/articles/[slug].astro

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ const onThisPageLabel = t(labels, 'article.onThisPage')
117117
{article.data.category}
118118
</a>
119119
</div>
120-
<h1 class="text-3xl font-bold tracking-tight text-foreground dark:text-foreground-dark mb-4">
120+
<h1
121+
id="title"
122+
class="text-3xl font-bold tracking-tight text-foreground dark:text-foreground-dark mb-4 scroll-mt-60"
123+
>
121124
{article.data.title}
122125
</h1>
123126
<div
@@ -152,6 +155,6 @@ const onThisPageLabel = t(labels, 'article.onThisPage')
152155
<ShareButtons title={article.data.title} url={articleUrl} labels={labels} />
153156
</div>
154157

155-
<TableOfContents headings={tocHeadings} label={onThisPageLabel} />
158+
<TableOfContents headings={tocHeadings} label={onThisPageLabel} title={article.data.title} />
156159
</div>
157160
</DocsLayout>

src/styles/global.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
border-color: hsl(var(--border));
33
}
44

5+
html {
6+
scroll-behavior: smooth;
7+
}
8+
59
body {
610
background-color: hsl(var(--background));
711
color: hsl(var(--foreground));
@@ -77,3 +81,9 @@ html .astro-code span {
7781
color: var(--shiki-light) !important;
7882
background-color: var(--shiki-light-bg) !important;
7983
}
84+
85+
.prose h2,
86+
.prose h3,
87+
.prose h4 {
88+
scroll-margin-top: 5rem;
89+
}

0 commit comments

Comments
 (0)