diff --git a/src/routes/posts/[slug]/+page.svelte b/src/routes/posts/[slug]/+page.svelte index 6a04c5d..f917fc4 100644 --- a/src/routes/posts/[slug]/+page.svelte +++ b/src/routes/posts/[slug]/+page.svelte @@ -2,7 +2,7 @@ import { page } from '$app/stores'; import { resolve } from '$app/paths'; import type { PageData } from './$types'; - import { onMount, tick } from 'svelte'; + import { tick } from 'svelte'; import tocbot from 'tocbot'; import MarkdownRenderer from '$lib/components/MarkdownRenderer.svelte'; @@ -45,7 +45,10 @@ } } - onMount(() => { + $effect(() => { + // track data.post.id to re-initialize TOC when navigating between posts + if (!data.post.id) return; + const headers = document.querySelectorAll( '.js-toc-content h1, .js-toc-content h2, .js-toc-content h3, .js-toc-content h4, .js-toc-content h5, .js-toc-content h6' ); @@ -72,28 +75,32 @@ sidebarShow = false; } }); - } - - $effect(() => { - if (hasToc && sidebarShow) { - document.body.classList.add('sidebar-show'); - } else { - document.body.classList.remove('sidebar-show'); - } - - return () => { - document.body.classList.remove('sidebar-show'); - }; - }); - - if (seriesContentEl && isSeriesOpen) { - seriesContentEl.style.maxHeight = 'none'; + } else { + hasToc = false; } return () => { - if (hasToc) tocbot.destroy(); + tocbot.destroy(); }; }); + + $effect(() => { + if (hasToc && sidebarShow) { + document.body.classList.add('sidebar-show'); + } else { + document.body.classList.remove('sidebar-show'); + } + + return () => { + document.body.classList.remove('sidebar-show'); + }; + }); + + $effect(() => { + if (seriesContentEl && isSeriesOpen) { + seriesContentEl.style.maxHeight = 'none'; + } + });