{{/* Copy from https://github.com/imfing/hextra/blob/v0.9.7/layouts/partials/utils/fragments.html */}} {{/* Split page raw content into fragments */}} {{ $page := .context }} {{ $type := .type | default "content" }} {{ $s := newScratch }} {{ $s.Set "keys" slice }} {{ $s.Set "titles" slice }} {{ partial "utils/extract-headings.html" (dict "target" $page.Fragments "scratch" $s) }} {{ $headingKeys := $s.Get "keys" }} {{ $headingTitles := $s.Get "titles" }} {{ $content := $page.Content | htmlUnescape }} {{ $len := len $headingKeys }} {{ $data := dict }} {{ if eq $type "content" }} {{/* Include full content of the page */}} {{ if eq $len 0 }} {{ $data = $data | merge (dict "" ($page.Plain | htmlUnescape | chomp)) }} {{ 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 }} {{ $data = $data | merge (dict $headingKey ($content | plainify | htmlUnescape | chomp)) }} {{ else }} {{ $parts := split $content (printf "%s" $headingTitle) }} {{ $lastPart := index $parts (sub (len $parts) 1) }} {{ $data = $data | merge (dict $headingKey ($lastPart | plainify | htmlUnescape | chomp)) }} {{ $content = strings.TrimSuffix $lastPart $content }} {{ $content = strings.TrimSuffix (printf "%s" $headingTitle) $content }} {{ end }} {{ end }} {{ end }} {{ 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" ) }} {{ $data = $data | merge (dict "" ($page.Summary | plainify | htmlUnescape | chomp)) }} {{ end }} {{ return $data }}