Hey everybody,
I have a weird escaping issue I’m not sure how to solve. On a site I’m building I have a shortcode (“review”) to add a review to the page. The review shortcode intends to add both the “structured data in JSON-LD format” and the visible HTML of the review.
My content page (which is generated from the review database periodically includes this:
{{% review name="Reviewer" %}} Love's this product {{% /quote %}}
This is layouts/shortcodes/review.html:
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Review",
"itemReviewed": {
"@type": "Product",
"name": "XYZ",
"url": "{{ "services" | absLangURL }}"
},
"author": "{{ .Get "name" }}",
"reviewBody": "{{ .Inner }}"
}
</script>
<div>
<p>{{.Inner}} - {{ .Get "name" }}</p>
</div>
The result is:
<script type="application/ld+json">
{
"@context": "http://schema.org/",
"@type": "Review",
"itemReviewed": {
"@type": "Product",
"name": "XYZ",
"url": "http:\/\/localhost:1313\/en\/services"
},
"author": "Reviewer",
"reviewBody": " Love\x27s this product"
}
</script>
<div>
<p>Love's this product - Reviewer</p>
</div>
Two things get escaped in the JSON-LD part (but not the HTML part). The url and the reviewBody. So it seems to be caused by Hugo assuming it needs to handle the JSON-LD part differently. Especially the escape in the reviewBody is ‘illegal’ in JSON.
I’ve tried using '
in the review text, but then it just gets escapes to \x26apos;
which is again illegal. I tried adding | safeHTML
and | safeJS
but both don’t seem to affect the outcome.
For the url I’ve tried (with no effect):
"url": "{{ "services" | absLangURL | safeURL }}"
Please help me understand what I can do to prevent this escaping behaviour within a Hugo Shortcode!