Created
June 26, 2020 18:04
-
-
Save y1zhou/cb3452b9a87f7a23c4f03fde66ee21e1 to your computer and use it in GitHub Desktop.
Hugo template for generating json file for Algolia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{/* $index is an array of json objects that we'll submit to Algolia */}} | |
{{ $index := slice }} | |
{{ range site.RegularPages }} | |
{{ $page := . }} | |
{{/* Split long posts into 1000-word chunks for less noise in results */}} | |
{{ $chunked := slice }} | |
{{ $post_len := len .PlainWords }} | |
{{ range $i := (seq 0 1000 $post_len) }} | |
{{ $chunked = $chunked | append (delimit (first 1000 (after $i $page.PlainWords) ) " " ) }} | |
{{ end }} | |
{{/* Add each chunk to $index with different object IDs */}} | |
{{ range $i, $c := $chunked }} | |
{{ $index = $index | append (dict "objectID" (print $page.File.UniqueID "_" $i) "title" $page.Title "date" $page.Date "href" $page.Permalink "featured" $page.Params.featured "summary" $page.Params.summary "content" $c ) }} | |
{{ end }} | |
{{ end }} | |
{{- $index | jsonify -}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment