mirror of
https://github.com/imfing/hextra.git
synced 2025-08-23 07:07:06 -04:00
feat: search in all headings (#740)
This commit is contained in:

committed by
GitHub

parent
30866e328c
commit
532cbcce10
31
layouts/_partials/utils/extract-headings.html
Normal file
31
layouts/_partials/utils/extract-headings.html
Normal 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 -}}
|
@@ -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 }}
|
||||
|
Reference in New Issue
Block a user