WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 548cbda

Browse files
committed
site: improve schema.org json-ld markup
Signed-off-by: David Karlsson <[email protected]>
1 parent a55bf1b commit 548cbda

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

layouts/partials/meta.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@
5858
itemprop="datePublished"
5959
content="{{ .PublishDate | default .Lastmod }}"
6060
/>
61-
<script type="application/ld+json">{"@context":"https://schema.org","@type":"WebPage","headline":{{ .LinkTitle | jsonify }},"description":{{ $description | jsonify }},"url":"{{ .Permalink }}"}</script>
61+
{{ partial "schema.html" . }}

layouts/partials/schema.html

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
{{- $description := partial "utils/description.html" . -}}
2+
{{- $keywords := delimit (partialCached "utils/keywords.html" . .) ", " -}}
3+
4+
{{- /* Build TechArticle schema for content pages */ -}}
5+
{{- $schema := dict
6+
"@context" "https://schema.org"
7+
"@type" "TechArticle"
8+
"headline" .LinkTitle
9+
"description" $description
10+
"url" .Permalink
11+
-}}
12+
13+
{{- /* Add dates (from Git via enableGitInfo) */ -}}
14+
{{- with .PublishDate -}}
15+
{{- $schema = merge $schema (dict "datePublished" (time.Format "2006-01-02T15:04:05Z07:00" .)) -}}
16+
{{- end -}}
17+
{{- with .Lastmod -}}
18+
{{- $schema = merge $schema (dict "dateModified" (time.Format "2006-01-02T15:04:05Z07:00" .)) -}}
19+
{{- end -}}
20+
21+
{{- /* Add author and publisher */ -}}
22+
{{- $logoUrl := printf "%sassets/images/docker-logo.png" (strings.TrimSuffix "/" site.BaseURL | printf "%s/") -}}
23+
{{- $org := dict
24+
"@type" "Organization"
25+
"name" "Docker Inc"
26+
"url" "https://www.docker.com"
27+
-}}
28+
{{- $schema = merge $schema (dict "author" $org "publisher" (merge $org (dict "logo" (dict "@type" "ImageObject" "url" $logoUrl)))) -}}
29+
30+
{{- /* Add article section (from Hugo section) */ -}}
31+
{{- with .Section -}}
32+
{{- $schema = merge $schema (dict "articleSection" .) -}}
33+
{{- end -}}
34+
35+
{{- /* Add keywords if present */ -}}
36+
{{- with $keywords -}}
37+
{{- $schema = merge $schema (dict "keywords" .) -}}
38+
{{- end -}}
39+
40+
{{- /* Add time required if specified in frontmatter */ -}}
41+
{{- with .Params.time -}}
42+
{{- /* Convert "20 minutes" to ISO 8601 duration "PT20M" */ -}}
43+
{{- $duration := . -}}
44+
{{- $isoDuration := "" -}}
45+
{{- if findRE `(\d+)\s*minutes?` $duration -}}
46+
{{- $mins := index (findRE `\d+` $duration) 0 -}}
47+
{{- $isoDuration = printf "PT%sM" $mins -}}
48+
{{- else if findRE `(\d+)\s*hours?` $duration -}}
49+
{{- $hours := index (findRE `\d+` $duration) 0 -}}
50+
{{- $isoDuration = printf "PT%sH" $hours -}}
51+
{{- end -}}
52+
{{- with $isoDuration -}}
53+
{{- $schema = merge $schema (dict "timeRequired" .) -}}
54+
{{- end -}}
55+
{{- end -}}
56+
57+
{{- /* Add isPartOf relationship to parent section */ -}}
58+
{{- with .Parent -}}
59+
{{- if not .IsHome -}}
60+
{{- $isPartOf := dict
61+
"@type" "WebPage"
62+
"@id" .Permalink
63+
"name" .LinkTitle
64+
-}}
65+
{{- $schema = merge $schema (dict "isPartOf" $isPartOf) -}}
66+
{{- end -}}
67+
{{- end -}}
68+
69+
{{- /* Output the schema as JSON-LD */ -}}
70+
<script type="application/ld+json">{{ $schema | jsonify | safeJS }}</script>
71+
72+
{{- /* Add BreadcrumbList schema */ -}}
73+
{{- $breadcrumbs := slice -}}
74+
{{- $position := 1 -}}
75+
{{- range .Ancestors.Reverse -}}
76+
{{- if not .IsHome -}}
77+
{{- $item := dict
78+
"@type" "ListItem"
79+
"position" $position
80+
"name" .LinkTitle
81+
"item" .Permalink
82+
-}}
83+
{{- $breadcrumbs = $breadcrumbs | append $item -}}
84+
{{- $position = add $position 1 -}}
85+
{{- end -}}
86+
{{- end -}}
87+
88+
{{- /* Add current page to breadcrumbs */ -}}
89+
{{- $currentItem := dict
90+
"@type" "ListItem"
91+
"position" $position
92+
"name" .LinkTitle
93+
"item" .Permalink
94+
-}}
95+
{{- $breadcrumbs = $breadcrumbs | append $currentItem -}}
96+
97+
{{- /* Only output breadcrumbs if there's more than just the current page */ -}}
98+
{{- if gt (len $breadcrumbs) 1 -}}
99+
<script type="application/ld+json">
100+
{{- dict
101+
"@context" "https://schema.org"
102+
"@type" "BreadcrumbList"
103+
"itemListElement" $breadcrumbs
104+
| jsonify | safeJS -}}
105+
</script>
106+
{{- end -}}

0 commit comments

Comments
 (0)