[Custom output format] How to prevent whitespace?

I’m using the custom output format feature of Hugo 0.20.

Previously, I used a single.html file to generate a JSON search index as a .html file. That outcome looked like this:

Now when I create a JSON file with the custom output feature, I get the following:

This unwanted whitespace invalidates my JSON file. Why this happens, I don’t know. I’m using the same Hugo code as before, and even when I add the - to avoid generating blank lines, the same problem happens.

This is what I have in my single.json file:

[
{{- range $index, $page := where .Site.RegularPages "Section" "!=" "" -}}
{{- if and $index (gt $index 0) -}},{{- end -}}
    {{- if (ne $page.Section "") -}}
    {
        "objectID": "{{- $page.UniqueID -}}",
        "url":      "{{- $page.RelPermalink -}}",
        "title":    "{{- $page.Title -}}",
        "platform": "{{- index $page.Params.categories 0 -}}",
        "content":  "{{- $page.Content | plainify -}}"
    }
    {{- end -}}
{{- end -}}
]

Thanks!

2 Likes