|
1 | | -{{/* Split page raw content into fragments */}} |
2 | | -{{ $page := .context }} |
3 | | -{{ $type := .type | default "content" }} |
| 1 | +{{- /* |
| 2 | + fragments.html - Split page content into searchable fragments |
| 3 | + |
| 4 | + This partial processes a Hugo page and splits its content into fragments based on headings, |
| 5 | + creating a data structure suitable for search indexing. It supports different fragment types |
| 6 | + and handles hierarchical heading structures (h1, h2). |
| 7 | + |
| 8 | + Parameters: |
| 9 | + - .context (Page): The Hugo page to process |
| 10 | + - .type (string): Fragment type - "content" (default), "heading", "title", or "summary" |
| 11 | + |
| 12 | + Returns: |
| 13 | + - dict: Map of heading keys to content fragments |
| 14 | + |
| 15 | + Example: |
| 16 | + Input page with content: |
| 17 | + # Introduction |
| 18 | + This is the intro text. |
| 19 | + ## Setup |
| 20 | + Setup instructions here. |
| 21 | + # Configuration |
| 22 | + Config details here. |
| 23 | + |
| 24 | + Output (type "content"): |
| 25 | + { |
| 26 | + "": "This is the intro text.", |
| 27 | + "intro#Introduction": "This is the intro text. Setup instructions here.", |
| 28 | + "setup#Setup": "Setup instructions here.", |
| 29 | + "config#Configuration": "Config details here." |
| 30 | + } |
| 31 | + |
| 32 | + Fragment types: |
| 33 | + - "content": Splits page content by headings (default) |
| 34 | + - "heading": Returns heading keys with empty content |
| 35 | + - "title": Returns empty content (title handled elsewhere) |
| 36 | + - "summary": Returns page summary only |
| 37 | +*/ -}} |
4 | 38 |
|
5 | | -{{ $headingKeys := slice }} |
6 | | -{{ $headingTitles := slice }} |
| 39 | +{{- /* Extract page context and fragment type */ -}} |
| 40 | +{{- $page := .context -}} |
| 41 | +{{- $type := .type | default "content" -}} |
7 | 42 |
|
8 | | -{{ range $h1 := $page.Fragments.Headings }} |
9 | | - {{ if eq $h1.Title "" }} |
10 | | - {{ $headingKeys = $headingKeys | append $h1.Title }} |
11 | | - {{ else }} |
12 | | - {{ $headingKeys = $headingKeys | append (printf "%s#%s" $h1.ID $h1.Title) }} |
13 | | - {{ end }} |
14 | | - {{ $headingTitles = $headingTitles | append (printf "<h1>%s" $h1.Title) }} |
| 43 | +{{- /* Initialize slices to store heading data */ -}} |
| 44 | +{{- $headingKeys := slice -}} {{- /* Keys for indexing (ID#Title or just Title) */ -}} |
| 45 | +{{- $headingTitles := slice -}} {{- /* HTML heading tags for content splitting */ -}} |
15 | 46 |
|
16 | | - {{ range $h2 := $h1.Headings }} |
17 | | - {{ $headingKeys = $headingKeys | append (printf "%s#%s" $h2.ID $h2.Title) }} |
18 | | - {{ $headingTitles = $headingTitles | append (printf "<h2>%s" $h2.Title) }} |
19 | | - {{ end }} |
20 | | -{{ end }} |
| 47 | +{{- /* Process all h1 headings and their nested h2 headings */ -}} |
| 48 | +{{- range $h1 := $page.Fragments.Headings -}} |
| 49 | + {{- /* Handle h1 headings - empty titles get special treatment */ -}} |
| 50 | + {{- if eq $h1.Title "" -}} |
| 51 | + {{- $headingKeys = $headingKeys | append $h1.Title -}} |
| 52 | + {{- else -}} |
| 53 | + {{- $headingKeys = $headingKeys | append (printf "%s#%s" $h1.ID $h1.Title) -}} |
| 54 | + {{- end -}} |
| 55 | + {{- $headingTitles = $headingTitles | append (printf "<h1>%s" $h1.Title) -}} |
| 56 | + |
| 57 | + {{- /* Process nested h2 headings */ -}} |
| 58 | + {{- range $h2 := $h1.Headings -}} |
| 59 | + {{- $headingKeys = $headingKeys | append (printf "%s#%s" $h2.ID $h2.Title) -}} |
| 60 | + {{- $headingTitles = $headingTitles | append (printf "<h2>%s" $h2.Title) -}} |
| 61 | + {{- end -}} |
| 62 | +{{- end -}} |
21 | 63 |
|
22 | | -{{ $content := $page.Content | htmlUnescape }} |
23 | | -{{ $len := len $headingKeys }} |
24 | | -{{ $data := dict }} |
| 64 | +{{- $content := $page.Content | htmlUnescape -}} |
| 65 | +{{- $len := len $headingKeys -}} |
| 66 | +{{- $data := dict -}} |
25 | 67 |
|
26 | 68 | {{ if eq $type "content" }} |
27 | 69 | {{/* Include full content of the page */}} |
28 | 70 | {{ if eq $len 0 }} |
29 | | - {{ $data = $data | merge (dict "" ($page.Plain | htmlUnescape | chomp)) }} |
| 71 | + {{ $data = $data | merge (dict "" ($page.Plain | htmlUnescape | strings.TrimSpace)) }} |
30 | 72 | {{ else }} |
31 | 73 | {{/* Split the raw content from bottom to top */}} |
32 | 74 | {{ range seq $len }} |
|
35 | 77 | {{ $headingTitle := index $headingTitles $i }} |
36 | 78 |
|
37 | 79 | {{ if eq $i 0 }} |
38 | | - {{ $data = $data | merge (dict $headingKey ($content | plainify | htmlUnescape | chomp)) }} |
| 80 | + {{ $data = $data | merge (dict $headingKey ($content | plainify | htmlUnescape | strings.TrimSpace)) }} |
39 | 81 | {{ else }} |
40 | 82 | {{ $parts := split $content (printf "%s" $headingTitle) }} |
41 | | - {{ $lastPart := index $parts (sub (len $parts) 1) }} |
| 83 | + {{ $lastPart := index $parts (sub (len $parts) 1) | strings.TrimSpace }} |
42 | 84 |
|
43 | | - {{ $data = $data | merge (dict $headingKey ($lastPart | plainify | htmlUnescape | chomp)) }} |
| 85 | + {{ $data = $data | merge (dict $headingKey ($lastPart | plainify | htmlUnescape | strings.TrimSpace)) }} |
44 | 86 | {{ $content = strings.TrimSuffix $lastPart $content }} |
45 | 87 | {{ $content = strings.TrimSuffix (printf "%s" $headingTitle) $content }} |
46 | 88 | {{ end }} |
|
56 | 98 | {{/* Use empty data object since title is included in search-data.json */}} |
57 | 99 | {{ $data = $data | merge (dict "" "") }} |
58 | 100 | {{ else if (eq $type "summary" ) }} |
59 | | - {{ $data = $data | merge (dict "" ($page.Summary | plainify | htmlUnescape | chomp)) }} |
| 101 | + {{ $data = $data | merge (dict "" ($page.Summary | plainify | htmlUnescape | strings.TrimSpace)) }} |
60 | 102 | {{ end }} |
61 | 103 |
|
62 | 104 | {{ return $data }} |
0 commit comments