Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jsonify is escaping characters #7229

Closed
zbayoff opened this issue Apr 30, 2020 · 6 comments
Closed

Jsonify is escaping characters #7229

zbayoff opened this issue Apr 30, 2020 · 6 comments

Comments

@zbayoff
Copy link
Contributor

zbayoff commented Apr 30, 2020

What version of Hugo are you using (hugo version)?

Hugo Static Site Generator v0.63.1-CE9ACEB7 darwin/amd64 BuildDate: 2020-01-23T20:10:12Z

Test:

{{ $testDict := dict "<any-key> " “value”}}
{{ $testDict | jsonify }}

gives:

{"\u003cany-key\u003e":“value”}

Is this a bug with jsonify or is there a hugo function I’m missing? Even a short term fix/hack would be greatly appreciated.
Thanks!

@bep
Copy link
Member

bep commented Apr 30, 2020

It's the Go HTML template doing its ... job.

Try

{{ $testDict | jsonify | safeJS }}
or

{{ $testDict | jsonify | safeHTML }}

@bep bep closed this as completed Apr 30, 2020
@zbayoff
Copy link
Contributor Author

zbayoff commented Apr 30, 2020

@bep Those don't work unfortunately. I had to resolve with a replace:

replace (replace $testDict "\\u003c" "<") "\\u003e" ">"

@bep bep reopened this Apr 30, 2020
@bep
Copy link
Member

bep commented Apr 30, 2020

That function just delegates to Go's JSON package, so not sure what's going on ... But I'll reopen.

@bep bep added this to the v0.71 milestone Apr 30, 2020
@bep bep modified the milestones: v0.71, v0.72 May 18, 2020
@bep bep modified the milestones: v0.72, v0.73 May 31, 2020
@bep bep modified the milestones: v0.73, v0.74 Jun 23, 2020
@bep bep modified the milestones: v0.74, v0.75 Jul 13, 2020
@bep bep modified the milestones: v0.75, v0.76 Sep 14, 2020
@bep bep modified the milestones: v0.76, v0.77 Oct 6, 2020
@bep bep modified the milestones: v0.77, v0.78 Oct 30, 2020
@bep bep modified the milestones: v0.78, v0.83 Apr 23, 2021
@bep bep modified the milestones: v0.83, v0.84 May 3, 2021
@bep bep modified the milestones: v0.84, v0.85 Jun 18, 2021
@bep bep modified the milestones: v0.85, v0.86 Jul 5, 2021
@bep bep removed this from the v0.86 milestone Jul 26, 2021
@bep bep modified the milestones: v0.109.0, v0.111.0, v0.110.0 Jan 26, 2023
@bep bep modified the milestones: v0.111.0, v0.112.0 Feb 15, 2023
@bep bep modified the milestones: v0.112.0, v0.113.0 Apr 15, 2023
@bep bep modified the milestones: v0.113.0, v0.115.0 Jun 13, 2023
@bep bep modified the milestones: v0.115.0, v0.116.0 Jun 30, 2023
@bep bep modified the milestones: v0.116.0, v0.117.0 Aug 1, 2023
@bep bep modified the milestones: v0.117.0, v0.118.0 Aug 30, 2023
@bep bep modified the milestones: v0.118.0, v0.119.0 Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 4, 2023
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@jmooring
Copy link
Member

There's nothing wrong with escaping those characters. See this roundtrip:

{{ index (dict "<key>" "val" | jsonify | transform.Unmarshal) "<key>" }} --> val

If you really don't want to escape &, <, and >, do this:

{{ dict "<key>" "val" | jsonify (dict "noHTMLEscape" true) }}

See https://gohugo.io/functions/encoding/jsonify/

Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants