1
Files
2025/layouts/partials/head/json-ld.html

164 lines
5.7 KiB
HTML

{{- $page := . -}}
{{- $data := dict -}}
{{- if reflect.IsMap . -}}
{{- with index . "Page" -}}
{{- $page = . -}}
{{- end -}}
{{- with index . "Data" -}}
{{- $data = . -}}
{{- end -}}
{{- end -}}
{{- if eq (len $data) 0 -}}
{{- $data = partialCached "head/data.html" $page $page.RelPermalink -}}
{{- end -}}
{{- $noindex := index $data "noindex" -}}
{{- if not $noindex -}}
{{- $language := index $data "localeCode" -}}
{{- $description := index $data "description" -}}
{{- $pageClassification := index $data "pageClassification" -}}
{{- $websiteID := printf "%s#website" $page.Site.Home.Permalink -}}
{{- $author := partial "seo/person.html" $page -}}
{{- $authorID := index $author "@id" -}}
{{- $website := dict
"@type" "WebSite"
"@id" $websiteID
"url" $page.Site.Home.Permalink
"name" $page.Site.Title
"inLanguage" $language
"author" (dict "@id" $authorID)
-}}
{{- with $page.Site.Params.description -}}
{{- $website = merge $website (dict "description" .) -}}
{{- end -}}
{{- with $page.Site.Params.search -}}
{{- with .action -}}
{{- $searchURL := . | absURL -}}
{{- $searchParam := $page.Site.Params.search.param | default "q" -}}
{{- $website = merge $website (dict
"potentialAction" (dict
"@type" "SearchAction"
"target" (printf "%s?%s={search_term_string}" $searchURL $searchParam)
"query-input" "required name=search_term_string"
)
) -}}
{{- end -}}
{{- end -}}
{{- $graph := slice $website $author -}}
{{- if not $page.IsHome -}}
{{- $breadcrumbPages := partial "seo/breadcrumb-pages.html" $page -}}
{{- if gt (len $breadcrumbPages) 1 -}}
{{- $items := slice -}}
{{- range $index, $item := $breadcrumbPages -}}
{{- $items = $items | append (dict
"@type" "ListItem"
"position" (add $index 1)
"name" (default $item.Title $item.LinkTitle)
"item" $item.Permalink
) -}}
{{- end -}}
{{- $graph = $graph | append (dict
"@type" "BreadcrumbList"
"@id" (printf "%s#breadcrumb" $page.Permalink)
"itemListElement" $items
) -}}
{{- end -}}
{{- end -}}
{{- if $page.IsPage -}}
{{- $image := partial "seo/image.html" (dict "Page" $page "Data" $data) -}}
{{- $pageSchema := dict
"@type" (index $pageClassification "jsonLdType")
"@id" (printf "%s#webpage" $page.Permalink)
"url" $page.Permalink
"name" $page.Title
"inLanguage" $language
"isPartOf" (dict "@id" $websiteID)
"author" (dict "@id" $authorID)
-}}
{{- with $description -}}
{{- $pageSchema = merge $pageSchema (dict "description" .) -}}
{{- end -}}
{{- with $image -}}
{{- $pageSchema = merge $pageSchema (dict "image" .) -}}
{{- end -}}
{{- with $page.Date -}}
{{- $pageSchema = merge $pageSchema (dict "datePublished" (.Format "2006-01-02T15:04:05Z07:00")) -}}
{{- end -}}
{{- with $page.Lastmod -}}
{{- $pageSchema = merge $pageSchema (dict "dateModified" (.Format "2006-01-02T15:04:05Z07:00")) -}}
{{- end -}}
{{- if eq (index $pageClassification "jsonLdType") "BlogPosting" -}}
{{- $pageSchema = merge $pageSchema (dict
"headline" $page.Title
"mainEntityOfPage" $page.Permalink
) -}}
{{- with $page.CurrentSection -}}
{{- if ne .RelPermalink "/" -}}
{{- $pageSchema = merge $pageSchema (dict "articleSection" .Title) -}}
{{- end -}}
{{- end -}}
{{- $keywords := slice -}}
{{- $taxonomyNames := slice -}}
{{- range $taxonomy, $_ := $page.Site.Taxonomies -}}
{{- $taxonomyNames = $taxonomyNames | append $taxonomy -}}
{{- end -}}
{{- range sort $taxonomyNames -}}
{{- $taxonomy := . -}}
{{- with $page.GetTerms $taxonomy -}}
{{- range sort . "LinkTitle" -}}
{{- $keyword := strings.TrimSpace (printf "%v" (default .Title .LinkTitle)) -}}
{{- if ne $keyword "" -}}
{{- $keywords = $keywords | append $keyword -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $keywords = uniq $keywords -}}
{{- if gt (len $keywords) 0 -}}
{{- $pageSchema = merge $pageSchema (dict "keywords" $keywords) -}}
{{- end -}}
{{- else if eq (index $pageClassification "jsonLdType") "Review" -}}
{{- $itemReviewed := dict
"@type" (index $pageClassification "reviewedType")
"name" $page.Title
-}}
{{- with $page.Params.wikidata_id -}}
{{- $itemReviewed = merge $itemReviewed (dict "sameAs" (printf "https://www.wikidata.org/wiki/%s" .)) -}}
{{- end -}}
{{- $pageSchema = merge $pageSchema (dict
"itemReviewed" $itemReviewed
"mainEntityOfPage" $page.Permalink
) -}}
{{- else if eq (index $pageClassification "jsonLdType") "Article" -}}
{{- $pageSchema = merge $pageSchema (dict
"headline" $page.Title
"mainEntityOfPage" $page.Permalink
) -}}
{{- end -}}
{{- $graph = $graph | append $pageSchema -}}
{{- else if not $page.IsHome -}}
{{- $pageSchema := dict
"@type" (index $pageClassification "jsonLdType")
"@id" (printf "%s#collection" $page.Permalink)
"url" $page.Permalink
"name" $page.Title
"inLanguage" $language
"isPartOf" (dict "@id" $websiteID)
-}}
{{- with $description -}}
{{- $pageSchema = merge $pageSchema (dict "description" .) -}}
{{- end -}}
{{- $graph = $graph | append $pageSchema -}}
{{- end -}}
{{- $payload := dict
"@context" "https://schema.org"
"@graph" $graph
-}}
<script type="application/ld+json">{{ $payload | jsonify | safeJS }}</script>
{{- end -}}