Skip to content

Commit 74ce5dc

Browse files
jmooringbep
authored andcommitted
tpl/tplimpl: Update schema template
Changes: - Remove trailing comma from list of keywords. - Improve keywords precedence: 1. Use "keywords" term page titles. 2. Use "keywords" from front matter if "keywords" is not a taxonomy. 3. Use "tags" term page titles. 4. Use term page titles from all taxonomies. - Enable schema for all page kinds, previously limited to kind = page. - Remove trailing slashes from void elements. - Improve readability. Closes #7570 Co-authored by: 0urobor0s <0urobor0s@users.noreply.github.com>
1 parent 54a8f0c commit 74ce5dc

File tree

2 files changed

+64
-25
lines changed

2 files changed

+64
-25
lines changed

hugolib/embedded_templates_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ title: My Site
6868
<meta property="article:published_time" content="2021-02-26T18:02:00-01:00" />
6969
<meta property="article:modified_time" content="2021-05-22T19:25:00-01:00" />
7070
<meta itemprop="name" content="My Bundle">
71-
<meta itemprop="image" content="https://example.org/mybundle/featured-sunset.jpg" />
72-
<meta itemprop="datePublished" content="2021-02-26T18:02:00-01:00" />
73-
<meta itemprop="dateModified" content="2021-05-22T19:25:00-01:00" />
71+
<meta itemprop="image" content="https://example.org/mybundle/featured-sunset.jpg">
72+
<meta itemprop="datePublished" content="2021-02-26T18:02:00-01:00">
73+
<meta itemprop="dateModified" content="2021-05-22T19:25:00-01:00">
7474
7575
`)
7676
b.AssertFileContent("public/mypage/index.html", `
@@ -81,17 +81,17 @@ title: My Site
8181
<meta property="og:image" content="https://example.org/mypage/sample.jpg" />
8282
<meta property="article:published_time" content="2021-02-26T18:02:00+01:00" />
8383
<meta property="article:modified_time" content="2021-05-22T19:25:00+01:00" />
84-
<meta itemprop="image" content="https://example.org/pageimg1.jpg" />
85-
<meta itemprop="image" content="https://example.org/pageimg2.jpg" />
86-
<meta itemprop="image" content="https://example.local/logo.png" />
87-
<meta itemprop="image" content="https://example.org/mypage/sample.jpg" />
88-
<meta itemprop="datePublished" content="2021-02-26T18:02:00+01:00" />
89-
<meta itemprop="dateModified" content="2021-05-22T19:25:00+01:00" />
84+
<meta itemprop="image" content="https://example.org/pageimg1.jpg">
85+
<meta itemprop="image" content="https://example.org/pageimg2.jpg">
86+
<meta itemprop="image" content="https://example.local/logo.png">
87+
<meta itemprop="image" content="https://example.org/mypage/sample.jpg">
88+
<meta itemprop="datePublished" content="2021-02-26T18:02:00+01:00">
89+
<meta itemprop="dateModified" content="2021-05-22T19:25:00+01:00">
9090
`)
9191
b.AssertFileContent("public/mysite/index.html", `
9292
<meta name="twitter:image" content="https://example.org/siteimg1.jpg" />
9393
<meta property="og:image" content="https://example.org/siteimg1.jpg" />
94-
<meta itemprop="image" content="https://example.org/siteimg1.jpg" />
94+
<meta itemprop="image" content="https://example.org/siteimg1.jpg">
9595
`)
9696
}
9797

+54-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,56 @@
1-
<meta itemprop="name" content="{{ .Title }}">
2-
<meta itemprop="description" content="{{ with .Description }}{{ . }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end }}">
3-
4-
{{- if .IsPage -}}
5-
{{- $iso8601 := "2006-01-02T15:04:05-07:00" -}}
6-
{{ with .PublishDate }}<meta itemprop="datePublished" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }} />{{ end}}
7-
{{ with .Lastmod }}<meta itemprop="dateModified" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }} />{{ end}}
8-
<meta itemprop="wordCount" content="{{ .WordCount }}">
9-
10-
{{- $images := partial "_funcs/get-page-images" . -}}
11-
{{- range first 6 $images -}}
12-
<meta itemprop="image" content="{{ .Permalink }}" />
13-
{{- end -}}
1+
{{- with or .Title site.Title }}
2+
<meta itemprop="name" content="{{ . }}">
3+
{{- end }}
4+
5+
{{- with or .Description .Summary site.Params.Description }}
6+
<meta itemprop="description" content="{{ . }}">
7+
{{- end }}
8+
9+
{{- $ISO8601 := "2006-01-02T15:04:05-07:00" }}
10+
{{- with .PublishDate }}
11+
<meta itemprop="datePublished" {{ .Format $ISO8601 | printf "content=%q" | safeHTMLAttr }}>
12+
{{- end }}
13+
14+
{{- with .Lastmod }}
15+
<meta itemprop="dateModified" {{ .Format $ISO8601 | printf "content=%q" | safeHTMLAttr }}>
16+
{{- end }}
17+
18+
{{- with .WordCount }}
19+
<meta itemprop="wordCount" content="{{ . }}">
20+
{{- end }}
21+
22+
{{- $images := partial "_funcs/get-page-images" . }}
23+
{{- range first 6 $images }}
24+
<meta itemprop="image" content="{{ .Permalink }}">
25+
{{- end }}
26+
27+
{{- /*
28+
Keywords precedence:
29+
30+
1. Use "keywords" term page titles.
31+
2. Use "keywords" from front matter if "keywords" is not a taxonomy.
32+
3. Use "tags" term page titles.
33+
4. Use term page titles from all taxonomies.
1434

15-
<!-- Output all taxonomies as schema.org keywords -->
16-
<meta itemprop="keywords" content="{{ if .IsPage}}{{ range $index, $tag := .Params.tags }}{{ $tag }},{{ end }}{{ else }}{{ range $plural, $terms := .Site.Taxonomies }}{{ range $term, $val := $terms }}{{ printf "%s," $term }}{{ end }}{{ end }}{{ end }}" />
35+
*/}}
36+
{{- $keywords := slice }}
37+
{{- range .GetTerms "keywords" }}
38+
{{- $keywords = $keywords | append .Title }}
39+
{{- else }}
40+
{{- with .Keywords }}
41+
{{- $keywords = . }}
42+
{{- else }}
43+
{{- range .GetTerms "tags" }}
44+
{{- $keywords = $keywords | append .Title }}
45+
{{- else }}
46+
{{- range $taxonomy, $_ := site.Taxonomies }}
47+
{{- range $.GetTerms $taxonomy }}
48+
{{- $keywords = $keywords | append .Title }}
49+
{{- end }}
50+
{{- end }}
51+
{{- end }}
52+
{{- end }}
53+
{{- end }}
54+
{{- with $keywords }}
55+
<meta itemprop="keywords" content="{{ delimit . `,` }}">
1756
{{- end -}}

0 commit comments

Comments
 (0)