fix: the ToC shown on non-toc pages

This commit is contained in:
Junyi Hou 2024-03-06 13:24:28 +08:00
parent ada44ff67d
commit 3d600a74c3
2 changed files with 44 additions and 5 deletions

View File

@ -1,9 +1,20 @@
{{ $toc := and ($.Site.Params.toc) (ge .WordCount $.Site.Params.tocWordCount) }} {{ if isset .Params "toc" }} {{ $toc = .Params.toc }} {{ end }} {{ if $toc }}
{{ $toc := and ($.Site.Params.toc) (ge .WordCount $.Site.Params.tocWordCount) }}
{{ if isset .Params "toc" }}
{{ $toc = .Params.toc }}
{{ end }}
{{ if $toc }}
{{ $tocContent := .TableOfContents }}
{{ if findRE "<li>" $tocContent }}
<!-- we show the toc only if we have element in .TableOfContents -->
<details class="toc noselect">
<summary>Table of Contents</summary>
<div class="inner">{{ .TableOfContents }}</div>
</details>
{{ end }}
{{ end }}
<script>
document.querySelector(".toc").addEventListener("click", function () {

View File

@ -0,0 +1,28 @@
diff a/layouts/partials/toc.html b/layouts/partials/toc.html (rejected hunks)
@@ -1,10 +1,21 @@
-{{ $toc := and ($.Site.Params.toc) (ge .WordCount $.Site.Params.tocWordCount) }} {{ if isset .Params "toc" }} {{ $toc = .Params.toc }} {{ end }} {{ if $toc }}
-<details class="toc noselect">
- <summary>Table of Contents</summary>
- <div class="inner">{{ .TableOfContents }}</div>
-</details>
+{{ $toc := and ($.Site.Params.toc) (ge .WordCount $.Site.Params.tocWordCount) }}
+{{ if isset .Params "toc" }}
+ {{ $toc = .Params.toc }}
+{{ end }}
+
+
+{{ if $toc }}
+ {{ $tocContent := .TableOfContents }}
+ {{ if findRE "<li>" $tocContent }}
+ <!-- we show the toc only if we have element in .TableOfContents -->
+ <details class="toc noselect">
+ <summary>Table of Contents</summary>
+ <div class="inner">{{ .TableOfContents }}</div>
+ </details>
+ {{ end }}
{{ end }}
+
<script>
document.querySelector(".toc").addEventListener("click", function () {
if (event.target.tagName !== "A") {