diff --git a/layouts/_partials/utils/extract-headings.html b/layouts/_partials/utils/extract-headings.html new file mode 100644 index 0000000..7611923 --- /dev/null +++ b/layouts/_partials/utils/extract-headings.html @@ -0,0 +1,31 @@ +{{- /* +Extracts all headings from a page and adds them to the scratchpad. + +The keys can be obtained from the scratchpad by using the "keys" key. +The titles can be obtained from the scratchpad by using the "titles" key. + +The scratchpad must be initialized with empty slices before calling this function for the keys "keys" and "titles" + +@param {any} target The element to extract headings from. +@param {any} scratch The scratchpad to add the keys and titles to. + +@example {{ partial "utils/extract-headings.html" (dict "target" $h1 "scratch" $s) }} +*/ -}} + +{{- range $heading := index .target.Headings -}} + {{- if and (eq $heading.Level 0) (not $heading.Title) -}} + {{- $.scratch.Add "keys" (slice $heading.Title) -}} + {{- else -}} + {{- $key := (printf "%s#%s" $heading.ID $heading.Title) -}} + {{- $.scratch.Add "keys" (slice $key) -}} + {{- end -}} + + {{- $title := (printf "%s" $heading.Level $heading.Title) -}} + {{- $.scratch.Add "titles" (slice $title) -}} + + {{- partial "utils/extract-headings.html" (dict + "target" $heading + "scratch" $.scratch + ) + }} +{{- end -}} diff --git a/layouts/_partials/utils/fragments.html b/layouts/_partials/utils/fragments.html index ce268b3..511a4dd 100644 --- a/layouts/_partials/utils/fragments.html +++ b/layouts/_partials/utils/fragments.html @@ -40,26 +40,15 @@ {{- $page := .context -}} {{- $type := .type | default "content" -}} -{{- /* Initialize slices to store heading data */ -}} -{{- $headingKeys := slice -}} {{- /* Keys for indexing (ID#Title or just Title) */ -}} -{{- $headingTitles := slice -}} {{- /* HTML heading tags for content splitting */ -}} +{{- /* Process all headings */ -}} +{{- $s := newScratch -}} +{{- $s.Set "keys" slice -}} +{{- $s.Set "titles" slice -}} -{{- /* Process all h1 headings and their nested h2 headings */ -}} -{{- range $h1 := $page.Fragments.Headings -}} - {{- /* Handle h1 headings - empty titles get special treatment */ -}} - {{- if eq $h1.Title "" -}} - {{- $headingKeys = $headingKeys | append $h1.Title -}} - {{- else -}} - {{- $headingKeys = $headingKeys | append (printf "%s#%s" $h1.ID $h1.Title) -}} - {{- end -}} - {{- $headingTitles = $headingTitles | append (printf "

%s" $h1.Title) -}} +{{- partial "utils/extract-headings.html" (dict "target" $page.Fragments "scratch" $s) -}} - {{- /* Process nested h2 headings */ -}} - {{- range $h2 := $h1.Headings -}} - {{- $headingKeys = $headingKeys | append (printf "%s#%s" $h2.ID $h2.Title) -}} - {{- $headingTitles = $headingTitles | append (printf "

%s" $h2.Title) -}} - {{- end -}} -{{- end -}} +{{- $headingKeys := $s.Get "keys" -}} +{{- $headingTitles := $s.Get "titles" -}} {{- $content := $page.Content | htmlUnescape -}} {{- $len := len $headingKeys -}} @@ -80,7 +69,7 @@ {{ $data = $data | merge (dict $headingKey ($content | plainify | htmlUnescape | strings.TrimSpace)) }} {{ else }} {{ $parts := split $content (printf "%s" $headingTitle) }} - {{ $lastPart := index $parts (sub (len $parts) 1) | strings.TrimSpace }} + {{ $lastPart := index $parts (sub (len $parts) 1) }} {{ $data = $data | merge (dict $headingKey ($lastPart | plainify | htmlUnescape | strings.TrimSpace)) }} {{ $content = strings.TrimSuffix $lastPart $content }}