Skip to content

Instantly share code, notes, and snippets.

@y1zhou
Created June 26, 2020 18:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Hugo template for generating json file for Algolia
{{/* $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