[PATCH] tpl: Allow using page resources on the images page parameter

for `opengraph`, `schema` and `twitter_cards` templates

The page images selection order as follows:

1. Page's images parameter, image resources are supported.
2. Page's image resources that naming in *feature*, *cover* or *thumbnail* pattern.
3. If no page images specified, then the first one of site's images will be used as the fallback, supports site resources.

cherry-picked from 14d85ec136

++ compatibility fixes for PaperMod

Co-authored-by: Aditya Telange <21258296+adityatelange@users.noreply.github.com>
This commit is contained in:
Razon Yang 2023-12-04 19:05:41 +08:00 committed by Aditya Telange
parent b5f7118d82
commit df330a05b5
No known key found for this signature in database
GPG Key ID: 82E844EF3DA99E77
3 changed files with 55 additions and 28 deletions

View File

@ -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 }}

View File

@ -10,18 +10,10 @@
{{- end}}
{{- else }}
{{- with $.Params.images -}}
{{- range first 6 . }}<meta property="og: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 property="og:image" content="{{ $featured.Permalink }}"/>
{{- else -}}
{{- with site.Params.images }}<meta property="og:image" content="{{ index . 0 | absURL }}"/>{{ end -}}
{{- end -}}
{{- end -}}
{{- $images := partial "partials/templates/_funcs/get-page-images" . -}}
{{- range first 6 $images }}
<meta property="og:image" content="{{ .Permalink }}" />
{{ end -}}
{{- end }}
{{- if .IsPage }}

View File

@ -6,25 +6,13 @@
<meta name="twitter:image" content="{{ (path.Join .RelPermalink .Params.cover.image ) | absURL }}" />
{{- end}}
{{- else }}
{{- with $.Params.images -}}
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="{{ index . 0 | absURL }}"/>
{{ else -}}
{{- $images := $.Resources.ByType "image" -}}
{{- $featured := $images.GetMatch "*feature*" -}}
{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}}
{{- with $featured -}}
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="{{ $featured.Permalink }}"/>
{{- $images := partial "partials/templates/_funcs/get-page-images" . -}}
{{- with index $images 0 -}}
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:image" content="{{ .Permalink }}" />
{{- else -}}
{{- with site.Params.images -}}
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="{{ index . 0 | absURL }}"/>
{{ else -}}
<meta name="twitter:card" content="summary"/>
{{- end -}}
{{- end -}}
{{- end }}
{{- end }}
<meta name="twitter:title" content="{{ .Title }}"/>
<meta name="twitter:description" content="{{ with .Description }}{{ . }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ else }}{{ with site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end -}}"/>