Typst and PDF export
Export semantic Document trees and MDHTML fragments to native Typst markup and compile PDFs via the typst CLI.
Cross-references, numbering, figures, tables, footnotes, and code map to live Typst constructs; LaTeX math renders through mitex; {=typst} raw payloads splice verbatim; and template tokens render through an optional tmpl callable.
from mdhtml import parse_md
typst_src = mdhtml2typst(parse_md("# Report\n\nSummary paragraph."))
assert "= Report" in typst_src and not typst_src.warnings
mdhtml2typst raises ValueError on invalid reference targets or numbering schemes; mdhtml2pdf raises FileNotFoundError or subprocess.CalledProcessError if typst is missing or compilation fails.
mdhtml2typst
def mdhtml2typst(
src, # `Document`, symbolic MDHTML string, or `fast5ever` DOM (never mutated)
dest:str | Path | None=None, # Optional `.typ` output file path
reftypes:dict | None=None, # Override reference-type prefixes
number_headings:str | dict | bool | None=None, # Scheme name, `{lvlText: numFmt}` dict, `False` to disable, or `None`
tmpl=None, # Optional `(node) -> str | None` renderer for semantic operations
table_styles:dict | None=None, # Case-insensitive mapping of `custom-style` or CSS class name to Typst `table(...)` args
prelude:str='', # Typst preamble inserted before generated imports and setup
cite_style:str | None=None, # Citation style (`'author-year'`, `'numeric'`, or `None` to inherit metadata)
)->Typst: # Exported Typst markup (`str` subclass) carrying `.warnings`
Export a Document or symbolic MDHTML as native Typst markup without modifying the input.
References, numbering, figures, tables, and footnotes remain live Typst constructs; citations are prepared as linked text. LaTeX math uses mitex, {=typst} raw payloads pass through verbatim, and custom-style takes priority over CSS classes in table_styles.
mdhtml2pdf
def mdhtml2pdf(
src, # `Document`, symbolic MDHTML string, or `fast5ever` DOM
dest:str | Path, # Output PDF path (relative image paths resolve against `dest.parent`)
*, reftypes:dict | None=None, # Override reference-type prefixes
number_headings:str | dict | bool | None=None, # Scheme name, `{lvlText: numFmt}` dict, `False` to disable, or `None`
tmpl=None, # Optional `(node) -> str | None` renderer for semantic operations
table_styles:dict | None=None, # Case-insensitive mapping of `custom-style` or CSS class name to Typst `table(...)` args
prelude:str='', # Typst preamble inserted before generated imports and setup
cite_style:str | None=None, # Citation style (`'author-year'`, `'numeric'`, or `None` to inherit metadata)
)->Typst: # Intermediate `Typst` markup carrying `.warnings`
Compile src to a PDF at dest by rendering mdhtml2typst and running the typst CLI on PATH.
Typst
def Typst(
s:str, warnings:list
):
Exported Typst markup (str subclass) carrying the export's .warnings list.