| 
									
										
										
										
											2025-08-11 17:36:37 +08:00
										 |  |  | {{- /* | 
					
						
							|  |  |  |   fragments.html - Split page content into searchable fragments | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   This partial processes a Hugo page and splits its content into fragments based on headings, | 
					
						
							|  |  |  |   creating a data structure suitable for search indexing. It supports different fragment types | 
					
						
							|  |  |  |   and handles hierarchical heading structures (h1, h2). | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Parameters: | 
					
						
							|  |  |  |   - .context (Page): The Hugo page to process | 
					
						
							|  |  |  |   - .type (string): Fragment type - "content" (default), "heading", "title", or "summary" | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Returns: | 
					
						
							|  |  |  |   - dict: Map of heading keys to content fragments | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Example: | 
					
						
							|  |  |  |   Input page with content: | 
					
						
							|  |  |  |     # Introduction | 
					
						
							|  |  |  |     This is the intro text. | 
					
						
							|  |  |  |     ## Setup | 
					
						
							|  |  |  |     Setup instructions here. | 
					
						
							|  |  |  |     # Configuration   | 
					
						
							|  |  |  |     Config details here. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Output (type "content"): | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     "": "This is the intro text.", | 
					
						
							|  |  |  |     "intro#Introduction": "This is the intro text. Setup instructions here.", | 
					
						
							|  |  |  |     "setup#Setup": "Setup instructions here.",  | 
					
						
							|  |  |  |     "config#Configuration": "Config details here." | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   Fragment types: | 
					
						
							|  |  |  |   - "content": Splits page content by headings (default) | 
					
						
							|  |  |  |   - "heading": Returns heading keys with empty content | 
					
						
							|  |  |  |   - "title": Returns empty content (title handled elsewhere) | 
					
						
							|  |  |  |   - "summary": Returns page summary only | 
					
						
							|  |  |  | */ -}} | 
					
						
							| 
									
										
										
										
											2023-08-06 01:06:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-11 17:36:37 +08:00
										 |  |  | {{- /* Extract page context and fragment type */ -}} | 
					
						
							|  |  |  | {{- $page := .context -}} | 
					
						
							|  |  |  | {{- $type := .type | default "content" -}} | 
					
						
							| 
									
										
										
										
											2023-08-06 01:06:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-12 02:52:21 +02:00
										 |  |  | {{- /* Process all headings */ -}} | 
					
						
							|  |  |  | {{- $s := newScratch -}} | 
					
						
							|  |  |  | {{- $s.Set "keys" slice -}} | 
					
						
							|  |  |  | {{- $s.Set "titles" slice -}} | 
					
						
							| 
									
										
										
										
											2023-08-06 01:06:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-12 02:52:21 +02:00
										 |  |  | {{- partial "utils/extract-headings.html" (dict "target" $page.Fragments "scratch" $s) -}} | 
					
						
							| 
									
										
										
										
											2025-08-11 17:36:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-12 02:52:21 +02:00
										 |  |  | {{- $headingKeys := $s.Get "keys" -}} | 
					
						
							|  |  |  | {{- $headingTitles := $s.Get "titles" -}} | 
					
						
							| 
									
										
										
										
											2023-08-06 01:06:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-11 17:36:37 +08:00
										 |  |  | {{- $content := $page.Content | htmlUnescape -}} | 
					
						
							|  |  |  | {{- $len := len $headingKeys -}} | 
					
						
							|  |  |  | {{- $data := dict -}} | 
					
						
							| 
									
										
										
										
											2023-08-06 01:06:32 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-21 16:00:39 -04:00
										 |  |  | {{ if eq $type "content" }} | 
					
						
							|  |  |  |   {{/* Include full content of the page */}} | 
					
						
							|  |  |  |   {{ if eq $len 0 }} | 
					
						
							| 
									
										
										
										
											2025-08-11 17:36:37 +08:00
										 |  |  |     {{ $data = $data | merge (dict "" ($page.Plain | htmlUnescape | strings.TrimSpace)) }} | 
					
						
							| 
									
										
										
										
											2023-10-21 16:00:39 -04:00
										 |  |  |   {{ else }} | 
					
						
							|  |  |  |     {{/* Split the raw content from bottom to top */}} | 
					
						
							|  |  |  |     {{ range seq $len }} | 
					
						
							|  |  |  |       {{ $i := sub $len . }} | 
					
						
							|  |  |  |       {{ $headingKey := index $headingKeys $i }} | 
					
						
							|  |  |  |       {{ $headingTitle := index $headingTitles $i }} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       {{ if eq $i 0 }} | 
					
						
							| 
									
										
										
										
											2025-08-11 17:36:37 +08:00
										 |  |  |         {{ $data = $data | merge (dict $headingKey ($content | plainify | htmlUnescape | strings.TrimSpace)) }} | 
					
						
							| 
									
										
										
										
											2023-10-21 16:00:39 -04:00
										 |  |  |       {{ else }} | 
					
						
							| 
									
										
										
										
											2025-08-11 08:23:30 +02:00
										 |  |  |         {{ $parts := split $content (printf "%s" $headingTitle) }} | 
					
						
							| 
									
										
										
										
											2025-08-12 02:52:21 +02:00
										 |  |  |         {{ $lastPart := index $parts (sub (len $parts) 1) }} | 
					
						
							| 
									
										
										
										
											2023-10-21 16:00:39 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-11 17:36:37 +08:00
										 |  |  |         {{ $data = $data | merge (dict $headingKey ($lastPart | plainify | htmlUnescape | strings.TrimSpace)) }} | 
					
						
							| 
									
										
										
										
											2023-10-21 16:00:39 -04:00
										 |  |  |         {{ $content = strings.TrimSuffix $lastPart $content }} | 
					
						
							| 
									
										
										
										
											2025-08-11 08:23:30 +02:00
										 |  |  |         {{ $content = strings.TrimSuffix (printf "%s" $headingTitle) $content }} | 
					
						
							| 
									
										
										
										
											2023-10-21 16:00:39 -04:00
										 |  |  |       {{ end }} | 
					
						
							| 
									
										
										
										
											2023-08-06 01:06:32 +01:00
										 |  |  |     {{ end }} | 
					
						
							|  |  |  |   {{ end }} | 
					
						
							| 
									
										
										
										
											2023-10-21 16:00:39 -04:00
										 |  |  | {{ else if (eq $type "heading" ) }} | 
					
						
							|  |  |  |   {{/* Put heading keys with empty content to the data object */}} | 
					
						
							|  |  |  |   {{ $data = dict "" "" }} | 
					
						
							|  |  |  |   {{ range $headingKeys }} | 
					
						
							|  |  |  |     {{ $data = $data | merge (dict . "") }} | 
					
						
							|  |  |  |   {{ end }} | 
					
						
							|  |  |  | {{ else if (eq $type "title") }} | 
					
						
							|  |  |  |   {{/* Use empty data object since title is included in search-data.json */}} | 
					
						
							|  |  |  |   {{ $data = $data | merge (dict "" "") }} | 
					
						
							|  |  |  | {{ else if (eq $type "summary" ) }} | 
					
						
							| 
									
										
										
										
											2025-08-11 17:36:37 +08:00
										 |  |  |   {{ $data = $data | merge (dict "" ($page.Summary | plainify | htmlUnescape | strings.TrimSpace)) }} | 
					
						
							| 
									
										
										
										
											2023-08-06 01:06:32 +01:00
										 |  |  | {{ end }} | 
					
						
							| 
									
										
										
										
											2023-10-21 16:00:39 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 01:06:32 +01:00
										 |  |  | {{ return $data }} |