|
| 1 | +{{/* Copy from https://github.com/imfing/hextra/blob/v0.9.7/layouts/partials/utils/fragments.html */}} |
| 2 | +{{/* Split page raw content into fragments */}} |
| 3 | +{{ $page := .context }} |
| 4 | +{{ $type := .type | default "content" }} |
| 5 | + |
| 6 | +{{ $headingKeys := slice }} |
| 7 | +{{ $headingTitles := slice }} |
| 8 | + |
| 9 | +{{ range $h1 := $page.Fragments.Headings }} |
| 10 | + {{ if eq $h1.Title "" }} |
| 11 | + {{ $headingKeys = $headingKeys | append $h1.Title }} |
| 12 | + {{ else }} |
| 13 | + {{ $headingKeys = $headingKeys | append (printf "%s#%s" $h1.ID $h1.Title) }} |
| 14 | + {{ end }} |
| 15 | + {{ $headingTitles = $headingTitles | append (printf "# %s" $h1.Title) }} |
| 16 | + |
| 17 | + {{ range $h2 := $h1.Headings }} |
| 18 | + {{ $headingKeys = $headingKeys | append (printf "%s#%s" $h2.ID $h2.Title) }} |
| 19 | + {{ $headingTitles = $headingTitles | append (printf "## %s" $h2.Title) }} |
| 20 | + {{ end }} |
| 21 | +{{ end }} |
| 22 | + |
| 23 | +{{ $content := $page.RawContent }} |
| 24 | +{{ $len := len $headingKeys }} |
| 25 | +{{ $data := dict }} |
| 26 | + |
| 27 | +{{ if eq $type "content" }} |
| 28 | + {{/* Include full content of the page */}} |
| 29 | + {{ if eq $len 0 }} |
| 30 | + {{ $data = $data | merge (dict "" ($page.Plain | htmlUnescape | chomp)) }} |
| 31 | + {{ else }} |
| 32 | + {{/* Split the raw content from bottom to top */}} |
| 33 | + {{ range seq $len }} |
| 34 | + {{ $i := sub $len . }} |
| 35 | + {{ $headingKey := index $headingKeys $i }} |
| 36 | + {{ $headingTitle := index $headingTitles $i }} |
| 37 | + |
| 38 | + {{ if eq $i 0 }} |
| 39 | + {{ $data = $data | merge (dict $headingKey ($content | $page.RenderString | plainify | htmlUnescape | chomp)) }} |
| 40 | + {{ else }} |
| 41 | + {{ $parts := split $content (printf "\n%s\n" $headingTitle) }} |
| 42 | + {{ $lastPart := index $parts (sub (len $parts) 1) }} |
| 43 | + |
| 44 | + {{ $data = $data | merge (dict $headingKey ($lastPart | $page.RenderString | plainify | htmlUnescape | chomp)) }} |
| 45 | + {{ $content = strings.TrimSuffix $lastPart $content }} |
| 46 | + {{ $content = strings.TrimSuffix (printf "\n%s\n" $headingTitle) $content }} |
| 47 | + {{ end }} |
| 48 | + {{ end }} |
| 49 | + {{ end }} |
| 50 | +{{ else if (eq $type "heading" ) }} |
| 51 | + {{/* Put heading keys with empty content to the data object */}} |
| 52 | + {{ $data = dict "" "" }} |
| 53 | + {{ range $headingKeys }} |
| 54 | + {{ $data = $data | merge (dict . "") }} |
| 55 | + {{ end }} |
| 56 | +{{ else if (eq $type "title") }} |
| 57 | + {{/* Use empty data object since title is included in search-data.json */}} |
| 58 | + {{ $data = $data | merge (dict "" "") }} |
| 59 | +{{ else if (eq $type "summary" ) }} |
| 60 | + {{ $data = $data | merge (dict "" ($page.Summary | plainify | htmlUnescape | chomp)) }} |
| 61 | +{{ end }} |
| 62 | + |
| 63 | +{{ return $data }} |
0 commit comments