hugo-theme-relearn/layouts/partials/toc-id.html

46 lines
2 KiB
HTML
Raw Normal View History

{{- if or (eq .Kind "taxonomy") (eq .Kind "term") }}
2023-10-30 18:11:39 +00:00
{{- $lastCapital := "" }}
{{- $pages := slice }}
{{- if eq .Kind "taxonomy" }}
{{- range .Data.Terms }}
{{- $len := 0 }}
{{- range .Pages }}
{{- $c:=""}}{{/* count pages of term */}}
{{- if and .Title .RelPermalink (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableTagHiddenPages true) ) }}
{{- $len = add $len 1 }}
{{- end }}
2023-10-30 18:11:39 +00:00
{{- end }}
{{- if $len }}
{{- $pages = $pages| append (dict "Title" (default .Page.Data.Term .Page.Title) "Page" . "Len" $len )}}
2023-10-30 18:11:39 +00:00
{{- end }}
{{- end }}
{{- else if eq .Kind "term" }}
{{- range .Pages }}
{{- if and .Title .RelPermalink (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableTagHiddenPages true) ) }}
{{- $pages = $pages | append (dict "Title" .Title "Page" . )}}
{{- end }}
2023-10-30 18:11:39 +00:00
{{- end }}
{{- end }}
{{- $toc_pages := "" }}
2023-10-30 18:11:39 +00:00
{{- range sort $pages ".Title" }}
{{- $capital := substr .Title 0 1 | upper }}
{{- if ne $lastCapital $capital }}
{{- $toc_pages = printf "%s <li><a href=\"#%s\">%s</a></li>\n" $toc_pages ($capital | plainify | anchorize) $capital }}
2023-10-30 18:11:39 +00:00
{{- end }}
{{- $lastCapital = $capital }}
{{- end }}
{{- $toc := trim (partial "toc.html" .) " \n\r\t" }}
{{- if and (not $toc) $toc_pages }}
{{- $toc = printf "<nav id=\"TableOfContents\"></nav>" }}
{{- end }}
{{- if and $toc $toc_pages }}
{{- $toc = replaceRE "^(<nav id=\"TableOfContents\">)[\\s]*" "${1}\n <ul>\n" $toc }}
{{- $toc = replaceRE "^(<nav id=\"TableOfContents\">[\\s]*<ul>)[\\s]*<ul>[\\n\\r]*" "${1}\n" $toc }}
{{- $toc = replaceRE "[\\s]*(</nav>)$" "\n </ul>\n${1}" $toc }}
{{- $toc = replaceRE "[ \\t]*</ul>[\\s]*(</ul>[\\s]*</nav>)$" " ${1}" $toc }}
{{- $toc = replaceRE "([ \\t]*</ul>[\\s]*</nav>)$" (printf "%s${1}" $toc_pages) $toc }}
{{- end }}
{{ $toc | safeHTML }}
2023-10-30 18:11:39 +00:00
{{- else }}
{{- partial "toc.html" . }}
{{- end }}