Skip to content

Commit

Permalink
1. Removed microdata and unified use of JSON-LD.
Browse files Browse the repository at this point in the history
2. Add alternateName field to webSite schema. That is an alternate site name
  used in Google search results.
3. Fix missing field in google rich result test.
4. Prioritize using the featured image in front matter as the article image in JSON-LD.
  • Loading branch information
wu0407 committed Dec 19, 2023
1 parent 2e6c4ab commit 1b99f14
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 65 deletions.
4 changes: 4 additions & 0 deletions exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ cacheRemoteImages = false
# thumbnail URL
# 缩略图 URL
thumbnailUrl = "/images/screenshot.png"
# Google 搜索结果中网站备用名字
# alternate site name in Google search result
# https://developers.google.com/search/docs/appearance/site-names#alternative
# alternateName = ["Hugo DoIt"]

# Analytics config
# 网站分析配置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,9 @@ Please open the code block below to view the complete sample configuration :(far
image = ""
# thumbnail URL
thumbnailUrl = ""
# {{< version 0.4.1 >}}
# alternate site name in Google search result
alternateName = ["Hugo DoIt"]

# {{< version 0.2.0 >}} Analytics config
[params.analytics]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,9 @@ hugo
image = ""
# 缩略图 URL
thumbnailUrl = ""
# {{< version 0.4.1 >}}
# Google 搜索结果中网站备用名字
alternateName = ["Hugo DoIt"]

# {{< version 0.2.0 >}} 网站分析配置
[params.analytics]
Expand Down
4 changes: 2 additions & 2 deletions layouts/_default/summary.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{- $params := .Params | merge .Site.Params.page -}}

<article class="single summary" itemscope itemtype="http://schema.org/Article">
<article class="single summary">
{{- /* Featured image */ -}}
{{- $image := $params.featuredImagePreview | default $params.featuredImage -}}
{{- $height := "auto" -}}
Expand Down Expand Up @@ -29,7 +29,7 @@
{{- end -}}

{{- /* Title */ -}}
<h1 class="single-title" itemprop="name headline">
<h1 class="single-title">
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
</h1>

Expand Down
8 changes: 3 additions & 5 deletions layouts/partials/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@
{{- if ne .Site.Params.footer.copyright false -}}
<i class="far fa-copyright fa-fw"></i>
{{- with .Site.Params.footer.since -}}
<span itemprop="copyrightYear">
{{- if lt . now.Year }}{{ . }} - {{ end }}{{ now.Year -}}
</span>
{{- if lt . now.Year }}{{ . }} - {{ end }}{{ now.Year -}}
{{- else -}}
<span itemprop="copyrightYear">{{ now.Year }}</span>
{{ now.Year }}
{{- end -}}
{{- end -}}

{{- /* Author */ -}}
{{- if ne .Site.Params.footer.author false -}}
<span class="author" itemprop="copyrightHolder">&nbsp;<a href="{{ $.Site.Author.link | default .Site.Home.RelPermalink }}" target="_blank" rel="noopener noreferrer">{{ .Site.Author.name }}</a></span>
<span class="author">&nbsp;<a href="{{ $.Site.Author.link | default .Site.Home.RelPermalink }}" target="_blank" rel="noopener noreferrer">{{ .Site.Author.name }}</a></span>
{{- end -}}

{{- /* License */ -}}
Expand Down
176 changes: 118 additions & 58 deletions layouts/partials/head/seo.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{{- $params := .Scratch.Get "params" -}}

{{- with .Site.Params.verification.google -}}
<meta name="google-site-verification" content="{{ . }}" />
{{- end -}}
Expand All @@ -22,57 +20,13 @@
<meta name="360-site-verification" content="{{ . }}" />
{{- end -}}

{{- /* Home SEO */ -}}
{{- if .IsHome -}}
<script type="application/ld+json">
{{- define "blogPosting" -}}
{{- $params := .Scratch.Get "params" -}}
{{- with .Page -}}
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": {{ .Permalink }},
{{- with .Site.LanguageCode -}}
"inLanguage": "{{ . }}",
{{- end -}}
{{- with .Site.Author.name -}}
"author": {
"@type": "Person",
"name": {{ . | safeHTML }}
},
{{- end -}}
{{- with .Site.Params.description -}}
"description": {{ . | safeHTML }},
{{- if not $.IsHome | and .IsPage -}}
"@context": "https://schema.org",
{{- end -}}
{{- $image := .Site.Params.seo.image -}}
{{- with dict "Path" $image "Resources" .Resources | partial "function/resource.html" -}}
"image": {
"@type": "ImageObject",
"url": {{ .Permalink }},
"width": {{ .Width }},
"height": {{ .Height }}
},
{{- else -}}
{{- with $image -}}
"image": {{ . | absURL }},
{{- end -}}
{{- end -}}
{{- with .Site.Params.seo.thumbnailUrl -}}
{{- with dict "Path" . "Resources" $.Resources | partial "function/resource.html" -}}
"thumbnailUrl": {{ .Permalink }},
{{- else -}}
"thumbnailUrl": {{ . | absURL }},
{{- end -}}
{{- end -}}
{{- with .Site.Copyright -}}
"license": "{{ . | safeHTML }}",
{{- end -}}
"name": {{ .Site.Title | safeHTML }}
}
</script>

{{- /* Page SEO */ -}}
{{- else if .IsPage -}}
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BlogPosting",
"headline": {{ .Title | safeHTML }},
"inLanguage": "{{ .Site.LanguageCode }}",
Expand All @@ -81,6 +35,14 @@
"@id": {{ .Permalink }}
},
{{- $images := $params.seo.images | default slice -}}
{{- if not $images -}}
{{- with $params.featuredImage -}}
{{- $images = slice $params.featuredImage -}}
{{- end -}}
{{- with $params.featuredImagePreview -}}
{{- $images = $images | append $params.featuredImagePreview -}}
{{- end -}}
{{- end -}}
{{- if not $images -}}
{{- with .Resources.GetMatch "featured-image-preview" -}}
{{- $images = slice "featured-image-preview" -}}
Expand Down Expand Up @@ -112,8 +74,10 @@
],
{{- end -}}
"genre": "{{ .Type }}",
{{- with .Params.tags -}}
"keywords": "{{ delimit . ", " }}",
{{- if .Params.keywords -}}
"keywords": {{- apply .Params.keywords "safeHTML" "." -}},
{{- else if .Params.tags -}}
"keywords": {{- apply .Params.tags "safeHTML" "." -}},
{{- end -}}
"wordcount": {{ .WordCount }},
"url": {{ .Permalink }},
Expand Down Expand Up @@ -148,35 +112,131 @@
{{- end -}}
},
{{- if .Params.authors -}}
"authors": [
"author": [
{{- $lang := ( $.Params.lang | default $.Lang ) -}}
{{- range $i, $name := .Params.authors -}}
{{- if $.Site.Data.author -}}
{{- if $.Site.Data.authors -}}
{{- with partial "function/author.html" (dict "name" $name "author" (index $.Site.Data.authors $name) "lang" $lang) -}}
{{- if gt $i 0 }},{{ end -}}
{
"@type": "Person",
"name": "{{ .name }}"
"name": "{{ .name }}",
"url": {{ .absLink }}
}
{{- end -}}
{{- else -}}
{{- with partial "function/author.html" (dict "name" $name "lang" $lang) -}}
{{- if gt $i 0 }},{{ end -}}
{
"@type": "Person",
"name": "{{ $name }}"
"name": "{{ $name }}",
"url": {{ .link | absURL }}
}
{{- end -}}
{{- end -}}
{{- end -}}
],
{{- else -}}
{{- $link := .Params.authorlink | default .Site.Author.link | default .Site.Home.RelPermalink -}}
{{- with .Params.author | default .Site.Author.name | default (T "author") -}}
"author": {
"@type": "Person",
"name": {{ . | safeHTML }}
"name": {{ . | safeHTML }},
"url": {{ $link }}
},
{{- end -}}
{{- end -}}
"description": {{ .Description | safeHTML }}
}
{{- end -}}
{{- end -}}


{{- /* Home SEO */ -}}
{{- if .IsHome -}}
{{- $showBlogPosting := and .Site.RegularPages .Site.Params.home.posts.enable -}}
{{- $paginatorPager := .Scratch.Get "paginatorPager" -}}
{{- $showWebSite := and $paginatorPager (eq $paginatorPager.PageNumber 1) (eq $paginatorPager.URL "/") -}}
{{- if or $showBlogPosting $showWebSite -}}
<script type="application/ld+json">
{"@context": "https://schema.org",
"@graph": [
{{- if and (eq $paginatorPager.PageNumber 1) (eq $paginatorPager.URL "/") -}}
{
"@type": "WebSite",
"url": {{ .Permalink }},
{{- with .Site.LanguageCode -}}
"inLanguage": "{{ . }}",
{{- end -}}
{{- with .Site.Author.name -}}
"author": {
"@type": "Person",
"name": {{ . | safeHTML }}
},
{{- end -}}
{{- with .Site.Params.description -}}
"description": {{ . | safeHTML }},
{{- end -}}
{{- $image := .Site.Params.seo.image -}}
{{- with dict "Path" $image "Resources" .Resources | partial "function/resource.html" -}}
"image": {
"@type": "ImageObject",
"url": {{ .Permalink }},
"width": {{ .Width }},
"height": {{ .Height }}
},
{{- else -}}
{{- with $image -}}
"image": {{ . | absURL }},
{{- end -}}
{{- end -}}
{{- with .Site.Params.seo.thumbnailUrl -}}
{{- with dict "Path" . "Resources" $.Resources | partial "function/resource.html" -}}
"thumbnailUrl": {{ .Permalink }},
{{- else -}}
"thumbnailUrl": {{ . | absURL }},
{{- end -}}
{{- end -}}
{{- with .Site.Copyright -}}
"license": "{{ . | safeHTML }}",
{{- end -}}
{{- with .Site.Params.seo.alternateName -}}
"alternateName": {{- apply . "safeHTML" "." -}},
{{- end -}}
{{- with .Site.Params.footer.since -}}
"copyrightYear": {{ . }},
{{- else -}}
"copyrightYear": {{ now.Year }},
{{- end -}}
{{- if .Site.Params.footer.author -}}
"copyrightHolder": {
"@type": "Person",
"name": {{ .Site.Params.footer.author | safeHTML }}
},
{{- else if .Site.Author.name -}}
"copyrightHolder": {
"@type": "Person",
"name": {{ .Site.Author.name | safeHTML }}
},
{{- end -}}
"name": {{ .Site.Title | safeHTML }}
}
{{- if $showBlogPosting -}},{{- end -}}
{{- end -}}
{{- if $showBlogPosting -}}
{{- /* Paginate is first call on baseof.html */ -}}
{{- range $index, $page := $paginatorPager.Pages -}}
{{- if gt $index 0 -}},{{- end -}}
{{- template "blogPosting" dict "IsHome" $.IsHome "Page" $page -}}
{{- end -}}
{{- end -}}
]}
</script>
{{- end -}}

{{- /* Page SEO */ -}}
{{- else if .IsPage -}}
<script type="application/ld+json">
{{- template "blogPosting" . -}}
</script>
{{- end -}}

0 comments on commit 1b99f14

Please sign in to comment.