How to make opengraph screenshots for hugo
it sure looks a lot nicer!
- tags
- hugo
- static_sites
- shot-scaper
Contents
Mostly I write these posts for myself – the act of writing is great
to clarify my thinking, and I actually do use the search on the site a
lot to remember stuff that I forgot. (Will I ever remember how to use
asdf
? Doubtful.) I did notice though that if you share the links on
social media, it looks boring. So lets fix it.
We are going to generate images automatically from the posts. We'll
loop through all of the files – a historical mix of markdown and
lately all org
files – pull out the title and subtitle, stick all of
that into an HTML page, and the use shot-scraper to generate an image
and move it into the leaf pages. Which means that we'll need to make
sure that everything is in a leaf page.
Lets go:
HTML Template
This is our template. It's straight HTML and we're going to replace
TITLE
with the title and SUBTITLE
with.. you get it.
|
|
Test
If you don't have shot-scraper
|
|
Then
|
|
Screenshot of 'file:/Users/wschenk/willschenk.com/content/howto/2024/how_to_make_opengraph_screenshots_for_hugo/og.html' written to 'shot.png'
Move all pages to leaf pages
My posts are in the form
content/{section}/{year}/title
So my glob looks like that. You may need to change it.
|
|
Make the images
This goes through everything and creates a cover.png
for each of the
leaf pages.
|
|
Adding the hugo short codes.
Inside of the <head>
tag, which for me is in the template
layouts/partials/head.html
be sure to add in the opengraph
and
twitter_cards
internal hugo templates.
|
|
Dynamically
This all started by going down a rabbit how for dynamically generating og-images but ultimately the static version was easier.
References
- https://zidhuss.tech/posts/generating-opengraph-images-for-hugo
- https://www.hackification.io/software-development/hugo/html/open-graph-tags/
- https://aarol.dev/posts/hugo-og-image/
- https://github.com/simonw/shot-scraper?tab=readme-ov-file
- https://www.himpler.com/en/blog/auto-generate-og-images-for-hugo/
- https://github.com/search?q=repo%3Agohugoio%2Fhugo%20twitter_cards&type=code
- https://gohugo.io/templates/embedded/#open-graph
Previously
Next