From 6c4e094c91ce143f3895550a28cf0b8057c23e57 Mon Sep 17 00:00:00 2001 From: Aditya Telange <21258296+adityatelange@users.noreply.github.com> Date: Sun, 11 Oct 2020 16:31:13 +0530 Subject: [PATCH 1/4] templates: Add Twitter Cards, OpenGraph, Schema This code is licensed under Apache License 2.0 and is part of https://github.com/gohugoio/hugo --- layouts/partials/templates/opengraph.html | 57 +++++++++++++++++++ layouts/partials/templates/schema.html | 23 ++++++++ layouts/partials/templates/twitter_cards.html | 29 ++++++++++ 3 files changed, 109 insertions(+) create mode 100644 layouts/partials/templates/opengraph.html create mode 100644 layouts/partials/templates/schema.html create mode 100644 layouts/partials/templates/twitter_cards.html diff --git a/layouts/partials/templates/opengraph.html b/layouts/partials/templates/opengraph.html new file mode 100644 index 00000000..07d9775d --- /dev/null +++ b/layouts/partials/templates/opengraph.html @@ -0,0 +1,57 @@ + + + + +{{ with $.Params.images }}{{ range first 6 . -}} + +{{ end }}{{ else -}} +{{- $images := $.Resources.ByType "image" -}} +{{- $featured := $images.GetMatch "*feature*" -}} +{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}} +{{- with $featured -}} + +{{ else -}} +{{- with $.Site.Params.images -}} + +{{ end }}{{ end }}{{ end }} + +{{- $iso8601 := "2006-01-02T15:04:05-07:00" -}} +{{- if .IsPage }} +{{- if not .PublishDate.IsZero }} +{{ else if not .Date.IsZero }} +{{ end }} +{{- if not .Lastmod.IsZero }}{{ end }} +{{- else }} +{{- if not .Date.IsZero }} +{{- end }} +{{- end }}{{/* .IsPage */}} + +{{- with .Params.audio }}{{ end }} +{{- with .Params.locale }}{{ end }} +{{- with .Site.Params.title }}{{ end }} +{{- with .Params.videos }} +{{- range . }} + +{{ end }}{{ end }} + +{{- /* If it is part of a series, link to related articles */}} +{{- $permalink := .Permalink }} +{{- $siteSeries := .Site.Taxonomies.series }}{{ with .Params.series }} +{{- range $name := . }} + {{- $series := index $siteSeries $name }} + {{- range $page := first 6 $series.Pages }} + {{- if ne $page.Permalink $permalink }}{{ end }} + {{- end }} +{{ end }}{{ end }} + +{{- if .IsPage }} +{{- range .Site.Authors }}{{ with .Social.facebook }} +{{ end }}{{ with .Site.Social.facebook }} +{{ end }} + +{{- with .Params.tags }}{{ range first 6 . }} +{{ end }}{{ end }} +{{- end }}{{ end }} + +{{- /* Facebook Page Admin ID for Domain Insights */}} +{{- with .Site.Social.facebook_admin }}{{ end }} diff --git a/layouts/partials/templates/schema.html b/layouts/partials/templates/schema.html new file mode 100644 index 00000000..0cbae153 --- /dev/null +++ b/layouts/partials/templates/schema.html @@ -0,0 +1,23 @@ + + + +{{- if .IsPage }}{{ $ISO8601 := "2006-01-02T15:04:05-07:00" }}{{ if not .PublishDate.IsZero }} +{{ end }} +{{ if not .Lastmod.IsZero }}{{ end }} + +{{ with $.Params.images }}{{ range first 6 . -}} + +{{ end }}{{ else -}} +{{- $images := $.Resources.ByType "image" -}} +{{- $featured := $images.GetMatch "*feature*" -}} +{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}} +{{- with $featured -}} + +{{ else -}} +{{- with $.Site.Params.images -}} + +{{ end }}{{ end }}{{ end }} + + + +{{- end }} diff --git a/layouts/partials/templates/twitter_cards.html b/layouts/partials/templates/twitter_cards.html new file mode 100644 index 00000000..cbe2430b --- /dev/null +++ b/layouts/partials/templates/twitter_cards.html @@ -0,0 +1,29 @@ +{{- with $.Params.images -}} + + +{{ else -}} +{{- $images := $.Resources.ByType "image" -}} +{{- $featured := $images.GetMatch "*feature*" -}} +{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}} +{{- with $featured -}} + + +{{- else -}} +{{- with $.Site.Params.images -}} + + +{{ else -}} + +{{- end -}} +{{- end -}} +{{- end }} + + +{{ with .Site.Social.twitter -}} + +{{ end -}} +{{ range .Site.Authors }} +{{ with .twitter -}} + +{{ end -}} +{{ end -}} \ No newline at end of file From 4a4d0c009c988e657134253732757b8be4885c7a Mon Sep 17 00:00:00 2001 From: Aditya Telange <21258296+adityatelange@users.noreply.github.com> Date: Sun, 11 Oct 2020 21:04:17 +0530 Subject: [PATCH 2/4] templates: add cover image to be linked into meta for opengraph, schema and twitter --- layouts/partials/templates/opengraph.html | 7 +++++++ layouts/partials/templates/schema.html | 7 +++++++ layouts/partials/templates/twitter_cards.html | 8 ++++++++ 3 files changed, 22 insertions(+) diff --git a/layouts/partials/templates/opengraph.html b/layouts/partials/templates/opengraph.html index 07d9775d..48ad5ebf 100644 --- a/layouts/partials/templates/opengraph.html +++ b/layouts/partials/templates/opengraph.html @@ -2,6 +2,13 @@ +{{- if .Params.cover.image -}} +{{- if (ne .Params.cover.relative true) }} + +{{- else}} + +{{- end}} +{{- end }} {{ with $.Params.images }}{{ range first 6 . -}} {{ end }}{{ else -}} diff --git a/layouts/partials/templates/schema.html b/layouts/partials/templates/schema.html index 0cbae153..e01dc483 100644 --- a/layouts/partials/templates/schema.html +++ b/layouts/partials/templates/schema.html @@ -5,6 +5,13 @@ {{ end }} {{ if not .Lastmod.IsZero }}{{ end }} +{{- if .Params.cover.image -}} +{{- if (ne .Params.cover.relative true) }} + +{{- else}} + +{{- end}} +{{- end }} {{ with $.Params.images }}{{ range first 6 . -}} {{ end }}{{ else -}} diff --git a/layouts/partials/templates/twitter_cards.html b/layouts/partials/templates/twitter_cards.html index cbe2430b..2b3b7c96 100644 --- a/layouts/partials/templates/twitter_cards.html +++ b/layouts/partials/templates/twitter_cards.html @@ -1,3 +1,11 @@ +{{- if .Params.cover.image -}} + +{{- if (ne $.Params.cover.relative true) }} + +{{- else }} + +{{- end}} +{{ end }} {{- with $.Params.images -}} From 306889f68bcbaae0789bc9dc4c5578c2ddc348af Mon Sep 17 00:00:00 2001 From: Aditya Telange <21258296+adityatelange@users.noreply.github.com> Date: Sun, 11 Oct 2020 21:08:44 +0530 Subject: [PATCH 3/4] head: link custom templates for social meta --- layouts/partials/head.html | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 9d1a43f1..55fa1328 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -49,7 +49,7 @@ {{- if eq (getenv "HUGO_ENV") "production" | or (eq .Site.Params.env "production") }} {{- template "_internal/google_analytics_async.html" . }} -{{- template "_internal/opengraph.html" . }} -{{- template "_internal/twitter_cards.html" . }} -{{- template "_internal/schema.html" . }} +{{- template "partials/templates/opengraph.html" . }} +{{- template "partials/templates/twitter_cards.html" . }} +{{- template "partials/templates/schema.html" . }} {{- end }} \ No newline at end of file From 23636ab3260ea41354d7ee560f8848e1c684ec6d Mon Sep 17 00:00:00 2001 From: Aditya Telange <21258296+adityatelange@users.noreply.github.com> Date: Mon, 12 Oct 2020 17:26:45 +0530 Subject: [PATCH 4/4] templates: social-meta: prefer cover image over other images --- layouts/partials/templates/opengraph.html | 3 ++- layouts/partials/templates/schema.html | 3 ++- layouts/partials/templates/twitter_cards.html | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/layouts/partials/templates/opengraph.html b/layouts/partials/templates/opengraph.html index 48ad5ebf..834e3a12 100644 --- a/layouts/partials/templates/opengraph.html +++ b/layouts/partials/templates/opengraph.html @@ -8,7 +8,7 @@ {{- else}} {{- end}} -{{- end }} +{{- else }} {{ with $.Params.images }}{{ range first 6 . -}} {{ end }}{{ else -}} @@ -21,6 +21,7 @@ {{- with $.Site.Params.images -}} {{ end }}{{ end }}{{ end }} +{{- end }} {{- $iso8601 := "2006-01-02T15:04:05-07:00" -}} {{- if .IsPage }} diff --git a/layouts/partials/templates/schema.html b/layouts/partials/templates/schema.html index e01dc483..6c032b66 100644 --- a/layouts/partials/templates/schema.html +++ b/layouts/partials/templates/schema.html @@ -11,7 +11,7 @@ {{- else}} {{- end}} -{{- end }} +{{- else }} {{ with $.Params.images }}{{ range first 6 . -}} {{ end }}{{ else -}} @@ -24,6 +24,7 @@ {{- with $.Site.Params.images -}} {{ end }}{{ end }}{{ end }} +{{- end }} diff --git a/layouts/partials/templates/twitter_cards.html b/layouts/partials/templates/twitter_cards.html index 2b3b7c96..de86c781 100644 --- a/layouts/partials/templates/twitter_cards.html +++ b/layouts/partials/templates/twitter_cards.html @@ -5,7 +5,7 @@ {{- else }} {{- end}} -{{ end }} +{{- else }} {{- with $.Params.images -}} @@ -25,6 +25,7 @@ {{- end -}} {{- end -}} {{- end }} +{{- end }} {{ with .Site.Social.twitter -}}