{{- /* Copy from https://github.com/HugoBlox/hugo-blox-builder/blob/main/modules/blox-bootstrap/layouts/index.json. */ -}}
{{- /* Generate the search index. */ -}}
{{- $index := slice -}}
{{- $pages := site.RegularPages -}}
{{- /* Add the index page of multi-page content separately since it's not in RegularPages above. */ -}}
{{- $pages := $pages | union (where (where site.Pages "Kind" "section") "Type" "book") -}}
{{- /* Add author pages to index so their bios can be searched. Hide empty `/authors/` node. */ -}}
{{- $pages := $pages | union (where (where site.Pages "Section" "authors") "Params.superuser" "!=" nil) -}}

{{- range $pages -}}
  {{- /* Do not index drafts or private pages. */ -}}
  {{- if and (not .Draft) (not .Params.private) | and (ne .Params.searchable false) -}}

    {{- /* Generate page description. */ -}}
    {{- $desc := "" -}}
    {{- if .Params.summary -}}
      {{- $desc = .Params.summary -}}
    {{- else if .Params.abstract -}}
      {{- $desc = .Params.abstract -}}
    {{- else -}}
      {{- $desc = .Summary -}}
    {{- end -}}

    {{- $authors := .Params.authors -}}
    {{- $title := .Title}}
    {{- $rel_permalink := .RelPermalink -}}
    {{- $permalink := .Permalink -}}

    {{/* Correct the title and URL for author profile pages. */}}
    {{- if eq .Section "authors" -}}
      {{- $username := path.Base .File.Dir -}}
      {{- with site.GetPage (printf "/authors/%s" $username) -}}
        {{- $permalink = .Permalink -}}
        {{- $rel_permalink = .RelPermalink -}}
      {{- end -}}
    {{- else -}}
      {{/* Include a user's display name rather than username where possible. */}}
      {{- if .Params.authors -}}
        {{- $authorLen := len .Params.authors -}}
        {{- if gt $authorLen 0 -}}
          {{- $authors = slice -}}
            {{- range $k, $v := .Params.authors -}}
              {{- $person_page_path := (printf "/authors/%s" (urlize $v)) -}}
              {{- $person_page := site.GetPage $person_page_path -}}
              {{- if and $person_page $person_page.File -}}
                {{- $authors = $authors | append $person_page.Title -}}
              {{- else -}}
                {{- $authors = $authors | append ($v | plainify) -}}
              {{- end -}}
            {{- end -}}
          {{- end -}}
        {{- end -}}
    {{- end -}}

    {{- /* Add page to index. */ -}}
    {{/* Exclude virtual pages which aren't backed by a file */}}
    {{- if .File }}
      {{/* exclude "content/readme.md". */}}
      {{ $hidden_file := "/readme/" }}
      {{ if ne $hidden_file $rel_permalink }}
        {{- $index = $index | append (dict "objectID" .File.UniqueID "date" .Date.UTC.Unix "publishdate" .PublishDate "lastmod" .Lastmod.UTC.Unix "expirydate" .ExpiryDate.UTC.Unix "lang" .Lang "permalink" $permalink "relpermalink" $rel_permalink "title" $title "summary" (plainify $desc | htmlUnescape) "content" (.Plain | htmlUnescape | truncate 5000) "authors" $authors "kind" .Kind "type" .Type "section" .Section "tags" .Params.Tags "categories" .Params.Categories) -}}
      {{ end }}
    {{ end -}}
  {{- end -}}
{{- end -}}

{{- $index | jsonify -}}
