Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/layouts/_partials/utils/extract-headings.html
Original file line number Diff line number Diff line change
@@ -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 "<h%d>%s" $heading.Level $heading.Title) }}
{{ $.scratch.Add "titles" (slice $title) }}

{{ partial "utils/extract-headings.html" (dict
"target" $heading
"scratch" $.scratch
)
}}
{{ end }}
20 changes: 6 additions & 14 deletions docs/layouts/_partials/utils/fragments.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,14 @@
{{ $page := .context }}
{{ $type := .type | default "content" }}

{{ $headingKeys := slice }}
{{ $headingTitles := slice }}
{{ $s := newScratch }}
{{ $s.Set "keys" slice }}
{{ $s.Set "titles" slice }}

{{ range $h1 := $page.Fragments.Headings }}
{{ 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 "<h1>%s" $h1.Title) }}
{{ partial "utils/extract-headings.html" (dict "target" $page.Fragments "scratch" $s) }}

{{ range $h2 := $h1.Headings }}
{{ $headingKeys = $headingKeys | append (printf "%s#%s" $h2.ID $h2.Title) }}
{{ $headingTitles = $headingTitles | append (printf "<h2>%s" $h2.Title) }}
{{ end }}
{{ end }}
{{ $headingKeys := $s.Get "keys" }}
{{ $headingTitles := $s.Get "titles" }}

{{ $content := $page.Content | htmlUnescape }}
{{ $len := len $headingKeys }}
Expand Down
Loading