diff options
| author | Aditya Telange <21258296+adityatelange@users.noreply.github.com> | 2020-10-15 10:21:33 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-15 10:21:33 +0530 |
| commit | fb945ba3c1c1317bb70eba9d998cc6d690d077d5 (patch) | |
| tree | 7beea11692d300078558adb02ee6a667edecc3d6 | |
| parent | 95e6bf1e135b4acaf4b14b6ae9592d968bbfc231 (diff) | |
json-schema: add support for Rich Results: Json schema for structured data (#36)
* json-schema: init
* sameAs can be set through .Site.Params.schema.sameAs
* if it is not specified but social-icons exsists, we use social-icons for sameAs vars
* .Site.Params.schema.sameAs overrides social-icons
* publisherType can be changed via .Site.Params.schema.publisherType
| -rw-r--r-- | layouts/partials/head.html | 4 | ||||
| -rw-r--r-- | layouts/partials/templates/schema.html | 31 | ||||
| -rw-r--r-- | layouts/partials/templates/schema_json.html | 65 |
3 files changed, 67 insertions, 33 deletions
diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 55fa132..1f78628 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -51,5 +51,5 @@ {{- template "_internal/google_analytics_async.html" . }} {{- template "partials/templates/opengraph.html" . }} {{- template "partials/templates/twitter_cards.html" . }} -{{- template "partials/templates/schema.html" . }} -{{- end }}
\ No newline at end of file +{{- template "partials/templates/schema_json.html" . }} +{{- end }} diff --git a/layouts/partials/templates/schema.html b/layouts/partials/templates/schema.html deleted file mode 100644 index 6c032b6..0000000 --- a/layouts/partials/templates/schema.html +++ /dev/null @@ -1,31 +0,0 @@ -<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" }}{{ if not .PublishDate.IsZero }} -<meta itemprop="datePublished" {{ .PublishDate.Format $ISO8601 | printf "content=%q" | safeHTMLAttr }} />{{ end }} -{{ if not .Lastmod.IsZero }}<meta itemprop="dateModified" {{ .Lastmod.Format $ISO8601 | printf "content=%q" | safeHTMLAttr }} />{{ end }} -<meta itemprop="wordCount" content="{{ .WordCount }}"> -{{- if .Params.cover.image -}} -{{- if (ne .Params.cover.relative true) }} -<meta itemprop="image" content="{{ .Params.cover.image | absURL }}" /> -{{- else}} -<meta itemprop="image" content="{{ (path.Join .RelPermalink .Params.cover.image ) | absURL }}" /> -{{- end}} -{{- else }} -{{ with $.Params.images }}{{ range first 6 . -}} -<meta itemprop="image" content="{{ . | absURL }}"> -{{ end }}{{ else -}} -{{- $images := $.Resources.ByType "image" -}} -{{- $featured := $images.GetMatch "*feature*" -}} -{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}} -{{- with $featured -}} -<meta itemprop="image" content="{{ $featured.Permalink }}"> -{{ else -}} -{{- with $.Site.Params.images -}} -<meta itemprop="image" content="{{ index . 0 | absURL }}"/> -{{ end }}{{ end }}{{ end }} -{{- end }} - -<!-- 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 }}" /> -{{- end }} diff --git a/layouts/partials/templates/schema_json.html b/layouts/partials/templates/schema_json.html new file mode 100644 index 0000000..a186603 --- /dev/null +++ b/layouts/partials/templates/schema_json.html @@ -0,0 +1,65 @@ +{{ if .IsHome }} +<script type="application/ld+json"> +{ + "@context": "https://schema.org", + "@type": "{{- ( .Site.Params.schema.publisherType | default "Organization") | humanize -}}", + "name": {{ .Site.Title }}, + "url": {{ .Site.BaseURL }}, + "description": {{ .Site.Params.description | plainify | truncate 180 | safeHTML }}, + "thumbnailUrl": {{ .Site.Params.assets.favicon | default "favicon.ico" | absURL }}, + "sameAs": [ + {{- if .Site.Params.schema.sameAs }} + {{ range $i, $e := .Site.Params.schema.sameAs }}{{ if $i }}, {{ end }}{{ $e }}{{ end }} + {{- else}} + {{ range $i, $e := .Site.Params.SocialIcons }}{{ if $i }}, {{ end }}{{ $e.url }}{{ end }} + {{- end}} + ] +} +</script> +{{- else if .IsPage }} +<script type="application/ld+json"> +{ + "@context": "https://schema.org", + "@type": "BlogPosting", + "headline": {{ .Title | plainify}}, + "name": "{{ .Title | plainify }}", + "description": {{ .Summary | plainify | truncate 180 | safeHTML }}, + "keywords": [ + {{- if .Params.keywords }} + {{ range $i, $e := .Params.keywords }}{{ if $i }}, {{ end }}{{ $e }}{{ end }} + {{- else }} + {{ range $i, $e := .Params.tags }}{{ if $i }}, {{ end }}{{ $e }}{{ end }} + {{- end }} + ], + "articleBody": {{ .Content | safeJS | htmlUnescape | plainify }}, + "wordCount" : "{{ .WordCount }}", + "inLanguage": {{ .Language.Lang | default "en-us" }}, + {{ if .Params.cover.image -}} + "image": + {{- if (ne .Params.cover.relative true) -}} + {{ .Params.cover.image | absURL }}, + {{- else -}} + {{ (path.Join .RelPermalink .Params.cover.image ) | absURL }}, + {{- end}} + {{- end}} + "datePublished": {{ .PublishDate }}, + "dateModified": {{ .Lastmod }}, + "author": { + "@type": "Person", + "name": {{ .Params.author | default .Site.Params.author }} + }, + "mainEntityOfPage": { + "@type": "WebPage", + "@id": {{ .Permalink | safeHTML }} + }, + "publisher": { + "@type": "{{- ( .Site.Params.schema.publisherType | default "Organization") | humanize -}}", + "name": {{ .Site.Title }}, + "logo": { + "@type": "ImageObject", + "url": {{ .Site.Params.assets.favicon | default "favicon.ico" | absURL }} + } + } +} +</script> +{{ end }} |
