Skip to content

Commit 34ab0cb

Browse files
authored
dev: improve documentation search index (#5989)
1 parent 034597c commit 34ab0cb

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 }}

docs/layouts/_partials/utils/fragments.html

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,14 @@
33
{{ $page := .context }}
44
{{ $type := .type | default "content" }}
55

6-
{{ $headingKeys := slice }}
7-
{{ $headingTitles := slice }}
6+
{{ $s := newScratch }}
7+
{{ $s.Set "keys" slice }}
8+
{{ $s.Set "titles" slice }}
89

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) }}
1611

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" }}
2214

2315
{{ $content := $page.Content | htmlUnescape }}
2416
{{ $len := len $headingKeys }}

0 commit comments

Comments
 (0)