Skip to content

Commit 14d85ec

Browse files
authored
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.
1 parent 171836c commit 14d85ec

6 files changed

Lines changed: 75 additions & 51 deletions

File tree

hugolib/embedded_templates_test.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ lastmod: 2021-05-22T19:25:00-01:00
3636
---
3737
`)
3838

39-
b.WithContent("mypage.md", `---
39+
b.WithContent("mypage/index.md", `---
4040
title: My Page
41-
images: ["pageimg1.jpg", "pageimg2.jpg"]
41+
images: ["pageimg1.jpg", "pageimg2.jpg", "https://example.local/logo.png", "sample.jpg"]
4242
date: 2021-02-26T18:02:00+01:00
4343
lastmod: 2021-05-22T19:25:00+01:00
4444
---
@@ -58,37 +58,42 @@ title: My Site
5858
`)
5959

6060
b.WithSunset("content/mybundle/featured-sunset.jpg")
61+
b.WithSunset("content/mypage/sample.jpg")
6162
b.Build(BuildCfg{})
6263

6364
b.AssertFileContent("public/mybundle/index.html", `
64-
<meta name="twitter:image" content="https://example.org/mybundle/featured-sunset.jpg"/>
65+
<meta name="twitter:image" content="https://example.org/mybundle/featured-sunset.jpg" />
6566
<meta name="twitter:title" content="My Bundle"/>
6667
<meta property="og:title" content="My Bundle" />
6768
<meta property="og:url" content="https://example.org/mybundle/" />
68-
<meta property="og:image" content="https://example.org/mybundle/featured-sunset.jpg"/>
69+
<meta property="og:image" content="https://example.org/mybundle/featured-sunset.jpg" />
6970
<meta property="article:published_time" content="2021-02-26T18:02:00-01:00" />
7071
<meta property="article:modified_time" content="2021-05-22T19:25:00-01:00" />
7172
<meta itemprop="name" content="My Bundle">
72-
<meta itemprop="image" content="https://example.org/mybundle/featured-sunset.jpg">
73+
<meta itemprop="image" content="https://example.org/mybundle/featured-sunset.jpg" />
7374
<meta itemprop="datePublished" content="2021-02-26T18:02:00-01:00" />
7475
<meta itemprop="dateModified" content="2021-05-22T19:25:00-01:00" />
7576
7677
`)
7778
b.AssertFileContent("public/mypage/index.html", `
78-
<meta name="twitter:image" content="https://example.org/pageimg1.jpg"/>
79+
<meta name="twitter:image" content="https://example.org/pageimg1.jpg" />
7980
<meta property="og:image" content="https://example.org/pageimg1.jpg" />
8081
<meta property="og:image" content="https://example.org/pageimg2.jpg" />
82+
<meta property="og:image" content="https://example.local/logo.png" />
83+
<meta property="og:image" content="https://example.org/mypage/sample.jpg" />
8184
<meta property="article:published_time" content="2021-02-26T18:02:00+01:00" />
8285
<meta property="article:modified_time" content="2021-05-22T19:25:00+01:00" />
83-
<meta itemprop="image" content="https://example.org/pageimg1.jpg">
84-
<meta itemprop="image" content="https://example.org/pageimg2.jpg">
86+
<meta itemprop="image" content="https://example.org/pageimg1.jpg" />
87+
<meta itemprop="image" content="https://example.org/pageimg2.jpg" />
88+
<meta itemprop="image" content="https://example.local/logo.png" />
89+
<meta itemprop="image" content="https://example.org/mypage/sample.jpg" />
8590
<meta itemprop="datePublished" content="2021-02-26T18:02:00+01:00" />
8691
<meta itemprop="dateModified" content="2021-05-22T19:25:00+01:00" />
8792
`)
8893
b.AssertFileContent("public/mysite/index.html", `
89-
<meta name="twitter:image" content="https://example.org/siteimg1.jpg"/>
90-
<meta property="og:image" content="https://example.org/siteimg1.jpg"/>
91-
<meta itemprop="image" content="https://example.org/siteimg1.jpg"/>
94+
<meta name="twitter:image" content="https://example.org/siteimg1.jpg" />
95+
<meta property="og:image" content="https://example.org/siteimg1.jpg" />
96+
<meta itemprop="image" content="https://example.org/siteimg1.jpg" />
9297
`)
9398
}
9499

tpl/tplimpl/embedded/templates/opengraph.html

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,9 @@
33
<meta property="og:type" content="{{ if .IsPage }}article{{ else }}website{{ end }}" />
44
<meta property="og:url" content="{{ .Permalink }}" />
55

6-
{{- with $.Params.images -}}
7-
{{- range first 6 . }}<meta property="og:image" content="{{ . | absURL }}" />{{ end -}}
8-
{{- else -}}
9-
{{- $images := $.Resources.ByType "image" -}}
10-
{{- $featured := $images.GetMatch "*feature*" -}}
11-
{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}}
12-
{{- with $featured -}}
13-
<meta property="og:image" content="{{ $featured.Permalink }}"/>
14-
{{- else -}}
15-
{{- with $.Site.Params.images }}<meta property="og:image" content="{{ index . 0 | absURL }}"/>{{ end -}}
16-
{{- end -}}
6+
{{- $images := partial "_funcs/get-page-images" . -}}
7+
{{- range first 6 $images -}}
8+
<meta property="og:image" content="{{ .Permalink }}" />
179
{{- end -}}
1810

1911
{{- if .IsPage }}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{{- $imgs := slice }}
2+
{{- $imgParams := .Params.images }}
3+
{{- $resources := .Resources.ByType "image" -}}
4+
{{/* Find featured image resources if the images parameter is empty. */}}
5+
{{- if not $imgParams }}
6+
{{- $featured := $resources.GetMatch "*feature*" -}}
7+
{{- if not $featured }}{{ $featured = $resources.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}}
8+
{{- with $featured }}
9+
{{- $imgs = $imgs | append (dict
10+
"Image" .
11+
"RelPermalink" .RelPermalink
12+
"Permalink" .Permalink) }}
13+
{{- end }}
14+
{{- end }}
15+
{{/* Use the first one of site images as the fallback. */}}
16+
{{- if and (not $imgParams) (not $imgs) }}
17+
{{- with site.Params.images }}
18+
{{- $imgParams = first 1 . }}
19+
{{- end }}
20+
{{- end }}
21+
{{/* Parse page's images parameter. */}}
22+
{{- range $imgParams }}
23+
{{- $img := . }}
24+
{{- $url := urls.Parse $img }}
25+
{{- if eq $url.Scheme "" }}
26+
{{/* Internal image. */}}
27+
{{- with $resources.GetMatch $img -}}
28+
{{/* Image resource. */}}
29+
{{- $imgs = $imgs | append (dict
30+
"Image" .
31+
"RelPermalink" .RelPermalink
32+
"Permalink" .Permalink) }}
33+
{{- else }}
34+
{{- $imgs = $imgs | append (dict
35+
"RelPermalink" (relURL $img)
36+
"Permalink" (absURL $img)
37+
) }}
38+
{{- end }}
39+
{{- else }}
40+
{{/* External image */}}
41+
{{- $imgs = $imgs | append (dict
42+
"RelPermalink" $img
43+
"Permalink" $img
44+
) }}
45+
{{- end }}
46+
{{- end }}
47+
{{- return $imgs }}

tpl/tplimpl/embedded/templates/schema.html

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,9 @@
77
{{ with .Lastmod }}<meta itemprop="dateModified" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }} />{{ end}}
88
<meta itemprop="wordCount" content="{{ .WordCount }}">
99

10-
{{- with $.Params.images -}}
11-
{{- range first 6 . -}}<meta itemprop="image" content="{{ . | absURL }}">{{ end -}}
12-
{{- else -}}
13-
{{- $images := $.Resources.ByType "image" -}}
14-
{{- $featured := $images.GetMatch "*feature*" -}}
15-
{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}}
16-
{{- with $featured -}}
17-
<meta itemprop="image" content="{{ $featured.Permalink }}">
18-
{{- else -}}
19-
{{- with $.Site.Params.images -}}<meta itemprop="image" content="{{ index . 0 | absURL }}"/>{{ end -}}
20-
{{- end -}}
10+
{{- $images := partial "_funcs/get-page-images" . -}}
11+
{{- range first 6 $images -}}
12+
<meta itemprop="image" content="{{ .Permalink }}" />
2113
{{- end -}}
2214

2315
<!-- Output all taxonomies as schema.org keywords -->

tpl/tplimpl/embedded/templates/twitter_cards.html

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1-
{{- with $.Params.images -}}
2-
<meta name="twitter:card" content="summary_large_image"/>
3-
<meta name="twitter:image" content="{{ index . 0 | absURL }}"/>
4-
{{ else -}}
5-
{{- $images := $.Resources.ByType "image" -}}
6-
{{- $featured := $images.GetMatch "*feature*" -}}
7-
{{- if not $featured }}{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}}
8-
{{- with $featured -}}
9-
<meta name="twitter:card" content="summary_large_image"/>
10-
<meta name="twitter:image" content="{{ $featured.Permalink }}"/>
1+
{{- $images := partial "_funcs/get-page-images" . -}}
2+
{{- with index $images 0 -}}
3+
<meta name="twitter:card" content="summary_large_image" />
4+
<meta name="twitter:image" content="{{ .Permalink }}" />
115
{{- else -}}
12-
{{- with $.Site.Params.images -}}
13-
<meta name="twitter:card" content="summary_large_image"/>
14-
<meta name="twitter:image" content="{{ index . 0 | absURL }}"/>
15-
{{ else -}}
166
<meta name="twitter:card" content="summary"/>
177
{{- end -}}
18-
{{- end -}}
19-
{{- end }}
208
<meta name="twitter:title" content="{{ .Title }}"/>
219
<meta name="twitter:description" content="{{ with .Description }}{{ . }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end -}}"/>
2210

tpl/tplimpl/template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ func (t *templateHandler) applyTemplateTransformers(ns *templateNamespace, ts *t
751751
return c, err
752752
}
753753

754-
//go:embed embedded/templates/*
754+
//go:embed all:embedded/templates/*
755755
//go:embed embedded/templates/_default/*
756756
//go:embed embedded/templates/_server/*
757757
var embeddedTemplatesFs embed.FS
@@ -779,7 +779,7 @@ func (t *templateHandler) loadEmbedded() error {
779779
// For the render hooks and the server templates it does not make sense to preserve the
780780
// double _internal double book-keeping,
781781
// just add it if its now provided by the user.
782-
if !strings.Contains(path, "_default/_markup") && !strings.HasPrefix(name, "_server/") {
782+
if !strings.Contains(path, "_default/_markup") && !strings.HasPrefix(name, "_server/") && !strings.HasPrefix(name, "partials/_funcs/") {
783783
templateName = internalPathPrefix + name
784784
}
785785

0 commit comments

Comments
 (0)