File tree Expand file tree Collapse file tree 2 files changed +37
-14
lines changed
docs/layouts/_partials/utils Expand file tree Collapse file tree 2 files changed +37
-14
lines changed Original file line number Diff line number Diff line change
1
+ {{/*
2
+ Extracts all headings from a page and adds them to the scratchpad.
3
+
4
+ The keys can be obtained from the scratchpad by using the "keys" key.
5
+ The titles can be obtained from the scratchpad by using the "titles" key.
6
+
7
+ The scratchpad must be initialized with empty slices before calling this function for the keys "keys" and "titles"
8
+
9
+ @param {any} target The element to extract headings from.
10
+ @param {any} scratch The scratchpad to add the keys and titles to.
11
+
12
+ @example {{ partial "utils/extract-headings.html" (dict "target" $h1 "scratch" $s) }}
13
+ */}}
14
+
15
+ {{ range $heading := index .target.Headings }}
16
+ {{ if and (eq $heading.Level 0) (not $heading.Title) }}
17
+ {{ $.scratch.Add "keys" (slice $heading.Title) }}
18
+ {{ else }}
19
+ {{ $key := (printf "%s#%s" $heading.ID $heading.Title) }}
20
+ {{ $.scratch.Add "keys" (slice $key) }}
21
+ {{ end }}
22
+
23
+ {{ $title := (printf "< h %d > %s" $heading.Level $heading.Title) }}
24
+ {{ $.scratch.Add "titles" (slice $title) }}
25
+
26
+ {{ partial "utils/extract-headings.html" (dict
27
+ "target" $heading
28
+ "scratch" $.scratch
29
+ )
30
+ }}
31
+ {{ end }}
Original file line number Diff line number Diff line change 3
3
{{ $page := .context }}
4
4
{{ $type := .type | default "content" }}
5
5
6
- {{ $headingKeys := slice }}
7
- {{ $headingTitles := slice }}
6
+ {{ $s := newScratch }}
7
+ {{ $s.Set "keys" slice }}
8
+ {{ $s.Set "titles" slice }}
8
9
9
- {{ range $h1 := $page.Fragments.Headings }}
10
- {{ if eq $h1.Title "" }}
11
- {{ $headingKeys = $headingKeys | append $h1.Title }}
12
- {{ else }}
13
- {{ $headingKeys = $headingKeys | append (printf "%s#%s" $h1.ID $h1.Title) }}
14
- {{ end }}
15
- {{ $headingTitles = $headingTitles | append (printf "< h1 > %s" $h1.Title) }}
10
+ {{ partial "utils/extract-headings.html" (dict "target" $page.Fragments "scratch" $s) }}
16
11
17
- {{ range $h2 := $h1.Headings }}
18
- {{ $headingKeys = $headingKeys | append (printf "%s#%s" $h2.ID $h2.Title) }}
19
- {{ $headingTitles = $headingTitles | append (printf "< h2 > %s" $h2.Title) }}
20
- {{ end }}
21
- {{ end }}
12
+ {{ $headingKeys := $s.Get "keys" }}
13
+ {{ $headingTitles := $s.Get "titles" }}
22
14
23
15
{{ $content := $page.Content | htmlUnescape }}
24
16
{{ $len := len $headingKeys }}
You can’t perform that action at this time.
0 commit comments