]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl/tplimpl: Update schema template
authorJoe Mooring <joe.mooring@veriphor.com>
Sat, 23 Mar 2024 23:34:39 +0000 (16:34 -0700)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 28 Mar 2024 13:56:02 +0000 (14:56 +0100)
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>

hugolib/embedded_templates_test.go
tpl/tplimpl/embedded/templates/schema.html

index c409b0fbd35f6a4ab19c3799560a60d65281d07d..5ef92597c5a3541b105ff2aaf5e6b2ed76fd14ce 100644 (file)
@@ -68,9 +68,9 @@ title: My Site
 <meta property="article:published_time" content="2021-02-26T18:02:00-01:00" />
 <meta property="article:modified_time" content="2021-05-22T19:25:00-01:00" />
 <meta itemprop="name" content="My Bundle">
-<meta itemprop="image" content="https://example.org/mybundle/featured-sunset.jpg" />
-<meta itemprop="datePublished" content="2021-02-26T18:02:00-01:00" />
-<meta itemprop="dateModified" content="2021-05-22T19:25:00-01:00" />
+<meta itemprop="image" content="https://example.org/mybundle/featured-sunset.jpg">
+<meta itemprop="datePublished" content="2021-02-26T18:02:00-01:00">
+<meta itemprop="dateModified" content="2021-05-22T19:25:00-01:00">
 
 `)
        b.AssertFileContent("public/mypage/index.html", `
@@ -81,17 +81,17 @@ title: My Site
 <meta property="og:image" content="https://example.org/mypage/sample.jpg" />
 <meta property="article:published_time" content="2021-02-26T18:02:00+01:00" />
 <meta property="article:modified_time" content="2021-05-22T19:25:00+01:00" />
-<meta itemprop="image" content="https://example.org/pageimg1.jpg" />
-<meta itemprop="image" content="https://example.org/pageimg2.jpg" />
-<meta itemprop="image" content="https://example.local/logo.png" />
-<meta itemprop="image" content="https://example.org/mypage/sample.jpg" />
-<meta itemprop="datePublished" content="2021-02-26T18:02:00+01:00" />
-<meta itemprop="dateModified" content="2021-05-22T19:25:00+01:00" />
+<meta itemprop="image" content="https://example.org/pageimg1.jpg">
+<meta itemprop="image" content="https://example.org/pageimg2.jpg">
+<meta itemprop="image" content="https://example.local/logo.png">
+<meta itemprop="image" content="https://example.org/mypage/sample.jpg">
+<meta itemprop="datePublished" content="2021-02-26T18:02:00+01:00">
+<meta itemprop="dateModified" content="2021-05-22T19:25:00+01:00">
 `)
        b.AssertFileContent("public/mysite/index.html", `
 <meta name="twitter:image" content="https://example.org/siteimg1.jpg" />
 <meta property="og:image" content="https://example.org/siteimg1.jpg" />
-<meta itemprop="image" content="https://example.org/siteimg1.jpg" />
+<meta itemprop="image" content="https://example.org/siteimg1.jpg">
 `)
 }
 
index 8a55afe804e369e9458a46f21c9d23ba74cf7558..c4e89abd6e94e013a18d77abf7e7b4c4019df3fa 100644 (file)
@@ -1,17 +1,56 @@
-<meta itemprop="name" content="{{ .Title }}">
-<meta itemprop="description" content="{{ with .Description }}{{ . }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end }}">
-
-{{- if .IsPage -}}
-{{- $iso8601 := "2006-01-02T15:04:05-07:00" -}}
-{{ with .PublishDate }}<meta itemprop="datePublished" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }} />{{ end}}
-{{ with .Lastmod }}<meta itemprop="dateModified" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }} />{{ end}}
-<meta itemprop="wordCount" content="{{ .WordCount }}">
-
-{{- $images := partial "_funcs/get-page-images" . -}}
-{{- range first 6 $images -}}
-  <meta itemprop="image" content="{{ .Permalink }}" />
-{{- end -}}
+{{- with or .Title site.Title }}
+  <meta itemprop="name" content="{{ . }}">
+{{- end }}
+
+{{- with or .Description .Summary site.Params.Description }}
+  <meta itemprop="description" content="{{ . }}">
+{{- end }}
+
+{{- $ISO8601 := "2006-01-02T15:04:05-07:00" }}
+{{- with .PublishDate }}
+  <meta itemprop="datePublished" {{ .Format $ISO8601 | printf "content=%q" | safeHTMLAttr }}>
+{{- end }}
+
+{{- with .Lastmod }}
+  <meta itemprop="dateModified" {{ .Format $ISO8601 | printf "content=%q" | safeHTMLAttr }}>
+{{- end }}
+
+{{- with .WordCount }}
+  <meta itemprop="wordCount" content="{{ . }}">
+{{- end }}
+
+{{- $images := partial "_funcs/get-page-images" . }}
+{{- range first 6 $images }}
+  <meta itemprop="image" content="{{ .Permalink }}">
+{{- end }}
+
+{{- /*
+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.
 
-<!-- Output all taxonomies as schema.org keywords -->
-<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 }}" />
+*/}}
+{{- $keywords := slice }}
+{{- range .GetTerms "keywords" }}
+  {{- $keywords = $keywords | append .Title }}
+{{- else }}
+  {{- with .Keywords }}
+    {{- $keywords = . }}
+  {{- else }}
+    {{- range .GetTerms "tags" }}
+      {{- $keywords = $keywords | append .Title }}
+    {{- else }}
+      {{- range $taxonomy, $_ := site.Taxonomies }}
+        {{- range $.GetTerms $taxonomy }}
+          {{- $keywords = $keywords | append .Title }}
+        {{- end }}
+      {{- end }}
+    {{- end }}
+  {{- end }}
+{{- end }}
+{{- with $keywords }}
+  <meta itemprop="keywords" content="{{ delimit . `,` }}">
 {{- end -}}