| 1 |
--- |
| 2 |
interface Heading { |
| 3 |
depth: number; |
| 4 |
slug: string; |
| 5 |
text: string; |
| 6 |
} |
| 7 |
|
| 8 |
interface Props { |
| 9 |
headings: (Heading & { label?: boolean })[]; |
| 10 |
} |
| 11 |
|
| 12 |
const {headings} = Astro.props; |
| 13 |
|
| 14 |
const items = headings.filter(h => h.depth >= 2 && h.depth <= 4); |
| 15 |
const minDepth = items.length > 0 ? Math.min(...items.map(h => h.depth)) : 2; |
| 16 |
--- |
| 17 |
|
| 18 |
{items.length > 0 && ( |
| 19 |
<nav id="page-outline" |
| 20 |
aria-label="Table of Contents" |
| 21 |
class="w-full" |
| 22 |
_="install Scrollspy"> |
| 23 |
<div class="w-full border-l border-neutral-200 dark:border-neutral-800"> |
| 24 |
{items.map((heading) => { |
| 25 |
const depth = heading.depth - minDepth; |
| 26 |
const indentClass = depth === 0 ? 'pl-4' : depth === 1 ? 'pl-8' : 'pl-12'; |
| 27 |
const isLabel = heading.label; |
| 28 |
|
| 29 |
return isLabel ? ( |
| 30 |
<span class:list={[ |
| 31 |
'block py-2 pr-2 -ml-px border-l-2 border-transparent', |
| 32 |
indentClass, |
| 33 |
'font-sans text-[0.625rem] tracking-widest uppercase text-neutral-600 dark:text-neutral-400', |
| 34 |
]}> |
| 35 |
{heading.text} |
| 36 |
</span> |
| 37 |
) : ( |
| 38 |
<a href={`#${heading.slug}`} |
| 39 |
class:list={[ |
| 40 |
'block py-2 pr-2 text-sm truncate -ml-px border-l-2', |
| 41 |
indentClass, |
| 42 |
'border-transparent text-neutral-600 interact:text-neutral-900 interact:border-neutral-300', |
| 43 |
'dark:text-neutral-400 dark:interact:text-neutral-200 dark:interact:border-neutral-700', |
| 44 |
'aria-[current]:border-neutral-900 aria-[current]:text-neutral-900 aria-[current]:font-semibold', |
| 45 |
'dark:aria-[current]:border-neutral-100 dark:aria-[current]:text-neutral-100', |
| 46 |
]}> |
| 47 |
{heading.text} |
| 48 |
</a> |
| 49 |
); |
| 50 |
})} |
| 51 |
</div> |
| 52 |
</nav> |
| 53 |
)} |