finally fixed the fucking table of contents

This commit is contained in:
2025-04-03 22:33:47 +02:00
parent 60901a398e
commit b99e185446
5 changed files with 78 additions and 61 deletions

View File

@@ -1,16 +1,23 @@
<script lang="ts"> <script lang="ts">
let { import {onMount} from 'svelte';
document,
querySelector = "h2, h3, h4, h5",
}: {
document: Document;
querySelector?: string;
} = $props();
var idCounter: number = 0; let idCounter: number = 0;
let container: HTMLElement;
onMount(() => {
let headers = getHeaders();
headers.forEach(header => {
let headerId = getHeaderId(header);
var element = document.createElement("li");
element.classList += "toc-level-" + getHeaderLevel(header);
element.innerHTML = `<a href="#${headerId}">${(header as HTMLElement).innerHTML}</a>`;
container.appendChild(element);
});
});
let getHeaders = function(): NodeList { let getHeaders = function(): NodeList {
return document.querySelectorAll(querySelector); return document.querySelectorAll("h2, h3, h4, h5");
} }
let getHeaderId = function(header: Node): string { let getHeaderId = function(header: Node): string {
@@ -40,61 +47,63 @@
</script> </script>
<div class="toc-container"> <div class="toc-container">
<ul class="toc-list"> <ul class="toc-list" bind:this={container}>
{#each getHeaders() as header} <!-- {#each getHeaders() as header}
<li class="toc-level-{getHeaderLevel(header)}"><a href="#{getHeaderId(header)}">{(header as HTMLElement).innerText}</a></li> <li class="toc-level-{getHeaderLevel(header)}"><a href="#{getHeaderId(header)}">{(header as HTMLElement).innerText}</a></li>
{/each} {/each} -->
</ul> </ul>
</div> </div>
<style> <style>
.toc-container { :global {
width: 80%; .toc-container {
margin-left: auto; width: 80%;
margin-right: auto; margin-left: auto;
margin-right: auto;
background-color: var(--color-background-highlight); background-color: var(--color-background-highlight);
padding: 16px 0; padding: 16px 0;
} }
.toc-list { .toc-list {
padding: 0; padding: 0;
margin: 0; margin: 0;
} }
.toc-list li { .toc-list li {
list-style: none; list-style: none;
} }
.toc-list li a { .toc-list li a {
width: 100%; width: 100%;
padding-top: 8px; padding-top: 8px;
padding-bottom: 8px; padding-bottom: 8px;
padding-right: 24px; padding-right: 24px;
display: inline-block; display: inline-block;
color: var(--color-text); color: var(--color-text);
text-decoration: none; text-decoration: none;
transition: all 0.2s ease-in-out; transition: all 0.2s ease-in-out;
transition-property: color background-color; transition-property: color background-color;
box-sizing: border-box; box-sizing: border-box;
} }
.toc-list li a:hover { .toc-list li a:hover {
color: var(--color-text-dark); color: var(--color-text-dark);
background-color: var(--color-highlight); background-color: var(--color-highlight);
} }
.toc-level-0 a { .toc-level-0 a {
padding-left: 44px; padding-left: 44px;
} }
.toc-level-1 a { .toc-level-1 a {
padding-left: 68px; padding-left: 68px;
} }
.toc-level-2 a { .toc-level-2 a {
padding-left: 92px; padding-left: 92px;
} }
.toc-level-3 a { .toc-level-3 a {
padding-left: 116px; padding-left: 116px;
} }
.toc-level-1 a::before, .toc-level-2 a::before, .toc-level-3 a::before { .toc-level-1 a::before, .toc-level-2 a::before, .toc-level-3 a::before {
content: "↳ "; content: "↳ ";
}
} }
</style> </style>

View File

@@ -2,6 +2,8 @@
import Header from "$lib/header.svelte"; import Header from "$lib/header.svelte";
import Footer from "$lib/footer.svelte"; import Footer from "$lib/footer.svelte";
export const prerender = true;
let { children } = $props(); let { children } = $props();
</script> </script>
@@ -54,7 +56,7 @@
height: 100%; height: 100%;
width: 100%; width: 100%;
background-color: var(--color-waters); background-color: var(--color-waters);
mask-image: url('bremen-waters-white.svg'); mask-image: url('/bremen-waters-white.svg');
mask-position: center; mask-position: center;
background-position: center; background-position: center;
background-attachment: fixed; background-attachment: fixed;

View File

@@ -21,7 +21,7 @@
<p>This is a listing of some of my more noteworthy projects that can be found on the web.</p> <p>This is a listing of some of my more noteworthy projects that can be found on the web.</p>
{#if browser} {#if browser}
<TableOfContents {document}/> <TableOfContents />
{/if} {/if}
<h2>Active Projects</h2> <h2>Active Projects</h2>

View File

@@ -6,7 +6,7 @@
<BannerTitle title="Project N5 Progress Update: 2025-03-16" subtitle="Refactoring" banner="/projects/projectn5/devlog/20250316/fishmonger.webp" /> <BannerTitle title="Project N5 Progress Update: 2025-03-16" subtitle="Refactoring" banner="/projects/projectn5/devlog/20250316/fishmonger.webp" />
<TableOfContents document={document}/> <TableOfContents />
<p>I've been making a lot of progress in a lot of different areas, so I won't be able to elaborate on every little detail, but I'll focus on more major things. Excited to share what I've been working on!</p> <p>I've been making a lot of progress in a lot of different areas, so I won't be able to elaborate on every little detail, but I'll focus on more major things. Excited to share what I've been working on!</p>

View File

@@ -3,9 +3,15 @@ import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
/** @type {import('@sveltejs/kit').Config} */ /** @type {import('@sveltejs/kit').Config} */
const config = { const config = {
preprocess: vitePreprocess(), // preprocess: vitePreprocess(),
kit: { kit: {
adapter: adapter(), adapter: adapter({
pages: 'build',
assets: 'build',
fallback: undefined,
preprocess: false,
strict: true,
}),
}, },
}; };