{{- /* Initialize. */}} {{- $partialName := "toc" }} {{- /* Verify minimum required version. */}} {{- $minHugoVersion := "0.147.0" }} {{- if lt hugo.Version $minHugoVersion }} {{- errorf "The %q partial requires Hugo v%s or later." $partialName $minHugoVersion }} {{- end }} {{- /* Determine content path for warning and error messages. */}} {{- $contentPath := "" }} {{- /* Get configuration. */}} {{- $startLevel := 2 }} {{- $endLevel := 3 }} {{- $minNumHeadings := 2 }} {{- /* startLevel := or ($.Param "toc.startLevel" | int) 2 */}} {{- /* endLevel := or ($.Param "toc.endLevel" | int) 3 */}} {{- /* minNumHeadings := or ($.Param "toc.minNumHeadings" | int) 2 */}} {{- /* Get headings. */}} {{- $headings := slice }} {{- $ids := slice }} {{- range findRE `(?is)` .Content }} {{- if not (strings.Contains . "data-notoc") }} {{- $level := substr . 2 1 | int }} {{- if and (ge $level $startLevel) (le $level $endLevel) }} {{- $text := replaceRE `(?is)(.+?)` "$1" . }} {{- $text = trim $text " " | plainify | safeHTML }} {{- $id := "" }} {{- if findRE `\s+id=` . }} {{- $id = replaceRE `(?is).+?\s+id=(?:\x22|\x27)?(.*?)(?:\x22|\x27)?[\s>].+` "$1" . }} {{- $ids = $ids | append $id }} {{- if not $id }} {{- errorf "The %q partial detected that the %q heading has an empty ID attribute. See %s" $partialName $text $contentPath }} {{- end }} {{- /* else */}} {{- /* errorf "The %q partial detected that the %q heading does not have an ID attribute. See %s" $partialName $text $contentPath */}} {{- end }} {{- $headings = $headings | append (dict "ID" $id "level" $level "Title" $text) }} {{- end }} {{- end }} {{- end }} {{- /* Check for duplicate heading IDs. */}} {{- $unique := slice }} {{- $duplicates := slice }} {{- range $ids }} {{- if in $unique . }} {{- $duplicates = $duplicates | append . }} {{- else }} {{- $unique = $unique | append . }} {{- end }} {{- end }} {{- with $duplicates }} {{- warnf "The %q partial detected duplicate heading IDs (%s) in %s" $partialName (delimit . ", ") $contentPath }} {{- end }} {{/* Table of Contents */}} {{/* TODO: toc bottom part should be able to hide */}} {{- $toc := .Params.toc | default true -}} {{- $onThisPage := (T "onThisPage") | default "On this page"}} {{- $tags := (T "tags") | default "Tags"}} {{- $editThisPage := (T "editThisPage") | default "Edit this page"}} {{- $backToTop := (T "backToTop") | default "Scroll to top" -}}