main
1import { root } from "astro:config/server";
2import { fileURLToPath } from "node:url";
3
4import { NodeCompiler } from "@myriaddreamin/typst-ts-node-compiler";
5import type { RehypePlugins } from "astro";
6import { rehypeIt } from "astro-typst/dist/lib/rehype";
7
8const $typst = NodeCompiler.create({
9 workspace: fileURLToPath(root),
10 // Lets Typst templates detect feed rendering and skip site-only markup.
11 inputs: { "x-renderer": "kerplunk" },
12});
13
14export async function renderTypstToHtml(
15 mainFilePath: string,
16 plugins: RehypePlugins,
17): Promise<string> {
18 const compiled = $typst.compileHtml({ mainFilePath });
19 if (!compiled.result) {
20 compiled.printDiagnostics();
21 throw new Error(`Failed to compile Typst document: ${mainFilePath}`);
22 }
23 const html = $typst.tryHtml(compiled.result);
24 if (!html.result) {
25 html.printDiagnostics();
26 throw new Error(`Failed to render Typst document to HTML: ${mainFilePath}`);
27 }
28 return await rehypeIt(html.result.hast(), plugins);
29}