TLDR: Remove the images key from your front matter.
First, overriding the internal templates is by no means clunky. I view them as guides for creating my own. What they should do depends on the structure of your site, how you manage images, etc.
Second, I’ve reformatted the internal twitter card template with some warning messages so that you can see what parts of the code are exercised:
layouts/partials/twitter_cards.html
{{- with $.Params.images -}}
{{ warnf "A" }}
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="{{ index . 0 | absURL }}"/>
{{ else -}}
{{ warnf "B" }}
{{- $images := $.Resources.ByType "image" -}}
{{- $featured := $images.GetMatch "*feature*" -}}
{{- if not $featured }}
{{ warnf "C" }}
{{ $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}
{{ end -}}
{{- with $featured -}}
{{ warnf "D" }}
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="{{ $featured.Permalink }}"/>
{{- else -}}
{{ warnf "E" }}
{{- with $.Site.Params.images -}}
{{ warnf "F" }}
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="{{ index . 0 | absURL }}"/>
{{ else -}}
{{ warnf "G" }}
<meta name="twitter:card" content="summary"/>
{{- 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 -}}"/>
{{ with .Site.Social.twitter -}}
{{ warnf "H" }}
<meta name="twitter:site" content="@{{ . }}"/>
{{ end -}}
Run this yourself by calling {{ partial "twitter_cards.html" . }}
from the single page template, just so you can limit the usage to single pages. It would be even clearer if you only had one page to render.
With this structure…
content/
├── posts/
│ └── 2022-12-14-battery_infrastructure/
│ ├── cover.jpg
│ └── index.md
└── _index.md
… the modified template emits Warning A. If you read about the absURL
function, you can see that it simply prepends the site’s baseURL
to whatever path you provide. So it’s doing the right thing, but not giving you the result you want.
You could fix this in front matter with:
images = ['posts/2022-12-14-battery_infrastructure/cover.jpg']
But don’t. I suspect that reading the images array in front matter was to accommodate those who place their images in the static
directory (opinion: don’t do that either).
Instead, remove the images key from your front matter, and build the site again. Now the modified template emits:
- Warning B (there’s nothing in front matter)
- Warning C (it didn’t find an image in your page bundle matching the
*feature*
pattern)
- Warning D (it found an image in your page bundle matching the
*cover*
or *thumbnail*
pattern)
Because it now has a page resource, it uses the .Permalink
method to get the absolute URL. The .Permalink
and .RelPermalink
methods on page and global resources (in the assets directory) are always right. They consider the subdirectory (if any) from which the site is served, url
values in front matter, slug
values in front matter, permalinks
in your site configuration, etc.