@brief-jetzt/wasm-typst
v0.18.0
Published
[](https://github.com/brief-jetzt/wasm-typst/actions/workflows/CI.yml) [](https://github.
Readme
wasm bindings for typst
This package allows you to use the typst library in the browser.
Usage
The package ships an ergonomic wrapper. Each renderer owns its own wasm instance, so you can create as many as you need:
npm install @brief-jetzt/wasm-typstimport { createTypstRenderer } from "@brief-jetzt/wasm-typst";
const renderer = createTypstRenderer({
fonts: [{ path: "MyFont.ttf", data: fontBytes }], // Uint8Array; optional
sources: {
"main.typ": "#set text(font: \"My Font\")\nHello #sys.inputs.name",
},
// files: { "logo.png": pngBytes }, // optional binary assets
});
const { output, diagnostics } = renderer.render({
type: "pdf", // or "svg" | "svg-pages" | "png-pages"
input: { name: "world" }, // typst sys.inputs
// now: Temporal.Instant | Date | epoch millis — feeds datetime.today() and
// the PDF timestamp; defaults to Date.now(). Fix it for reproducible output.
});
// output: Uint8Array (pdf) or string (svg); one entry per page for the -pages types
// diagnostics: Diagnostic[] ([] when clean), errors first:
// { severity: "error" | "warning", message, path?, start?, end?, line?, column?, hints }
renderer.updateSource("main.typ", "Changed");
renderer.update({ sources: { "main.typ": "..." } }); // shallow-merge
// Single-page PNG (thumbnails etc.), 2 pixels per typographic point:
const png = renderer.renderPng(0, 2);
// Editor/IDE helpers (byte offsets into the given source):
renderer.autocomplete("main.typ", 3, true); // { from, completions: [{ kind, label, apply?, detail? }] }?
renderer.tooltip("main.typ", 13); // { kind: "text" | "code", text }?
renderer.goToDefinition(0, 35, 27); // click (page, xMm, yMm) -> { path, cursor }?
renderer.jumpFromCursor("main.typ", 3); // cursor -> [{ page, x, y }] in mm
// Editing on the rendered page, precise to the character (byte offsets, mm):
renderer.caretPosition("main.typ", 3); // cursor -> { page, x, y, height }?
renderer.selectionRects("main.typ", 0, 5); // range -> [{ page, x, y, width, height }]
renderer.cursorAt("main.typ", 0, 30, 27); // nearest cursor to (page, xMm, yMm), also beside the text
renderer.dispose(); // frees the wasm instance (or `using renderer = ...`)Development
Running tests:
npm testBuilding the package (runs wasm-pack build, then bundles the TS glue into pkg/):
npm ci
npm run buildYou can then install the package in another npm project:
cd <your-npm-project>
npm install <path-to-this-repo>/pkgIf you are installing it in a project that already has @brief-jetzt/wasm-typst installed, you may want to
modify it's package.json: Make sure that the local dependency has the prefix @brief-jetzt set, so that
your import statements keep working:
"dependencies": {
"@brief-jetzt/wasm-typst": "file:../../wasm-typst/pkg",
}Run npm i.
