beautifulhugo
beautifulhugo copied to clipboard
Single quotes cause invalid JSON-LD data
If the .Summary variable of a post or article contains a single quote, the description property of the JSON-LD metadata for that page will contain \x26lsquo;
. This is not valid JSON and causes Google to complain. I assume this is related to HTML escaping. Changing
"description" : "{{ if .Description }}{{ .Description | plainify }}{{ else }}{{if .IsPage}}{{ .Summary | plainify }}{{ end }}{{ end }}",
to
"description" : "{{ if .Description }}{{ .Description | plainify }}{{ else }}{{if .IsPage}}{{ htmlUnescape .Summary | plainify }}{{ end }}{{ end }}",
seems to fix it, although I don't know if this is safe or robust.
Additionally if there is a single quote in the .Description variable it shows up as \x27
, which also makes the JSON invalid.
I noticed the exact same problem. It is also Google that made me aware of it.
Btw, the problem not only applies to the summary, but also to the author name (my last name is O'Brien!!) and to the page title. These fields are also used in the json-ld data.
Maybe the the issue's title should be updated?
Example from my website:
</script><script type="application/ld+json">
--
| {
| "@context": "http://schema.org",
| "@type": "Article",
| "author": {
| "name" : "Daniel O\x27Brien"
| },
| "headline": "Entre l\x27Afrique, l\x27Europe et l\x27Asie",
| "description" : "Lors de nos neuf semaines en Éthiopie et à Djibouti, nous avons vu des paysages à couper le souffle et rencontré des gens exceptionnels. Par contre, voyager en tant que backpacker dans la Corne de l\x26rsquo;Afrique est épuisant. Notre bref passage en Europe nous fait prendre du recul et, surtout, nous permet de recharger nos batteries. Voici la suite de nos aventures parmi les produits laitiers, les baklavas et les mosquées.\n",
My temporary workaround is to hardcode the use of hugo v.0.54.0 in my CI/CD pipeline.
This is not still fixed as of hugo v59.0. What is the best workaround here? The way @henribru suggests works for me but it gets rid of escaped characters and I wonder if it somewhat affects the search result.
@akiradeveloper Removing quotes around the values e.g. changing the example in original post to "description" : {{ if .Description }}{{ .Description | plainify }}{{ else }}{{if .IsPage}}{{ htmlUnescape .Summary | plainify }}{{ end }}{{ end }}
would work as well.