From 456db3851b3719771114f0f4b89ddbf1591a0624 Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Fri, 27 Sep 2024 17:06:21 +0800 Subject: [PATCH] :truck: Feat: supersede `markup.tableOfContents` settings with `params.page.toc` --- hugo.toml | 11 +++++------ layouts/_default/_markup/render-heading.html | 5 +++-- layouts/_default/baseof.html | 3 ++- layouts/partials/init/patch.html | 2 +- layouts/posts/single.html | 9 +++++---- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/hugo.toml b/hugo.toml index af580825f..177d10251 100644 --- a/hugo.toml +++ b/hugo.toml @@ -189,11 +189,6 @@ enableEmoji = true # whether to use HTML tags directly in the document unsafe = true xhtml = false - # Table Of Contents settings - [markup.tableOfContents] - ordered = false - startLevel = 2 - endLevel = 6 # ------------------------------------------------------------------------------------- # Sitemap Configuration @@ -913,6 +908,10 @@ enableEmoji = true auto = true # FixIt 0.2.13 | NEW position of TOC ["left", "right"] position = "right" + # FixIt 0.3.12 | NEW supersede `markup.tableOfContents` settings + ordered = false + startLevel = 2 + endLevel = 6 # FixIt 0.2.13 | NEW Display a message at the beginning of an article to warn the reader that its content might be expired [params.page.expirationReminder] enable = false @@ -926,7 +925,7 @@ enableEmoji = true [params.page.heading] # FixIt 0.3.3 | NEW whether to capitalize automatic text of headings capitalize = false - # used with `markup.tableOfContents.ordered` parameter + # FixIt 0.3.12 | CHANGED must set `params.page.toc.ordered` to true [params.page.heading.number] # whether to enable auto heading numbering enable = false diff --git a/layouts/_default/_markup/render-heading.html b/layouts/_default/_markup/render-heading.html index 33abeef2b..fa3375171 100644 --- a/layouts/_default/_markup/render-heading.html +++ b/layouts/_default/_markup/render-heading.html @@ -1,5 +1,6 @@ {{- /* Read the config and format */ -}} {{- $params := .Page.Params | merge site.Params.page -}} +{{- $ordered := $params.toc.ordered | and -}} {{- $h1Format := $params.heading.number.format.h1 | default "{title}" -}} {{- $h2Format := $params.heading.number.format.h2 | default "{h2} {title}" -}} {{- $h3Format := $params.heading.number.format.h3 | default "{h2}.{h3} {title}" -}} @@ -7,7 +8,7 @@ {{- $h5Format := $params.heading.number.format.h5 | default "{h2}.{h3}.{h4}.{h5} {title}" -}} {{- $h6Format := $params.heading.number.format.h6 | default "{h2}.{h3}.{h4}.{h5}.{h6} {title}" -}} -{{- if $params.heading.number.enable -}} +{{- if $ordered -}} {{- /* Set the count for child level to 0 */ -}} {{- .Page.Scratch.SetInMap "heading-counter" (string (add .Level 1)) 0 -}} {{- end -}} @@ -16,7 +17,7 @@ {{- $title := .Text -}} {{- /* Only enable in main section pages */ -}} {{- $onlyMainSection := cond $params.heading.number.onlyMainSection (eq .Page.Type "posts") true -}} - {{- if $params.heading.number.enable | and $onlyMainSection -}} + {{- if $ordered | and $onlyMainSection -}} {{- /* Add 1 to the current level */ -}} {{- $headingMap := .Page.Scratch.Get "heading-counter" -}} {{- $count := (string .Level) | index $headingMap | int | add 1 -}} diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 4024a04ee..dc176499a 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -15,7 +15,8 @@ {{- partial "head/link.html" . -}} {{- partial "head/seo.html" . -}} {{- partial "head/script.html" . -}} - {{- /* TODO preload script https://developer.mozilla.org/zh-CN/docs/Web/HTML/Attributes/rel/preload */ -}} + {{- /* TODO preload script https://developer.mozilla.org/zh-CN/docs/Web/HTML/Attributes/rel/preload */ -}} + {{- /* TODO add config and page config to config.js (static) */ -}} {{- /* Custom head */ -}} {{- block "custom-head" . }}{{ end -}} diff --git a/layouts/partials/init/patch.html b/layouts/partials/init/patch.html index cd3026353..9a9874566 100644 --- a/layouts/partials/init/patch.html +++ b/layouts/partials/init/patch.html @@ -6,7 +6,7 @@ {{- if eq $toc true -}} {{- $toc = dict "enable" true | merge .Site.Params.page.toc -}} {{- else if eq $toc false -}} - {{- $toc = dict "enable" false -}} + {{- $toc = dict "enable" false | merge .Site.Params.page.toc -}} {{- end -}} {{- .Scratch.Set "toc" $toc -}} diff --git a/layouts/posts/single.html b/layouts/posts/single.html index 106b15bb0..d75989b72 100644 --- a/layouts/posts/single.html +++ b/layouts/posts/single.html @@ -7,7 +7,7 @@ {{- $title := title .Title -}} {{- $params := partial "function/params.html" -}} {{- $toc := .Scratch.Get "toc" -}} - {{- $tocEmpty := eq .TableOfContents `` -}} + {{- $showToc := $toc.enable | and (gt (len .Fragments.Headings) 0) -}}