Crate typst_batch

Crate typst_batch 

Source
Expand description

§typst-batch

A Typst → HTML batch compilation library with shared global resources.

This crate was created for tola, a Typst-based static site generator. It provides optimized batch compilation by sharing expensive resources across multiple document compilations:

  • Fonts: Loaded once (~100ms saved per compilation)
  • Packages: Downloaded once and cached globally
  • File cache: Fingerprint-based invalidation for incremental builds
  • Standard library: Shared instance with HTML feature enabled

§Note

This library is specifically designed for Typst → HTML workflows. If you need PDF output or other formats, consider using typst directly or the official typst-cli.

§Quick Start

use typst_batch::{compile_html, get_fonts};
use std::path::Path;

// Initialize fonts once at startup
get_fonts(&[]);

// Compile a single file
let result = compile_html(Path::new("doc.typ"), Path::new("."))?;
std::fs::write("output.html", &result.html)?;

// Compile with metadata extraction
// In your .typ file: #metadata((title: "Hello")) <post-meta>
let result = compile_html_with_metadata(
    Path::new("post.typ"),
    Path::new("."),
    "post-meta",  // label name
)?;
println!("Title: {:?}", result.metadata);

§High-Level API

For most use cases, use the high-level functions:

§Low-Level API

For advanced use cases, access the underlying modules:

  • config: Runtime configuration (User-Agent for package downloads)
  • world: Typst World implementation
  • font: Font discovery and loading
  • file: File caching and virtual file support
  • diagnostic: Error formatting

Re-exports§

pub use compile::compile_document;
pub use compile::compile_document_with_inputs;
pub use compile::compile_document_with_metadata;
pub use compile::compile_html;
pub use compile::compile_html_with_inputs;
pub use compile::compile_html_with_inputs_dict;
pub use compile::compile_html_with_metadata;
pub use compile::query_metadata;
pub use compile::query_metadata_map;
pub use compile::DocumentResult;
pub use compile::DocumentWithMetadataResult;
pub use compile::HtmlResult;
pub use compile::HtmlWithMetadataResult;
pub use diagnostic::CompileError;
pub use diagnostic::DiagnosticOptions;
pub use diagnostic::DisplayStyle;
pub use diagnostic::DiagnosticFilter;
pub use diagnostic::DiagnosticSummary;
pub use diagnostic::DiagnosticsExt;
pub use diagnostic::DiagnosticInfo;
pub use diagnostic::SourceLine;
pub use diagnostic::TraceInfo;
pub use config::Config;
pub use config::ConfigBuilder;
pub use file::clear_file_cache;
pub use file::file_id;
pub use file::file_id_from_path;
pub use file::get_accessed_files;
pub use file::is_virtual_path;
pub use file::read_virtual;
pub use file::read_with_global_virtual;
pub use file::record_file_access;
pub use file::reset_access_flags;
pub use file::set_virtual_fs;
pub use file::virtual_file_id;
pub use file::MapVirtualFS;
pub use file::NoVirtualFS;
pub use file::VirtualFileSystem;
pub use file::EMPTY_ID;
pub use file::GLOBAL_FILE_CACHE;
pub use file::STDIN_ID;
pub use font::font_count;
pub use font::font_family_count;
pub use font::fonts_initialized;
pub use font::get_fonts;
pub use font::init_fonts_with_options;
pub use font::FontOptions;
pub use library::create_library_with_inputs;
pub use library::GLOBAL_LIBRARY;
pub use world::SystemWorld;
pub use typst;
pub use typst_html;
pub use typst_kit;

Modules§

compile
High-level compilation API for Typst to HTML.
config
Configuration for typst-batch.
diagnostic
Diagnostic formatting for Typst compilation errors and warnings.
file
File caching with fingerprint-based invalidation.
font
Global shared font management.
library
Global shared Typst standard library.
package
Global shared package storage.
prelude
Prelude module for convenient imports.
world
SystemWorld implementation - the core World trait.