Update generateToc to accept container selector

Change generateToc signature to take a separate tocContainerSelector and use it when removing the container if no headings are found. Also update the post.html call site to pass the new third argument. This ensures the correct DOM element is targeted for removal when the content has no h1–h4 headings.
This commit is contained in:
Ryan Wang
2026-03-28 11:59:42 +08:00
parent fdc156f2a7
commit 1f89821456
2 changed files with 3 additions and 3 deletions
+2 -2
View File
@@ -1,10 +1,10 @@
import tocbot from "tocbot";
export function generateToc(contentId: string, tocSelector: string) {
export function generateToc(contentId: string, tocSelector: string, tocContainerSelector: string) {
const content = document.getElementById(contentId);
const titles = content?.querySelectorAll("h1, h2, h3, h4");
if (!titles || titles.length === 0) {
const tocContainer = document.querySelector(tocSelector);
const tocContainer = document.querySelector(tocContainerSelector);
tocContainer?.remove();
return;
}
+1 -1
View File
@@ -4,7 +4,7 @@
<script type="module" src="./assets/post.ts"></script>
<script type="module">
import { generateToc } from "./assets/utils/toc.ts";
generateToc("content", ".toc-container");
generateToc("content", ".toc", ".toc-container");
</script>
</template>
<template name="hero">