Files
pages/src/lib/table-of-contents.svelte

134 lines
3.3 KiB
Svelte
Raw Normal View History

2025-04-01 22:25:10 +02:00
<script lang="ts">
import {onMount} from 'svelte';
2025-04-01 22:25:10 +02:00
let {
disableStickyScrolling,
}: {
disableStickyScrolling?: boolean;
} = $props();
let idCounter: number = 0;
2025-12-05 17:07:45 +00:00
let root: HTMLElement;
let container: HTMLElement;
onMount(() => {
let headers = getHeaders();
headers.forEach(header => {
let headerId = getHeaderId(header);
var element = document.createElement("a");
element.classList += "toc-level-" + getHeaderLevel(header);
element.href = `#${headerId}`;
element.innerHTML = `${(header as HTMLElement).innerHTML}`;
container.appendChild(element);
});
2025-12-05 17:07:45 +00:00
// Hide table of contents if no valid entries have been found
if (headers.length == 0) {
root.style.display = "none";
}
});
2025-04-01 22:25:10 +02:00
let getHeaders = function(): NodeList {
return document.querySelectorAll("h2, h3, h4, h5");
2025-04-01 22:25:10 +02:00
}
let getHeaderId = function(header: Node): string {
var id = (header as HTMLElement).id;
if (!id) {
id = `header-${idCounter}`;
(header as HTMLElement).id = id;
idCounter += 1;
}
return id;
}
let getHeaderLevel = function(header: Node): string {
switch ((header as HTMLElement).tagName) {
case "H2":
return "0";
case "H3":
return "1";
case "H4":
return "2";
case "H5":
return "3";
default:
return "0";
}
}
</script>
{#if disableStickyScrolling}
2025-12-05 17:07:45 +00:00
<div class="toc-container notched" bind:this={root}>
2025-08-18 19:44:53 +02:00
{@render tocList()}
2025-04-01 22:25:10 +02:00
</div>
{:else}
2025-12-05 17:07:45 +00:00
<div class="toc-container notched sticky-toc" bind:this={root}>
2025-08-18 19:44:53 +02:00
{@render tocList()}
</div>
{/if}
2025-04-01 22:25:10 +02:00
2025-08-18 19:44:53 +02:00
{#snippet tocList()}
<ul class="toc-list" bind:this={container}></ul>
{/snippet}
2025-04-01 22:25:10 +02:00
<style>
:global {
.toc-container {
width: 80%;
margin-left: auto;
margin-right: auto;
2025-04-01 22:25:10 +02:00
background-color: var(--color-background-highlight);
padding: 16px 0;
}
.sticky-toc {
2025-07-14 12:12:30 +02:00
position: sticky;
top: 20px;
}
2025-04-01 22:25:10 +02:00
.toc-list {
padding: 0;
margin: 0;
}
/* .toc-list li {
list-style: none;
} */
.toc-list a {
width: 100%;
2025-08-25 11:44:01 +02:00
padding-top: 3px;
padding-bottom: 3px;
padding-right: 24px;
display: inline-block;
color: var(--color-text);
text-decoration: none;
transition: all 0.2s ease-in-out;
transition-property: color background-color;
box-sizing: border-box;
}
2025-04-01 22:25:10 +02:00
.toc-list a:hover {
color: var(--color-text-dark);
background-color: var(--color-highlight);
}
2025-04-01 22:25:10 +02:00
.toc-level-0 {
font-weight: 800;
padding-left: 44px;
}
.toc-level-1 {
padding-left: 68px;
}
.toc-level-2 {
padding-left: 92px;
}
.toc-level-3 {
padding-left: 116px;
}
.toc-level-1::before, .toc-level-2::before, .toc-level-3::before {
content: "↳ ";
}
2025-04-01 22:25:10 +02:00
}
</style>