barrettruth.com/src/components/TableOfContents.astro
2025-11-10 23:21:21 -05:00

78 lines
1.3 KiB
Text

---
// Auto-generated TOC from MDX headings - Left sidebar
interface Props {
headings: Array<{
depth: number;
slug: string;
text: string;
}>;
}
const { headings } = Astro.props;
// Filter to only show h1 and h2
const tocHeadings = headings.filter((h) => h.depth <= 2);
---
{
tocHeadings.length > 0 && (
<aside class="toc-sidebar">
<div class="toc-title">contents</div>
<nav class="toc-nav">
{tocHeadings.map((heading) => (
<a
href={`#${heading.slug}`}
class={heading.depth === 2 ? "sub" : "main"}
>
{heading.text}
</a>
))}
</nav>
</aside>
)
}
<style>
.toc-sidebar {
width: 100%;
}
.toc-title {
font-weight: normal;
margin-bottom: 1rem;
text-align: left;
font-size: 1.5em;
}
.toc-nav {
display: flex;
flex-direction: column;
gap: 0.3rem;
align-items: flex-start;
}
.toc-nav a {
color: var(--text);
text-decoration: none;
line-height: 1.4;
text-align: left;
}
.toc-nav a:hover {
text-decoration: underline;
}
.toc-nav a.main {
padding-left: 0;
}
.toc-nav a.sub {
padding-left: 1.5rem;
}
@media (max-width: 1200px) {
.toc-sidebar {
display: none;
}
}
</style>