HTML export

Export semantic Document trees and MDHTML fragments to finished HTML, with resolved cross-references, numbering, captions, and syntax highlighting.

Every exporter accepts a Document, symbolic MDHTML string, or fast5ever DOM without mutating its input. Combine mdhtml2html with dialect_css, math_js, and meta_table when assembling standalone HTML pages.

from mdhtml import md2mdhtml
html = mdhtml2html(md2mdhtml("# Guide\n\nSome *text*."), toc=True, hl=None)
assert "Guide" in html and not html.warnings

Invalid reference targets in refs='resolve' mode, unknown heading-numbering schemes, or invalid cite_style values raise ValueError.

mdhtml2html

def mdhtml2html(
    src:str | Document, # `Document`, symbolic MDHTML string, or `fast5ever` DOM (never mutated)
    dest:str | Path | None=None, # Optional output file path to write the rendered HTML
    reftypes:dict | None=None, # Override reference-type prefixes (`{prefix: (singular, plural)}`)
    number_headings:str | dict | bool | None=None, # Scheme name, `{lvlText: numFmt}` dict, `False` to disable, or `None` to inherit/auto-number
    hl:"Literal['spans', 'api'] | None"='spans', # Code highlighting mode (`'spans'`, `'api'`, or `None` for plain code blocks)
    auto_ids:bool=True, # Derive Pandoc-style ids for headings without an authored id?
    toc:bool=False, # Prepend a generated table of contents?
    refs:"Literal['resolve', 'ids', 'lenient']"='resolve', # Cross-reference mode (`'resolve'`, `'ids'`, or `'lenient'`)
    id_prefix:str='', # Prefix applied to every emitted element id and in-fragment href
    fn_salt:str='', # Extra prefix applied to footnote ids (`fn-*`/`fnref-*`)
    hl_lang=None, # Optional `(text, lang) -> lang` callback overriding a fence's language
    code_wrap=None, # Optional `(html, lang, text) -> html | None` callback wrapping highlighted blocks
    gh_ids:bool=False, # Derive heading ids using GitHub slugger rules instead of Pandoc rules?
    cite_style:"Literal['author-year', 'numeric'] | None"=None, # Citation style (`None` inherits metadata, defaulting to `'author-year'`)
)->Html: # Rendered HTML (`str` subclass) carrying `.warnings`

Lower a Document or symbolic MDHTML fragment to finished HTML with resolved cross-references, numbering, captions, {=html} splicing, and optional code highlighting.

Open/closed disclosure panels (div[data-panel]) lower to <details> with their header as <summary>. Authored ids also carry data-id. Pass auto_ids=False when combining multiple fragments on one page to avoid derived-id collisions. refs='ids' emits target ids as .xref links without requiring targets in the fragment; refs='lenient' resolves known targets and falls back to .xref links with warnings for missing ones. Syntax highlighting uses optional fastpylight (pip install 'mdhtml[hl]'); when absent, code blocks render plain with a warning.

Html

def Html(
    s:str, warnings:list
):

Exported HTML (str subclass) carrying the export's .warnings list.

bake_mdhtml

def bake_mdhtml(
    src:str | Document, # `Document`, symbolic MDHTML string, or `fast5ever` DOM
    reftypes:dict | None=None, # Override reference-type prefixes
    number_headings:str | dict | bool | None=None, # Heading numbering scheme, `False`, or `None`
    refs:"Literal['resolve', 'ids', 'lenient']"='resolve', # Cross-reference resolution mode
    links:bool=True, # Emit anchor links for resolved references (`False` emits plain text)?
    cite_style:"Literal['author-year', 'numeric'] | None"=None, # Citation style (`None` inherits metadata)
):

Resolve cross-references, numbering, and citations once into a baked Mdhtml fragment; built-in exporters bake automatically.

import_mdhtml

def import_mdhtml(
    src:str | Document
)->Document:

Import symbolic MDHTML (or a fast5ever DOM) into a semantic Document, returning an existing Document unchanged.

dialect_css

def dialect_css(
    preview:bool=False
):

Return the dialect stylesheet CSS for rendered MDHTML pages; set preview=True to include preview styles.

math_js

def math_js(
    fn:str | None=None, # Optional JS function name to emit (omit to render the whole document immediately)
    **opts
)->str: # JavaScript source rendering MDHTML math carriers with a preloaded `katex` global

Emit JavaScript that renders MDHTML math carriers in place using KaTeX, guarded against re-rendering.

meta_table

def meta_table(
    meta:dict[str, str]
)->str:

Render a frontmatter meta dict as an HTML <table class="frontmatter"> for prepending to a page.

Reference indexing

Low-level Rust index and heading-counter types shared by HTML, GFM, Typst, and DOCX exporters.

ReferenceIndex

class ReferenceIndex:

Cross-reference, citation, and heading-numbering index computed from a Document.

ReferenceIndex.validate

def validate(
    reference
):

Validate a single reference dict against this index, raising ValueError on unresolved or invalid targets.

ReferenceIndex.plan

def plan(
    items
):

Plan how a reference group items renders into inline nodes and native reference_field dicts.

ReferenceIndex.resolve

def resolve(
    items, links:bool=False
):

A reference group resolved to inline nodes in resolve mode: plain text, or links with links=True.

ReferenceIndex.number_headings

@property
def number_headings(self):

Active heading numbering scheme (None, False, scheme name, or {lvlText: numFmt} dict).

ReferenceIndex.targets

@property
def targets(self):

Mapping of target ids to target metadata dicts (kind, title, number, text).

ReferenceIndex.reftypes

@property
def reftypes(self):

Mapping of reference type prefixes (such as 'sec', 'fig', 'tbl') to (singular, plural) labels.

ReferenceIndex.cite_style

@property
def cite_style(self):

Resolved citation style ('author-year' or 'numeric').

HeadingNums

def HeadingNums(
    scheme
):

Heading numbering per a {lvlText: numFmt} scheme (Word semantics): bump at each heading, then read its display or full-context number.

HeadingNums.bump

def bump(
    lvl
):

Advance level lvl (0-based), resetting deeper levels; returns the display number, or None beyond the scheme.

HeadingNums.display

def display(
    lvl
):

The number as shown at a level-lvl heading.

HeadingNums.full

def full(
    lvl
):

Word-style full context: ancestor displays concatenated, unless lvl's own lvlText already includes them.

HeadingNums.scheme

@property
def scheme(self):

The scheme as (lvlText, numFmt) pairs in level order.

HeadingNums.counts

@property
def counts(self):

Current heading counter per level.