diff --git a/layouts/partials/templates/_funcs/get-page-images.html b/layouts/partials/templates/_funcs/get-page-images.html new file mode 100644 index 0000000..268ceb4 --- /dev/null +++ b/layouts/partials/templates/_funcs/get-page-images.html @@ -0,0 +1,47 @@ +{{- $imgs := slice }} +{{- $imgParams := .Params.images }} +{{- $resources := .Resources.ByType "image" -}} +{{/* Find featured image resources if the images parameter is empty. */}} +{{- if not $imgParams }} + {{- $featured := $resources.GetMatch "*feature*" -}} + {{- if not $featured }}{{ $featured = $resources.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}} + {{- with $featured }} + {{- $imgs = $imgs | append (dict + "Image" . + "RelPermalink" .RelPermalink + "Permalink" .Permalink) }} + {{- end }} +{{- end }} +{{/* Use the first one of site images as the fallback. */}} +{{- if and (not $imgParams) (not $imgs) }} + {{- with site.Params.images }} + {{- $imgParams = first 1 . }} + {{- end }} +{{- end }} +{{/* Parse page's images parameter. */}} +{{- range $imgParams }} + {{- $img := . }} + {{- $url := urls.Parse $img }} + {{- if eq $url.Scheme "" }} + {{/* Internal image. */}} + {{- with $resources.GetMatch $img -}} + {{/* Image resource. */}} + {{- $imgs = $imgs | append (dict + "Image" . + "RelPermalink" .RelPermalink + "Permalink" .Permalink) }} + {{- else }} + {{- $imgs = $imgs | append (dict + "RelPermalink" (relURL $img) + "Permalink" (absURL $img) + ) }} + {{- end }} + {{- else }} + {{/* External image */}} + {{- $imgs = $imgs | append (dict + "RelPermalink" $img + "Permalink" $img + ) }} + {{- end }} +{{- end }} +{{- return $imgs }} diff --git a/layouts/partials/templates/opengraph.html b/layouts/partials/templates/opengraph.html index 38fbd24..95f037c 100644 --- a/layouts/partials/templates/opengraph.html +++ b/layouts/partials/templates/opengraph.html @@ -10,18 +10,10 @@ {{- end}} {{- else }} -{{- 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 -}} +{{- $images := partial "partials/templates/_funcs/get-page-images" . -}} +{{- range first 6 $images }} + +{{ end -}} {{- end }} {{- if .IsPage }} diff --git a/layouts/partials/templates/twitter_cards.html b/layouts/partials/templates/twitter_cards.html index 0e7760a..46b0d04 100644 --- a/layouts/partials/templates/twitter_cards.html +++ b/layouts/partials/templates/twitter_cards.html @@ -6,25 +6,13 @@ {{- end}} {{- else }} -{{- with $.Params.images -}} - - -{{ else -}} -{{- $images := $.Resources.ByType "image" -}} -{{- $featured := $images.GetMatch "*feature*" -}} -{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}} -{{- with $featured -}} - - +{{- $images := partial "partials/templates/_funcs/get-page-images" . -}} +{{- with index $images 0 -}} + + {{- else -}} -{{- with site.Params.images -}} - - -{{ else -}} {{- end -}} -{{- end -}} -{{- end }} {{- end }}