Open Graph images with Astro and Typst
The author wanted blog posts to show an image when shared, which is set with the og:image meta tag from Open Graph[1]. Instead of picking stock photos from Unsplash by hand, he built a pipeline that generates image cards at build time.
The images are made with Typst[2], a tool that turns a simple markup language into documents and can save the result as a PNG from the command line. The post title is passed to the template with --input title=..., and the template reads it from sys.inputs and reduces the font size from 72pt to 60pt when the title is longer than 70 characters.
The site is built with Astro, a static site generator[3], which creates a route at src/pages/posts/[id]/og.png.ts for each post. The route runs the typst command during the build and returns the result with the image/png content type. So that platforms fetch a new picture after changes, the author adds a cache busting parameter[4] to the URL, made from a SHA-256 hash of the template and the title, which changes only when the content changes.
Read the original eaj.noDiscussion
Sources
-
eaj.no
Open Graph images with Astro and Typst





