Hello,
I was trying to use the internal twitter_cards.html template and it didn’t work for me for 2 reasons:
-
I am using the
imagesfront-matter to set thetwitter:imagemeta data.The page was a bundle, so the full path of the page was something like
content/posts/my-post/index.md, and I decided to keep the images in the bundle directory (though, not using the Resources method to fetch those for the meta data, as I don’t need image processing).So the path of an image from site root was
/posts/my-post/images/some-image.png.It was convenient to have the
imagesfront-matter (TOML) set as below:images = ["images/some-image.png"]… instead of typing the full path from site root.
The problem is that in the internal
twitter_cardsandopengraphtemplates use something like below:{{ with .Params.images }} <meta name="twitter:image" content="{{ index . 0 | absURL }}"/> {{ end }}So, with the above front-matter, the above Go template expands to:
<meta name="twitter:image" content="https://example.com/images/some-image.png"/>This expansion is wrong!
The correct image link would be:"https://example.com/posts/my-post/images/some-image.png". -
Another problem is that I cannot simply use
relURLinstead ofabsURLin the above template. Twitter cards require thetwitter:imagetag to have the absolute URL. I have tried using the relative URL, didn’t work.
Workaround
At the moment, the only way to make the twitter cards works (if not using Resources, but instead the images front-matter) is to put the absolute link manually in that front-matter.
So one would need to do something like:
images = ["/posts/my-post/images/some-image.png"]
… inside the front-matter of a /posts/my-post/index.md file.
More than the problem of lot of typing, that link has to be manually fixed if a custom slug is used for the post, or if the post if moved to any other section for some reason.
Fix
Instead I have tried and tested this fix to work (below is the gist of the fix):
{{ $permalink := .Permalink }}
{{ with .Params.images }}
{{ $image := (index . 0) }}
{{ $image_link_absolute := (findRE "^/" $image) }}
{{ if $image_link_absolute }}
<meta name="twitter:image" content="{{ $image | absURL }}"/>
{{ else }}
<meta name="twitter:image" content="{{ (printf "%s%s" $permalink $image) }}"/>
{{ end }}
{{ end }}
What above does:
- If the image path in
imagesin the front-matter begins with/(e.g./images/foo.png), the user is intending to specify the absolute path (would make sense if the path is to an image in thestatic/directory). In that case, the current method ofabsURLis used. - But if the image path does not begin with
/(e.g.images/foo.png), the user wants the Page’s.Permalinkto be auto-prefixed to that path.
Does this work?
Yes! I have tested and deployed this technique already on my blog.
Now, with:
images = ["images/seconds-to-human-time.png"]
the twitter image meta tag looks like:
<meta name="twitter:image" content="https://scripter.co/convert-seconds-to-human-time/images/seconds-to-human-time.png">
Here are the fixed internal templates saved as partials:
twitter_cards.html-
opengraph.html(similar fix applied when retrieving images, as well as videos and audio paths)
So what’s next?
- Is this fix (the above linked modified versions of the 2 internal templates) acceptable in the internal templates? Should I open a PR for this?
- Or should a different logic be used to decide when to auto-prefix the Permalink?
- If not, then well, may be someone else also finds those modified templates useful.