From 14d85ec136413dcfc96ad8e4d31633f8f9cbf410 Mon Sep 17 00:00:00 2001 From: Razon Yang Date: Mon, 4 Dec 2023 19:05:41 +0800 Subject: [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. --- hugolib/embedded_templates_test.go | 27 ++++++----- tpl/tplimpl/embedded/templates/opengraph.html | 14 ++---- .../partials/_funcs/get-page-images.html | 47 +++++++++++++++++++ tpl/tplimpl/embedded/templates/schema.html | 14 ++---- .../embedded/templates/twitter_cards.html | 20 ++------ tpl/tplimpl/template.go | 4 +- 6 files changed, 75 insertions(+), 51 deletions(-) create mode 100644 tpl/tplimpl/embedded/templates/partials/_funcs/get-page-images.html diff --git a/hugolib/embedded_templates_test.go b/hugolib/embedded_templates_test.go index b40d77dc4..a3b09e369 100644 --- a/hugolib/embedded_templates_test.go +++ b/hugolib/embedded_templates_test.go @@ -36,9 +36,9 @@ lastmod: 2021-05-22T19:25:00-01:00 --- `) - b.WithContent("mypage.md", `--- + b.WithContent("mypage/index.md", `--- title: My Page -images: ["pageimg1.jpg", "pageimg2.jpg"] +images: ["pageimg1.jpg", "pageimg2.jpg", "https://example.local/logo.png", "sample.jpg"] date: 2021-02-26T18:02:00+01:00 lastmod: 2021-05-22T19:25:00+01:00 --- @@ -58,37 +58,42 @@ title: My Site `) b.WithSunset("content/mybundle/featured-sunset.jpg") + b.WithSunset("content/mypage/sample.jpg") b.Build(BuildCfg{}) b.AssertFileContent("public/mybundle/index.html", ` - + - + - + `) b.AssertFileContent("public/mypage/index.html", ` - + + + - - + + + + `) b.AssertFileContent("public/mysite/index.html", ` - - - + + + `) } diff --git a/tpl/tplimpl/embedded/templates/opengraph.html b/tpl/tplimpl/embedded/templates/opengraph.html index f62ff474e..f3f41121c 100644 --- a/tpl/tplimpl/embedded/templates/opengraph.html +++ b/tpl/tplimpl/embedded/templates/opengraph.html @@ -3,17 +3,9 @@ -{{- 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 -}} +{{- $images := partial "_funcs/get-page-images" . -}} +{{- range first 6 $images -}} + {{- end -}} {{- if .IsPage }} diff --git a/tpl/tplimpl/embedded/templates/partials/_funcs/get-page-images.html b/tpl/tplimpl/embedded/templates/partials/_funcs/get-page-images.html new file mode 100644 index 000000000..268ceb4be --- /dev/null +++ b/tpl/tplimpl/embedded/templates/partials/_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/tpl/tplimpl/embedded/templates/schema.html b/tpl/tplimpl/embedded/templates/schema.html index 20b477b59..8a55afe80 100644 --- a/tpl/tplimpl/embedded/templates/schema.html +++ b/tpl/tplimpl/embedded/templates/schema.html @@ -7,17 +7,9 @@ {{ with .Lastmod }}{{ 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 -}} +{{- $images := partial "_funcs/get-page-images" . -}} +{{- range first 6 $images -}} + {{- end -}} diff --git a/tpl/tplimpl/embedded/templates/twitter_cards.html b/tpl/tplimpl/embedded/templates/twitter_cards.html index f9f6f10b0..e66a5029a 100644 --- a/tpl/tplimpl/embedded/templates/twitter_cards.html +++ b/tpl/tplimpl/embedded/templates/twitter_cards.html @@ -1,22 +1,10 @@ -{{- 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 "_funcs/get-page-images" . -}} +{{- with index $images 0 -}} + + {{- else -}} -{{- with $.Site.Params.images -}} - - -{{ else -}} {{- end -}} -{{- end -}} -{{- end }} diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go index 96b985cec..053b53b53 100644 --- a/tpl/tplimpl/template.go +++ b/tpl/tplimpl/template.go @@ -751,7 +751,7 @@ func (t *templateHandler) applyTemplateTransformers(ns *templateNamespace, ts *t return c, err } -//go:embed embedded/templates/* +//go:embed all:embedded/templates/* //go:embed embedded/templates/_default/* //go:embed embedded/templates/_server/* var embeddedTemplatesFs embed.FS @@ -779,7 +779,7 @@ func (t *templateHandler) loadEmbedded() error { // For the render hooks and the server templates it does not make sense to preserve the // double _internal double book-keeping, // just add it if its now provided by the user. - if !strings.Contains(path, "_default/_markup") && !strings.HasPrefix(name, "_server/") { + if !strings.Contains(path, "_default/_markup") && !strings.HasPrefix(name, "_server/") && !strings.HasPrefix(name, "partials/_funcs/") { templateName = internalPathPrefix + name }