Open Graph images with Astro and Typst

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Open Graph images with Astro and Typst

Open Graph images with Astro and Typst 9. november 2026

当博客文章链接配有图片时,看起来会更好。如果 HTML 头部包含如下元标签,它就会自动实现: <meta property="og:image" content="https://eaj.no/posts/graph-images-with-typst/og.png" />

My previous, tedious workflow for these kinds of images was to go to unspash.com, search for something like structure or archive, and then select an image I thought would fit the theme of the post. Being both boring and error-prone, I abandonded that process and set up a pipeline that generates image cards at build-time instead. They look like this: Let me show you how I did it.

我之前处理这类图片的工作流程非常繁琐:去 unsplash.com 搜索类似“结构”或“存档”的关键词,然后挑选一张我认为符合文章主题的图片。由于这种方式既枯燥又容易出错,我放弃了该流程,转而建立了一个在构建时自动生成图片卡片的流水线。它们看起来是这样的:让我来展示我是如何做到的。

Typst

Typst is a tool that generates high-quality documents from a simple markup language. A lovechild of LaTeX and Markdown, it works great for math whilst also keeping the day-to-day text formatting syntax bearable. As a small taste, this snippet (adapted from the Typst README) produces this fine document:

Typst 是一个通过简单标记语言生成高质量文档的工具。作为 LaTeX 和 Markdown 的结晶,它在处理数学公式方面表现出色,同时保持了日常文本格式语法的易用性。作为小试牛刀,这段(改编自 Typst README 的)代码片段生成了这份精美的文档:

By default the typst CLI will create a PDF, but it can also make PNGs. The above image was created with the command typst compile --format png --ppi 250 typst-example.typ. But if you’d rather create something with a bit more pizzazz, by all means. How about: #set page(width: 1200pt, height: 630pt, margin: 80pt, fill: rgb("#FF1493")) #set text(font: "Comic Sans MS", fill: white, lang: "no") #grid(rows: (1fr, auto), align(left + horizon, { text(size: 72pt, weight: 800, "Typst very nice") }), text(size: 24pt, "Would typst again"), ) which yields Fantastic.

默认情况下,Typst CLI 会创建 PDF,但它也可以制作 PNG。上面的图片是通过命令 typst compile --format png --ppi 250 typst-example.typ 创建的。但如果你想制作一些更炫酷的东西,当然可以。比如这段代码,它生成的效果非常棒。

If all of my posts were titled “Typst very nice”, we’d be done, but, alas. Luckily, typst accepts inputs to a document with --input key=value, which is put in a sys.inputs dictionary for the template to read. So the template becomes: #let title = sys.inputs.at("title", default: "You forgot the title :(") which, when rendered with typst compile --format png --ppi 144 --input title="Look ma, from the CLI!" fancy-typst-with-input.typ becomes Perfection. Let’s move on to generating the images at build-time.

如果我所有的文章标题都叫“Typst very nice”,那事情就简单了,可惜并非如此。幸运的是,Typst 允许通过 --input key=value 向文档传递输入,这些输入会被放入 sys.inputs 字典供模板读取。因此模板变成了这样,当使用特定命令渲染时,效果堪称完美。让我们继续探讨如何在构建时生成这些图片。

Astro

This site is built with Astro, a static site generator. Articles are written in Markdown, which is then bundled into a Content Collection by Astro. The details are irrelevant, what matters is that this lets me define metadata which I can query through a Javascript API. The source code for the page template is at src/pages/posts/[id].astro.

本网站使用静态网站生成器 Astro 构建。文章以 Markdown 编写,然后由 Astro 打包进内容集合(Content Collection)。细节并不重要,重要的是这让我能够定义元数据,并通过 Javascript API 进行查询。页面模板的源代码位于 src/pages/posts/[id].astro。

There are a few notable things about this file path: That it is in the src/pages/ directory tells Astro that this should be rendered as a page. The remainder of the path, /posts/[id], tells Astro where to put the page. [id] being in brackets means id is a dynamic parameter which must be defined at build time. In other words, Astro needs to know every ID when building the site—it can’t generate infinitely many IDs.

关于这个文件路径有几点值得注意:它位于 src/pages/ 目录下,告诉 Astro 这应该被渲染为一个页面。路径的其余部分 /posts/[id] 告诉 Astro 将页面放置在哪里。[id] 放在括号中意味着 id 是一个必须在构建时定义的动态参数。换句话说,Astro 在构建网站时需要知道每一个 ID——它无法生成无限多的 ID。

A function called getStaticPaths exported from the template defines the set. That is, fetch every article, and use its id as the id parameter. This is the object returned for the current page.

模板中导出的 getStaticPaths 函数定义了这个集合。也就是说,获取每一篇文章,并将其 ID 用作 ID 参数。这就是当前页面返回的对象。

By default Astro will generate an HTML file, but it can also return other kinds of responses. Together with this blog post I also put the following snippet at src/pages/api/build-info.json.ts. Note that there are no parameters in brackets, so this will create a single JSON file at build time.

默认情况下,Astro 会生成一个 HTML 文件,但它也可以返回其他类型的响应。随这篇博文一起,我还在 src/pages/api/build-info.json.ts 中放置了以下代码片段。请注意,这里没有括号内的参数,因此这将在构建时创建一个单一的 JSON 文件。

And just like that, this blog now has a JSON api. Note that the file extension is included in the path, i.e. .json.ts. This is because when the dev server is running it sets the appropriate content-type header to let the browser know what to do with the response, but once the page is built the result is a file with .ts stripped off. The browser then interprets the remaining file extension to know what content to expect at the path.

就这样,这个博客现在拥有了一个 JSON API。请注意,文件扩展名包含在路径中,即 .json.ts。这是因为当开发服务器运行时,它会设置适当的 content-type 头部,让浏览器知道如何处理响应,但一旦页面构建完成,结果就是一个去掉了 .ts 的文件。浏览器随后会解释剩余的文件扩展名,以了解在该路径下应期待什么内容。

You probably see where this is going: Create an API route which takes a post ID as a parameter and ends in .png.ts, gets the post title using the Astro Content Collection API, generates an image, and returns it in a response with the appropriate content-type header. You got it. Here’s the open graph image endpoint in its entirety, defined at src/pages/posts/[id]/og.png.ts.

你可能已经猜到接下来要做什么了:创建一个 API 路由,它接收一个文章 ID 作为参数并以 .png.ts 结尾,使用 Astro 内容集合 API 获取文章标题,生成一张图片,并以适当的 content-type 头部返回响应。没错,就是这样。这是完整的 Open Graph 图片端点,定义在 src/pages/posts/[id]/og.png.ts 中。