typst-batch
Typst → HTML batch compilation library with shared resources.
Note: This library was extracted from tola-ssg. It is designed for Typst → HTML workflows and may not be generic enough for all use cases.
Features
- Shared resources: Fonts loaded once, packages cached
- Batch compilation: Parallel processing with shared file snapshot
- Fast scanning: Skip Layout phase for metadata extraction
- Virtual file system: Inject dynamic content without physical files
- Structured diagnostics: Rich error messages with source locations
Installation
[]
= "0.1"
Feature Flags
| Feature | Default | Description |
|---|---|---|
colored-diagnostics |
✓ | ANSI colored output |
scan |
✓ | Fast scanning API (skips Layout) |
batch |
✓ | Parallel batch compilation (rayon) |
svg |
SVG rendering for frames |
Quick Start
use *;
API
Single File Compilation
use *;
// Basic compilation
let result = new
.with_path
.compile?;
// With sys.inputs
let result = new
.with_inputs
.with_path
.compile?;
// Query metadata: #metadata((title: "Hello")) <post-meta>
if let Some = result.document.query_metadata
Batch Compilation
use *;
// Create batcher with shared snapshot
let batcher = new
.into_batch
.with_inputs_obj
.with_snapshot_from?;
// Batch scan (Eval-only, skips Layout)
let scans = batcher.batch_scan?;
// Filter drafts, then compile
let non_drafts: = files.iter
.zip
.filter
.map
.collect;
// Batch compile with progress callback
let results = batcher.batch_compile_each?;
// Batch compile with per-file context
let results = batcher.batch_compile_with_context?;
Fast Scanning
use *;
// Single file scan
let result = new.scan?;
let links = result.links;
let headings = result.headings;
let meta = result.metadata;
// Batch scan (lightweight, no fonts)
let scanner = for_scan
.with_snapshot_from?;
let scans = scanner.batch_scan?;
Virtual File System
use *;
let mut vfs = new;
vfs.insert;
set_virtual_fs;
In Typst:
#let site = json("/_data/site.json")
= #site.title
SVG Frame Rendering
use *;
let result = new.with_path.compile?;
let doc = result.document;
// Collect frames from document tree
let frames: = collect_frames;
// Render frames to SVG (parallel with `batch` feature)
let svgs: = doc.render_frames;
Diagnostics
use *;
let result = new.with_path.compile?;
// Filter out unwanted diagnostics
let filtered = result.diagnostics.filter_out;
if !filtered.is_empty
Modules
| Module | Description |
|---|---|
process |
Compile and scan APIs (Compiler, Scanner, Batcher) |
html |
HTML document types (HtmlDocument, HtmlElement, HtmlFrame) |
codegen |
JSON ↔ Typst value conversion |
resource |
Shared resources (fonts, packages, file cache) |
world |
Typst World implementation |
diagnostic |
Error formatting and filtering |
Typst Access
For advanced use cases:
use typst;
use typst_html;
Requirements
- Typst 0.14.1
- Rust Edition 2024
License
MIT