Skip to content

Commit 4bbcd7f

Browse files
committed
feat(meta): Optimize $image for both OpenGraph and Twitter Card
Checking all possible featured image sources and resizing to 800px Resolves #144 Signed-off-by: Khusika Dhamar Gusti <[email protected]>
1 parent 1c8fbb1 commit 4bbcd7f

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

layouts/_partials/head/meta.html

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,37 @@
44
<meta name="description" content="{{ $params.description | default .Site.Params.description }}">
55

66
{{- if ne $offline true -}}
7-
{{ partial "opengraph.html" . }}
8-
{{- template "_internal/twitter_cards.html" . -}}
7+
{{- $og := partial "opengraph.html" . -}}
8+
{{- $twitter := partial "twitter_cards.html" . -}}
9+
{{- $image := "" -}}
10+
{{- with or .Params.featuredimage .Params.featuredImage -}}
11+
{{- $original := resources.Get . -}}
12+
{{- with $original }}
13+
{{- $processed := .Resize "800x Lanczos" -}}
14+
{{- $image = $processed.Permalink -}}
15+
{{- end -}}
16+
{{- else -}}
17+
{{- with .Resources.GetMatch "featured-image" -}}
18+
{{- $processed := .Resize "800x Lanczos" -}}
19+
{{- $image = $processed.Permalink -}}
20+
{{- else -}}
21+
{{- range .Params.resources -}}
22+
{{- if eq .name "featured-image" -}}
23+
{{- $original := resources.Get .src -}}
24+
{{- with $original }}
25+
{{- $processed := .Resize "800x Lanczos" -}}
26+
{{- $image = $processed.Permalink -}}
27+
{{- end -}}
28+
{{- end -}}
29+
{{- end -}}
30+
{{- end -}}
31+
{{- end -}}
32+
{{- if $image }}
33+
{{- $og = replaceRE `<meta property="og:image" content="[^"]*">` (printf `<meta property="og:image" content="%s">` $image) $og -}}
34+
{{- $twitter = replaceRE `<meta name="twitter:image" content="[^"]*">` (printf `<meta name="twitter:image" content="%s">` $image) $twitter -}}
35+
{{- end -}}
36+
{{- $og | safeHTML -}}
37+
{{- $twitter | safeHTML -}}
938
{{- end -}}
1039

1140
<meta name="application-name" content="{{ .Site.Params.app.title | default .Site.Title }}">

0 commit comments

Comments
 (0)