Continuing the discussion from Possible to get markdown escaped as just plain text if not JSON?:
I’ve searched the forum and Github, but I cannot make a JSON valid search index in Hugo. Each time there’s code inside the content (between ~~~
and ~~~
), JSON fails.
I’ve had a lot of help with the information on the forum – my code is based on @rdwatters contributions – but cannot make it work. I really hope someone can help, because with this my site cannot have a search.
Starting point
My Hugo code is:
[
{{ range $index, $page := where .Site.Pages "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.Plain }}"
}
{{ end }}
{{ end }}
]
With the JSON lint validator, this gives the unexpected EOF error:
As we can see here, the "content"
JSON element breaks whenever there’s code come across.
Using jsonify
to encode the content to JSON
Hugo code:
[
{{ range $index, $page := where .Site.Pages "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 | jsonify }}"
}
{{ end }}
{{ end }}
]
Error:
JSONfying plain words
Hugo code:
[
{{ range $index, $page := where .Site.Pages "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.PlainWords | jsonify }}"
}
{{ end }}
{{ end }}
]
Error:
Just PlainWords
When using PlainWords as used in this topic:
[
{{ range $index, $page := where .Site.Pages "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.PlainWords }}"
}
{{ end }}
{{ end }}
]
Error:
(Again JSON breaks whenever there’s code come across.)
RawContent
with jsonify
Hugo code:
[
{{ range $index, $page := where .Site.Pages "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.RawContent | jsonify }}"
}
{{ end }}
{{ end }}
]
Error:
I’m using:
PS I:\site> hugo env
Hugo Static Site Generator v0.18 BuildDate: 2016-12-19T14:42:56+01:00
GOOS="windows"
GOARCH="amd64"
GOVERSION="go1.7.4"
The above JSON troubles also happened with Hugo 0.17 (although I did not test RawContent
on that Hugo version).
Thanks in advance for any suggestion or idea!