mirror of
https://github.com/halo-dev/theme-earth.git
synced 2026-06-05 01:13:07 +08:00
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:
@@ -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
@@ -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">
|
||||
|
||||
Reference in New Issue
Block a user