Skip to content

Commit cb99cda

Browse files
committed
feat: refine JSON-LD schema for posts and pages
- Updated `@type` logic to differentiate between `WebPage` and `CollectionPage`. - Enhanced `BlogPosting` with `description`, `keywords`, and enriched `image` metadata. - Improved logic for linking `mainEntity` to the associated article.
1 parent 975f42a commit cb99cda

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

themes/custom/layouts/partials/schema-jsonld.html

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,13 @@
162162
{{- $pageURL := .Permalink -}}
163163
{{- $webpageId := printf "%s#webpage" $pageURL -}}
164164
{{- $webpageName := .Title | default $siteTitle -}}
165+
{{- $isPostList := or .IsHome (and (eq .Kind "section") (eq .Section "posts")) -}}
166+
{{- $webpageType := "WebPage" -}}
167+
{{- if $isPostList -}}
168+
{{- $webpageType = "CollectionPage" -}}
169+
{{- end -}}
165170
{{- $webpageNode := dict
166-
"@type" "WebPage"
171+
"@type" $webpageType
167172
"@id" $webpageId
168173
"url" $pageURL
169174
"name" $webpageName
@@ -173,7 +178,6 @@
173178
{{- $webpageNode = merge $webpageNode (dict "inLanguage" $languageCode) -}}
174179
{{- end -}}
175180

176-
{{- $isPostList := or .IsHome (and (eq .Kind "section") (eq .Section "posts")) -}}
177181
{{- if $isPostList -}}
178182
{{- $pageSize := 5 -}}
179183
{{- $posts := where .Site.RegularPages "Section" "posts" -}}
@@ -200,8 +204,6 @@
200204
{{- end -}}
201205
{{- end -}}
202206

203-
{{- $nodes = $nodes | append $webpageNode -}}
204-
205207
{{- if and .IsPage (eq .Section "posts") -}}
206208
{{- $imageURL := "" -}}
207209
{{- with .Params.cover.image -}}
@@ -226,18 +228,36 @@
226228
"isPartOf" (dict "@id" $blogId)
227229
"publisher" (dict "@id" $orgId)
228230
-}}
231+
{{- $postDescription := .Params.description | default .Summary -}}
232+
{{- if $postDescription -}}
233+
{{- $blogPosting = merge $blogPosting (dict "description" ($postDescription | plainify | htmlUnescape)) -}}
234+
{{- end -}}
235+
{{- with .Params.tags -}}
236+
{{- if gt (len .) 0 -}}
237+
{{- $blogPosting = merge $blogPosting (dict "keywords" .) -}}
238+
{{- end -}}
239+
{{- end -}}
229240
{{- if $personName -}}
230241
{{- $blogPosting = merge $blogPosting (dict "author" (dict "@id" $personId)) -}}
231242
{{- end -}}
232243
{{- if $hasModified -}}
233244
{{- $blogPosting = merge $blogPosting (dict "dateModified" (.Lastmod.Format "2006-01-02T15:04:05Z07:00")) -}}
234245
{{- end -}}
235246
{{- if $imageURL -}}
236-
{{- $blogPosting = merge $blogPosting (dict "image" $imageURL) -}}
247+
{{- $imageObject := dict "@type" "ImageObject" "url" $imageURL -}}
248+
{{- with .Params.cover.image -}}
249+
{{- with $.Resources.GetMatch . -}}
250+
{{- $imageObject = merge $imageObject (dict "width" .Width "height" .Height) -}}
251+
{{- end -}}
252+
{{- end -}}
253+
{{- $blogPosting = merge $blogPosting (dict "image" $imageObject) -}}
237254
{{- end -}}
255+
{{- $webpageNode = merge $webpageNode (dict "mainEntity" (dict "@id" (printf "%s#article" .Permalink))) -}}
238256
{{- $nodes = $nodes | append $blogPosting -}}
239257
{{- end -}}
240258

259+
{{- $nodes = $nodes | append $webpageNode -}}
260+
241261
{{- $graph := dict
242262
"@context" "https://schema.org"
243263
"@graph" $nodes

0 commit comments

Comments
 (0)