Persistent Typst rendering context.
A context wraps a Rust-side SystemWorld that keeps fonts, virtual files,
and the compiled document in memory. The typical lifecycle is:
new/1— create a context (scans fonts, sets root path)set_markup/2— load a Typst template- Optionally inject data via
set_virtual_file/3,stream_virtual_file/4, orset_inputs/2 compile/1— compile the markup into a paged documentrender_svg/2orexport_pdf/2— render output from the compiled document (export_html/2andexport_bundle/2compile on their own)
Steps 2-5 can be repeated without re-creating the context. Fonts and virtual files persist until explicitly changed.
Thread safety
Each function acquires internal locks, so a single context can be shared
across processes. However, concurrent compile and set_markup calls on
the same context will serialize — design your pipeline accordingly.
Summary
Functions
Append a chunk to a virtual file (creates it if new).
Remove a virtual file. Invalidates the compiled document.
Compile the current markup.
Export the document as a multi-file bundle.
Export the document as HTML.
Export the compiled document as a PDF binary.
List font families available in this context.
Create a new context.
Render a page of the compiled document as SVG.
Set a single sys.inputs key/value pair.
Replace all sys.inputs with the given map of string keys/values.
Set the main Typst markup. Invalidates any compiled document.
Set (or overwrite) a virtual file with text content. Invalidates the compiled document.
Set (or overwrite) a virtual file with raw binary content. Invalidates the compiled document.
Stream an Elixir enumerable into a virtual file as a Typst array.
Functions
Append a chunk to a virtual file (creates it if new).
Does not invalidate the compiled document — call compile/1
after streaming is complete.
Remove a virtual file. Invalidates the compiled document.
Compile the current markup.
Returns {:ok, %CompileResult{}} with the page count and warnings,
or {:error, %CompileError{}} with diagnostics.
Export the document as a multi-file bundle.
The bundle target lets a single Typst project emit multiple documents and
assets. The template declares its outputs with top-level document and
asset elements. Each takes the destination path as its first positional
argument, for example:
#document("index.html", title: [Home])[
= Home
See the #link("about.html")[about page].
]
#document("about.html", title: [About])[= About]
#asset("logo.svg", read("logo.svg", encoding: none))Performs its own compilation (separate from compile/1). Returns
{:ok, %AshTypst.BundleResult{}} whose files is a map of relative path (no
leading slash) to the rendered binary content, e.g. %{"index.html" => "...", "about.html" => "..."}, and whose warnings is a list of
AshTypst.Diagnostic warnings.
Documents within the bundle are exported according to their declared format (HTML, PDF, SVG, or PNG); assets are emitted verbatim.
Options
:pretty— format HTML/SVG documents in a human-readable way (defaultfalse, i.e. minified):render_bleed— include bleed margins for SVG documents (defaultfalse)
Export the document as HTML.
Performs its own compilation (separate from compile/1).
Options
:pretty— format the HTML in a human-readable way (defaultfalse, i.e. minified)
Export the compiled document as a PDF binary.
Options
:pages— page range string like"1-3,5,7-9"(1-indexed):pdf_standards— list of standards, e.g.[:pdf_a_2b]:document_id— stable identifier for caching
List font families available in this context.
Create a new context.
Fonts are scanned once during creation and reused across all operations.
Options
:root— root path for template resolution (default"."):font_paths— additional font directories to search:ignore_system_fonts— skip system fonts (defaultfalse)
Render a page of the compiled document as SVG.
Options
:page— zero-indexed page number (default0):pretty— format the SVG in a human-readable way (defaultfalse, i.e. minified):render_bleed— expand the rendered area beyond the page bounds to include bleed margins, useful when preparing documents for print (defaultfalse)
Set a single sys.inputs key/value pair.
Replace all sys.inputs with the given map of string keys/values.
Set the main Typst markup. Invalidates any compiled document.
Set (or overwrite) a virtual file with text content. Invalidates the compiled document.
Set (or overwrite) a virtual file with raw binary content. Invalidates the compiled document.
Use this for non-text files like images (PNG, SVG) that Typst reads via
#image(read("name", encoding: none)).
Stream an Elixir enumerable into a virtual file as a Typst array.
Each element is encoded via AshTypst.Code.encode/2 and batched
to Rust for memory efficiency.
Options
:variable_name— the#letbinding name (default"data"):context— encoding context passed toAshTypst.Code.encode/2:batch_size— records per NIF call (default100)