{{- if .Site.Params.search.enable -}}
  {{- $index := slice -}}
  {{- $pages := where .Site.RegularPages "Params.password" "eq" nil -}}
  {{- if .Site.Params.page.hiddenFromSearch -}}
    {{- $pages = where $pages "Params.hiddenfromsearch" false -}}
  {{- else -}}
    {{- $pages = where $pages "Params.hiddenfromsearch" "!=" true -}}
  {{- end -}}
  {{- range $pages -}}
    {{- $uri := .RelPermalink -}}
    {{- if $.Site.Params.search.absoluteURL -}}
      {{- $uri = .Permalink -}}
    {{- end -}}
    {{- $meta := dict "uri" $uri "title" .Title "tags" .Params.tags "categories" .Params.categories "collections" .Params.collections -}}
    {{- $meta = $.Site.Params.dateFormat | default "2006-01-02" | .PublishDate.Format | dict "date" | merge $meta -}}
    {{- with .Description -}}
      {{- $index = $index | append (dict "content" . "objectID" $uri | merge $meta) -}}
    {{- end -}}
    {{- $params := .Params | merge $.Site.Params.page -}}
    {{/* Extended Markdown syntax */}}
    {{- $content := dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" -}}
    {{/* Remove line number for code */}}
    {{- $content = $content | replaceRE `<span class="lnt?"> *\d*\n?</span>` "" -}}
    {{- $content = $content | replaceRE ` class="heading-element"` "" -}}
    {{- if ne $.Site.Params.search.anchorify false -}}
      {{- /* When anchorify is enabled (default), create separate index entries for each heading */ -}}
      {{- $anchor := "" -}}
      {{- range $h, $contenth := split $content "<h1 id=" -}}
        {{- if gt $h 0 -}}
          {{- $anchor = replace (index (split $contenth ">") 0) `"` "" -}}
          {{- $contenth = printf "<h1 id=%v" $contenth -}}
        {{- end -}}
        {{- range $i, $contenti := split $contenth "<h2 id=" -}}
          {{- if gt $i 0 -}}
            {{- $anchor = replace (index (split $contenti ">") 0) `"` "" -}}
            {{- $contenti = printf "<h2 id=%v" $contenti -}}
          {{- end -}}
          {{- range $j, $contentj := split $contenti "<h3 id=" -}}
            {{- if gt $j 0 -}}
              {{- $anchor = replace (index (split $contentj ">") 0) `"` "" -}}
              {{- $contentj = printf "<h3 id=%v" $contentj -}}
            {{- end -}}
            {{- range $k, $contentk := split $contentj "<h4 id=" -}}
              {{- if gt $k 0 -}}
                {{- $anchor = replace (index (split $contentk ">") 0) `"` "" -}}
                {{- $contentk = printf "<h4 id=%v" $contentk -}}
              {{- end -}}
              {{- range $l, $contentl := split $contentk "<h5 id=" -}}
                {{- if gt $l 0 -}}
                  {{- $anchor = replace (index (split $contentl ">") 0) `"` "" -}}
                  {{- $contentk = printf "<h5 id=%v" $contentl -}}
                {{- end -}}
                {{- range $m, $contentm := split $contentl "<h6 id=" -}}
                  {{- if gt $m 0 -}}
                    {{- $anchor = replace (index (split $contentm ">") 0) `"` "" -}}
                    {{- $contentm = printf "<h6 id=%v" $contentm -}}
                  {{- end -}}
                  {{/* Plainify and remove (\n, \t) */}}
                  {{- $contentj = $contentj | plainify | replaceRE `[\n\t ]+` " " -}}
                  {{- if gt $.Site.Params.search.contentLength 0 -}}
                    {{- $contentj = substr $contentj 0 $.Site.Params.search.contentLength -}}
                  {{- end -}}
                  {{- if $contentj | and (ne $contentj " ") -}}
                    {{- $one := printf "%v:%v:%v" $uri $i $j | dict "content" $contentj "uri" (printf "%v#%v" $uri $anchor) "objectID" | merge $meta -}}
                    {{- $index = $index | append $one -}}
                  {{- end -}}
                {{- end -}}
              {{- end -}}
            {{- end -}}
          {{- end -}}
        {{- end -}}
      {{- end -}}
    {{- else -}}
      {{- /* When anchorify is disabled, create index entries by splitting at h2 and h3 headings only */ -}}
      {{- range $i, $contenti := split $content "<h2 id=" -}}
        {{- if gt $i 0 -}}
          {{- $contenti = printf "<h2 id=%v" $contenti -}}
        {{- end -}}
        {{- range $j, $contentj := split $contenti "<h3 id=" -}}
          {{- if gt $j 0 -}}
            {{- $contentj = printf "<h3 id=%v" $contentj -}}
          {{- end -}}
          {{/* Plainify and remove (\n, \t) */}}
          {{- $contentj = $contentj | plainify | replaceRE `[\n\t ]+` " " -}}
          {{- if gt $.Site.Params.search.contentLength 0 -}}
            {{- $contentj = substr $contentj 0 $.Site.Params.search.contentLength -}}
          {{- end -}}
          {{- if $contentj | and (ne $contentj " ") -}}
            {{- $one := printf "%v:%v:%v" $uri $i $j | dict "content" $contentj "objectID" | merge $meta -}}
            {{- $index = $index | append $one -}}
          {{- end -}}
        {{- end -}}
      {{- end -}}
    {{- end -}}
  {{- end -}}

  {{- $index | jsonify | safeJS -}}
{{- end -}}
