1
0
mirror of https://github.com/alanorth/hugo-theme-bootstrap4-blog.git synced 2024-11-17 17:57:04 +01:00

Add Schema.org implementation based on JSON-LD

This replaces the metadata that Hugo's own schema.html template had
been providing, but does so in JSON-LD notation rather than via the
use of <meta> tags (this is Google's currently recommended form of
specifying this markup). There are a few exceptions where I did not
follow the conventions used in Hugo's template, for example the use
of up to six images from a post's frontmatter, because Google's tool
only recognizes one image, as well as different logic for a post's
publish and modified dates (using enableGitInfo = true).

Using this new markup, Google's Structured Data Testing Tool is now
able to understand site metadata much better (before it was reading
none).

The implementation here is a mix of the elements and types from the
official Schema.org types—for example, Blog and BlogPosting—as well
as from Google's search documentation. Note that Google's docs are
geared towards AMP, where some metadata is required, while for non-
AMP pages the metadata is just recommended.

We will have to re-evaluate this in the future, for example to add
height and width information to image metadata.

See: https://schema.org/Blog
See: https://schema.org/BlogPosting
See: https://developers.google.com/search/docs/data-types/data-type-selector
See: https://search.google.com/structured-data/testing-tool
This commit is contained in:
Alan Orth 2017-01-20 15:56:33 +02:00
parent 4b768d9da4
commit 8ab05ae625
Signed by: alanorth
GPG Key ID: 0FB860CC9C45B1B9
4 changed files with 75 additions and 1 deletions

View File

@ -4,6 +4,10 @@
<head>
{{ partial "head-meta.html" . }}
{{ block "schema-dot-org" . }}
<!-- schema.org metadata differs for Nodes and Pages -->
{{ end }}
<link rel="canonical" href="{{ .Permalink }}">
<title>{{ block "title" . }}{{ .Site.Title }}{{ end }}</title>

View File

@ -1,3 +1,28 @@
{{ define "schema-dot-org" }}
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Blog",
{{/* Google recommends the headline be no more than 110 characters */}}
"headline": {{ substr .Site.Title 0 110 }},
"url" : {{ printf "%s" .Permalink }},
"author": {
"@type": "Person",
"name": {{ .Site.Params.author }}
},
{{ with .Site.Social.GooglePlus }}
"publisher": {{ printf "%s" . }},
{{ end }}
{{/* all of the site's categories/tags, from Hugo's tpl/template_embedded.go */}}
"keywords": "{{ range $plural, $terms := .Site.Taxonomies }}{{ range $term, $val := $terms }}{{ printf "%s," $term }}{{ end }}{{ end }}"
{{ with .Site.Params.description }}
,
"description": {{- . -}}
{{ end }}
}
</script>
{{ end }}
{{ define "main" }}
{{ $paginator := .Paginate (where .Data.Pages "Type" "post") }}

View File

@ -1,4 +1,50 @@
{{ define "title" }}{{ .Title | markdownify }} | {{ .Site.Title }}{{ end }}
{{ define "schema-dot-org" }}
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BlogPosting",
{{/* Google recommends the headline be no more than 110 characters */}}
"headline": {{ substr .Title 0 110 }},
{{- with .Params.images -}}{{ range first 1 . }}
"image": {
"@type": "ImageObject",
"url": {{ . | absURL }}
},
{{- end -}}{{ end }}
"url": {{ printf "%s" .Permalink }},
"wordCount": "{{ .WordCount }}",
{{ $ISO8601 := "2006-01-02T15:04:05-07:00" }}
{{ if not .PublishDate.IsZero }}
"datePublished": {{ .PublishDate.Format $ISO8601 }},
{{ else }}
"datePublished": {{ .Date.Format $ISO8601 }},
{{ end }}
{{ if not .Lastmod.IsZero }}
"dateModified": {{ .Lastmod.Format $ISO8601 }},
{{ end }}
{{ with .Site.Social.GooglePlus }}
"publisher": {{ printf "%s" . }},
{{ end }}
"author": {
"@type": "Person",
"name": {{ .Params.author | default .Site.Params.author }}
}
{{ if or (.Params.categories) (.Params.tags) }}
,
"keywords": "{{ with .Params.categories }}{{ range $i, $e := . }}{{ if $i }},{{ end }}{{ $e }}{{ end }},{{ end }}{{ with .Params.tags }}{{ range $i, $e := . }}{{ if $i }},{{ end }}{{ $e }}{{ end }}{{ end }}"
{{ end }}
{{- with .Params.description -}}
,
"description": {{ . }}
{{ end }}
}
</script>
{{ end }}
{{ define "main" }}
{{ $dateFormat := default "Mon Jan 2, 2006" (index .Site.Params "date_format") }}

View File

@ -2,7 +2,6 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{{ template "_internal/opengraph.html" . }}
{{ template "_internal/schema.html" . }}
{{ template "_internal/twitter_cards.html" . }}
{{- with .Site.Params.google_verify_meta -}}