Skip to content

Commit

Permalink
🚚 Feat: supersede markup.tableOfContents settings with `params.page…
Browse files Browse the repository at this point in the history
….toc`
  • Loading branch information
Lruihao committed Sep 27, 2024
1 parent c36d87f commit d267715
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
11 changes: 5 additions & 6 deletions hugo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions layouts/_default/_markup/render-heading.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{{- /* 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}" -}}
{{- $h4Format := $params.heading.number.format.h4 | default "{h2}.{h3}.{h4} {title}" -}}
{{- $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 -}}
Expand All @@ -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 -}}
Expand Down
3 changes: 2 additions & 1 deletion layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
</head>
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/init/patch.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}

Expand Down
9 changes: 5 additions & 4 deletions layouts/posts/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{{- $title := title .Title -}}
{{- $params := partial "function/params.html" -}}
{{- $toc := .Scratch.Get "toc" -}}
{{- $tocEmpty := eq .TableOfContents `<nav id="TableOfContents"></nav>` -}}
{{- $showToc := $toc.enable | and (gt (len .Fragments.Headings) 0) -}}

<aside class="aside-collection animate__animated animate__fadeIn animate__faster" aria-label="{{ T "collections" }}">
{{- /* Collection List */ -}}
Expand Down Expand Up @@ -146,14 +146,15 @@ <h1 class="single-title animate__animated animate__flipInX">
{{- end -}}

{{- /* Static TOC */ -}}
{{- if (ne $toc.enable false) | and (ne $tocEmpty true) -}}
{{- if $showToc -}}
{{- $tableOfContents := .Fragments.ToHTML ($toc.startLevel | int) ($toc.endLevel | int) ($toc.ordered | default false) -}}
<div class="details toc{{ with $params.password }} encrypted-hidden{{ end }}" id="toc-static" data-kept="{{ if $toc.keepStatic }}true{{ else }}false{{ end }}">
<div class="details-summary toc-title">
<span>{{ T "single.contents" }}</span>
<span>{{ dict "Class" "details-icon fa-solid fa-angle-right" | partial "plugin/icon.html" }}</span>
</div>
<div class="details-content toc-content" id="toc-content-static">
{{- dict "Content" .TableOfContents "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
{{- dict "Content" $tableOfContents "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
</div>
</div>
{{- end -}}
Expand Down Expand Up @@ -203,7 +204,7 @@ <h1 class="single-title animate__animated animate__flipInX">

<aside class="toc" id="toc-auto" aria-label="{{ T "single.contents" }}">
{{- /* Auto TOC */ -}}
{{- if (ne $toc.enable false) | and (ne $tocEmpty true) -}}
{{- if $showToc -}}
<h2 class="toc-title{{ with $params.password }} encrypted-hidden{{ end }}">
{{- T "single.contents" -}}&nbsp;
{{- dict "Class" "toc-icon fa-solid fa-angle-down fa-fw" | partial "plugin/icon.html" -}}
Expand Down

0 comments on commit d267715

Please sign in to comment.