Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/components/TableOfContents.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
interface Props {
headings: Array<{ depth: number; slug: string; text: string }>
label: string
title?: string
}

const { headings, label } = Astro.props
const { headings, label, title } = Astro.props

const tocHeadings = headings.filter((h) => h.depth === 2 || h.depth === 3)
---
Expand All @@ -23,6 +24,17 @@ const tocHeadings = headings.filter((h) => h.depth === 2 || h.depth === 3)
style="top: 0; left: -9px;"
/>
<ul id="toc-list" class="space-y-1 text-sm border-l border-transparent">
{title && (
<li data-depth={1}>
<a
href="#title"
data-slug="title"
class="toc-link block py-1 transition-colors duration-200 text-foreground dark:text-foreground-dark font-medium"
>
{title}
</a>
</li>
)}
{tocHeadings.map((h) => (
<li data-depth={h.depth}>
<a
Expand Down Expand Up @@ -70,7 +82,7 @@ const tocHeadings = headings.filter((h) => h.depth === 2 || h.depth === 3)
if (top === undefined) return

const depth = depths.get(slug) || 2
const indent = depth === 3 ? 12 : 0
const indent = depth >= 3 ? 12 : 0

indicator.style.top = `${top}px`
indicator.style.left = `${-9 + indent}px`
Expand Down
7 changes: 5 additions & 2 deletions src/pages/articles/[slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ const onThisPageLabel = t(labels, 'article.onThisPage')
{article.data.category}
</a>
</div>
<h1 class="text-3xl font-bold tracking-tight text-foreground dark:text-foreground-dark mb-4">
<h1
id="title"
class="text-3xl font-bold tracking-tight text-foreground dark:text-foreground-dark mb-4 scroll-mt-60"
>
{article.data.title}
</h1>
<div
Expand Down Expand Up @@ -153,6 +156,6 @@ const onThisPageLabel = t(labels, 'article.onThisPage')
<ShareButtons title={article.data.title} url={articleUrl} labels={labels} />
</div>

<TableOfContents headings={tocHeadings} label={onThisPageLabel} />
<TableOfContents headings={tocHeadings} label={onThisPageLabel} title={article.data.title} />
</div>
</DocsLayout>
7 changes: 5 additions & 2 deletions src/pages/en/articles/[slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ const onThisPageLabel = t(labels, 'article.onThisPage')
{article.data.category}
</a>
</div>
<h1 class="text-3xl font-bold tracking-tight text-foreground dark:text-foreground-dark mb-4">
<h1
id="title"
class="text-3xl font-bold tracking-tight text-foreground dark:text-foreground-dark mb-4 scroll-mt-60"
>
{article.data.title}
</h1>
<div
Expand Down Expand Up @@ -152,6 +155,6 @@ const onThisPageLabel = t(labels, 'article.onThisPage')
<ShareButtons title={article.data.title} url={articleUrl} labels={labels} />
</div>

<TableOfContents headings={tocHeadings} label={onThisPageLabel} />
<TableOfContents headings={tocHeadings} label={onThisPageLabel} title={article.data.title} />
</div>
</DocsLayout>
10 changes: 10 additions & 0 deletions src/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
border-color: hsl(var(--border));
}

html {
scroll-behavior: smooth;
}

body {
background-color: hsl(var(--background));
color: hsl(var(--foreground));
Expand Down Expand Up @@ -77,3 +81,9 @@ html .astro-code span {
color: var(--shiki-light) !important;
background-color: var(--shiki-light-bg) !important;
}

.prose h2,
.prose h3,
.prose h4 {
scroll-margin-top: 5rem;
}
Loading