Minor codestyle fixes to meta_breadcrumb. Moving safeJS to the partial output.

This commit is contained in:
Fredrik Jonsson 2023-04-23 20:59:21 +02:00
commit 86d7c8d001
2 changed files with 25 additions and 28 deletions

View file

@ -1,10 +1,10 @@
{{- $breadcrumbScratch := newScratch -}}
{{- $crumbScratch := newScratch -}}
{{- $crumbScratch.Set "itemListElement" (slice) -}}
{{- $breadPos := 0 -}}
{{- range .Ancestors.Reverse -}}
{{- $breadPos = (add $breadPos 1) -}}
{{- $crumbScratch.Add "itemListElement" (dict
{{ $breadcrumbScratch := newScratch -}}
{{ $crumbScratch := newScratch -}}
{{ $crumbScratch.Set "itemListElement" (slice) -}}
{{ $breadPos := 0 -}}
{{ range .Ancestors.Reverse -}}
{{ $breadPos = (add $breadPos 1) -}}
{{ $crumbScratch.Add "itemListElement" (dict
"@type" "ListItem"
"position" $breadPos
"item" (dict
@ -12,9 +12,9 @@
"name" .LinkTitle
)
) -}}
{{- end -}}
{{- $breadPos = (add $breadPos 1) -}}
{{- $crumbScratch.Add "itemListElement" (dict
{{ end -}}
{{ $breadPos = (add $breadPos 1) -}}
{{ $crumbScratch.Add "itemListElement" (dict
"@type" "ListItem"
"position" $breadPos
"item" (dict
@ -22,8 +22,8 @@
"name" .LinkTitle
)
) -}}
{{- $breadcrumbScratch.SetInMap "breadcrumb" "@context" "https://schema.org" -}}
{{- $breadcrumbScratch.SetInMap "breadcrumb" "@type" "BreadcrumbList" -}}
{{- $breadcrumbScratch.SetInMap "breadcrumb" "itemListElement" ($crumbScratch.Get "itemListElement") -}}
{{- $jsonifiedBreadcrumb := $breadcrumbScratch.Get "breadcrumb" | jsonify (dict "indent" " " "prefix" " ") -}}
{{- return $jsonifiedBreadcrumb -}}
{{ $breadcrumbScratch.SetInMap "breadcrumb" "@context" "https://schema.org" -}}
{{ $breadcrumbScratch.SetInMap "breadcrumb" "@type" "BreadcrumbList" -}}
{{ $breadcrumbScratch.SetInMap "breadcrumb" "itemListElement" ($crumbScratch.Get "itemListElement") -}}
{{ $jsonifiedBreadcrumb := $breadcrumbScratch.Get "breadcrumb" | jsonify (dict "indent" " " "prefix" " ") -}}
{{ return ($jsonifiedBreadcrumb | safeJS) -}}

View file

@ -1,17 +1,14 @@
<script type="application/ld+json">
{{- $blogSections := site.Params.blogSections | default (slice "blog" "post") }}
{{- $breadcrumbSections := site.Params.breadcrumbSections | default (slice) }}
{{ $blogSections := site.Params.blogSections | default (slice "blog" "post") -}}
{{ $breadcrumbSections := site.Params.breadcrumbSections | default (slice) -}}
{
"@context": "https://schema.org",
{{ if .IsPage -}}
"@type": {{ if in $blogSections .Section -}}
"BlogPosting",
{{- else -}}
"WebPage",
{{- if in $breadcrumbSections .Section }}
"breadcrumb": {{ safeJS (partial "meta_breadcrumb_json_ld.html" .) }},
{{- end -}}
{{- end }}
"@type": {{ if in $blogSections .Section }}"BlogPosting",{{ else }}"WebPage",
{{ if in $breadcrumbSections .Section -}}
"breadcrumb": {{ partial "meta_breadcrumb_json_ld.html" . }},
{{ end -}}
{{ end -}}
"headline": {{ .Title }},
"datePublished": {{ .Date.Format "2006-01-02T15:04:05Z07:00" }},
"dateModified": {{ .Lastmod.Format "2006-01-02T15:04:05Z07:00" }},
@ -24,9 +21,9 @@
{{ end -}}
{{ else -}}
"@type": "WebPage",
{{- if in $breadcrumbSections .Section }}
"breadcrumb": {{ safeJS (partial "meta_breadcrumb_json_ld.html" .) }},
{{- end }}
{{ if in $breadcrumbSections .Section -}}
"breadcrumb": {{ partial "meta_breadcrumb_json_ld.html" . }},
{{ end -}}
"url" : {{ .Permalink }},
"name": {{ .Title }},
{{ with $.Param "description" -}}