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
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 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.
Types
@type t() :: reference()
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.
@spec compile(t()) :: {:ok, AshTypst.CompileResult.t()} | {:error, AshTypst.CompileError.t()}
Compile the current markup.
Returns {:ok, %CompileResult{}} with the page count and warnings,
or {:error, %CompileError{}} with diagnostics.
@spec export_html(t()) :: {:ok, String.t()} | {:error, AshTypst.CompileError.t()}
Export the document as HTML.
Performs its own compilation (separate from compile/1).
@spec export_pdf(t(), keyword() | AshTypst.PDFOptions.t()) :: {:ok, binary()} | {:error, AshTypst.CompileError.t()}
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.
@spec new(keyword() | AshTypst.Context.Options.t()) :: {:ok, t()}
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)
@spec render_svg( t(), keyword() ) :: {:ok, String.t()} | {:error, AshTypst.CompileError.t()}
Render a page of the compiled document as SVG.
Options
:page— zero-indexed page number (default0)
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)).
@spec stream_virtual_file(t(), String.t(), Enumerable.t(), keyword()) :: :ok
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)