add: expandable ToC

This commit is contained in:
Junyi Hou 2024-03-04 19:15:27 +08:00
parent e319a2414e
commit 528a25fed5
4 changed files with 47 additions and 9 deletions

View File

@ -4,6 +4,7 @@
--card-color: #2e2e33;
--card-border-color: #333333;
--card-border-radius: 0.5em;
--link-color:#599ada;
--link-state-color:#ff5858;

View File

@ -5,6 +5,7 @@
--card-color: #fafafa;
--card-border-color: #eeeeee;
--card-border-radius: 0.5em;
--link-color:#1d60a3;
--link-state-color:rgb(163, 29, 29);
@ -15,7 +16,6 @@
--nav-bg-color: #fafafa;
--nav-link-color: #696969;
--nav-border-radius: 10px;
--pre-color: rgb(31, 35, 40);
--pre-bg-color: rgba(175, 184, 193, 0.2);

View File

@ -72,6 +72,33 @@ main {
outline:none;
}
/* Table of Contents */
.toc {
cursor: zoom-in;
display: block;
margin-top: 1em;
margin-bottom: 1em;
padding: 0.5em 1em; /* top & bottom, left & right */
border: 1px solid var(--card-border-color);
border-radius: var(--card-border-radius);
background: var(--card-color);
font-size: 14px;
font-weight: bold;
}
.toc.expanded {
cursor: zoom-out;
}
.toc .inner {
margin: 0px;
padding: 0px;
}
/* Headers */
h1 {
font-size: 1.35em;
@ -257,7 +284,7 @@ header nav:not(:empty){
text-align: center;
padding: 0.5em 0;
border: 1px solid var(--card-border-color);
border-radius: var(--nav-border-radius);
border-radius: var(--card-border-radius);
}
header nav a {

View File

@ -1,9 +1,19 @@
{{ $toc := and ($.Site.Params.toc) (ge .WordCount $.Site.Params.tocWordCount) }}
{{ if isset .Params "toc" }}
{{ $toc = .Params.toc }}
{{ $toc := and ($.Site.Params.toc) (ge .WordCount $.Site.Params.tocWordCount) }} {{ if isset .Params "toc" }} {{ $toc = .Params.toc }} {{ end }} {{ if $toc }}
<details class="toc">
<summary>Table of Contents</summary>
<div class="inner">{{ .TableOfContents }}</div>
</details>
{{ end }}
{{ if $toc }}
{{ .TableOfContents }}
{{ end }}
<script>
document.querySelector(".toc").addEventListener("click", function () {
event.preventDefault();
if (this.open) {
this.open = false;
this.classList.remove("expanded");
} else {
this.open = true;
this.classList.add("expanded");
}
});
</script>