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

@@ -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 "<h1>%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 "<h2>%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 }}