feat: search in all headings (#740)

This commit is contained in:
Ludovic Fernandez
2025-08-12 02:52:21 +02:00
committed by GitHub
parent 30866e328c
commit 532cbcce10
2 changed files with 39 additions and 19 deletions

View File

@@ -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 -}}

View File

@@ -40,26 +40,15 @@
{{- $page := .context -}} {{- $page := .context -}}
{{- $type := .type | default "content" -}} {{- $type := .type | default "content" -}}
{{- /* Initialize slices to store heading data */ -}} {{- /* Process all headings */ -}}
{{- $headingKeys := slice -}} {{- /* Keys for indexing (ID#Title or just Title) */ -}} {{- $s := newScratch -}}
{{- $headingTitles := slice -}} {{- /* HTML heading tags for content splitting */ -}} {{- $s.Set "keys" slice -}}
{{- $s.Set "titles" slice -}}
{{- /* Process all h1 headings and their nested h2 headings */ -}} {{- partial "utils/extract-headings.html" (dict "target" $page.Fragments "scratch" $s) -}}
{{- 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 "<h1>%s" $h1.Title) -}}
{{- /* Process nested h2 headings */ -}} {{- $headingKeys := $s.Get "keys" -}}
{{- range $h2 := $h1.Headings -}} {{- $headingTitles := $s.Get "titles" -}}
{{- $headingKeys = $headingKeys | append (printf "%s#%s" $h2.ID $h2.Title) -}}
{{- $headingTitles = $headingTitles | append (printf "<h2>%s" $h2.Title) -}}
{{- end -}}
{{- end -}}
{{- $content := $page.Content | htmlUnescape -}} {{- $content := $page.Content | htmlUnescape -}}
{{- $len := len $headingKeys -}} {{- $len := len $headingKeys -}}
@@ -80,7 +69,7 @@
{{ $data = $data | merge (dict $headingKey ($content | plainify | htmlUnescape | strings.TrimSpace)) }} {{ $data = $data | merge (dict $headingKey ($content | plainify | htmlUnescape | strings.TrimSpace)) }}
{{ else }} {{ else }}
{{ $parts := split $content (printf "%s" $headingTitle) }} {{ $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)) }} {{ $data = $data | merge (dict $headingKey ($lastPart | plainify | htmlUnescape | strings.TrimSpace)) }}
{{ $content = strings.TrimSuffix $lastPart $content }} {{ $content = strings.TrimSuffix $lastPart $content }}