How to show symbols correctly

In some of my posts, I have titles with a / symbol which becomes \/ in my JSON-LD schema. What is causing this and how to stop it?

Permalinks slashes are also escaped when the quotation marks are added but Hugo strips them when the quotation marks are removed. Is that by design?

How are you building this?

Sorry, I don’t understand your question.

Are you manually building the object piece by piece, or are you creating a map then passing it through jsonify? Seeing the template would be helpful.

Here is an example for breadcrumbs schema (thanks for this btw which I found yesterday). The .Params.subtitle is the one with some values with a / which appears as \/.

<script type="application/ld+json">{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":{{ site.BaseURL }},"name":"{{ site.Title }}"}},{"@type":"ListItem","position":2,"item":{"@id"{{ with .Parent }}{{ with .Parent }}{{ .Permalink }}{{ end }}{{ end }},"name":"{{ with .Parent }}{{ with .Parent }}{{ .Title }}{{ end }}{{ end }}"}},{"@type":"ListItem","position":3,"item":{"@id"{{ with .Parent }}{{ .Permalink }}{{ end }},"name":"{{ with .Parent }}{{ .Title }}{{ end }}"}},{"@type":"ListItem","position":4,"item":{"@id":{{ .Permalink }},"name": "{{ .Title }} - {{ .Params.subtitle }}"}}]}</script>

What is the value of .Params.subtitle?

Please show an example of the front matter.

---
subtitle: Schools built in the Year 2017/2018
---

Comments:

  • Your JSON is malformed, missing a colon in the second list item.
  • The name attribute of the last list item is fine. The JSON consumer will handle this correctly.
  • This would much simpler if you used the .Ancestors method that was added in v0.109.0.

Corrected it.

For example? This code is something I have used for years, just changing the syntax, from WP, to Jekyll to Hugo.

layouts/partials/json-ld.html
{{- $items := slice }}

{{- $p := .Ancestors.Reverse | append . }}
{{- range $k, $_ := $p }}
  {{- $title := .Title }}
  {{- with .Params.subtitle }}
    {{- $title = printf "%s - %s" $title . }}
  {{- end }}
  {{- $item := dict
    "@type" "ListItem"
    "position" (add $k 1)
    "item" (dict
      "@id" .Permalink
      "name" $title
    )
  }}
  {{- $items = $items | append $item }}
{{- end }}

{{- $data := dict
  "@context" "https://schema.org"
  "@type" "BreadcrumbList"
  "itemListElement" $items
}}

<script type="application/ld+json">
  {{- jsonify (dict "indent" "  ") $data | safeJS }}
</script>

2 Likes

That’s more complex than I thought. In mine, I exclude the parent section (e.g. content/news) since it is just for grouping similar content. I cannot figure out how to do so with your implementation.

Skip if one level down from home.

layouts/partials/json-ld.html
{{- $items := slice }}

{{- $p := .Ancestors.Reverse | append . }}
{{- $position := 0 }}
{{- range $k, $_ := $p }}
  {{- if ne $k 1 }}
    {{- $position = add $position 1 }}
    {{- $title := .Title }}
    {{- with .Params.subtitle }}
      {{- $title = printf "%s - %s" $title . }}
    {{- end }}
    {{- $item := dict
      "@type" "ListItem"
      "position" $position
      "item" (dict
        "@id" .Permalink
        "name" $title
      )
    }}
    {{- $items = $items | append $item }}
  {{- end }}
{{- end }}

{{- $data := dict
  "@context" "https://schema.org"
  "@type" "BreadcrumbList"
  "itemListElement" $items
}}

<script type="application/ld+json">
  {{- jsonify (dict "indent" "  ") $data | safeJS }}
</script>

2 Likes

Now position jumps from 1 to 3 and skips 2.

Check the source above. I edited it right after the initial post.

One more edit: I corrected the opening script tag.

I figured that out just now coz the schema validator was missing the breadcrumb list. This helped me a lot coz I had several if/else conditions for different sections/pages.

1 Like

The benefits of this approach:

  1. It works at every level without a bunch of conditionals
  2. You don’t have to format the JSON yourself (avoid syntax errors)

I appreciate it! Is there a similar implementation for article/blogposting schema?