Resolved document tree and compile

The contract between compile and render. Template reference in the DocStudio developer reference.

Public from docstudio/core: compile, RDT_VERSION and the Rdt* types. Decision record: ADR 0016. Metadata, hashes and the trace are described under Metadata and Trace below.

compile

const result = compile({ template, data, config?, components?, registry?, formatters?, migrations?, limits?, trace?, hash? });

result is { ok, tree, diagnostics, meta, trace }. compile never throws for any JSON input. Inputs are never mutated.

Stages, in order: migrate, structural and semantic validation, data bounds check, application configuration check, configuration cascade, component expansion, binding and evaluation. A failure in migration, validation, the data check or the configuration check returns tree: undefined with that stage's diagnostics. Warnings never stop compilation. ok is true only when a tree exists and no diagnostic has severity error. A tree can exist with ok: false, for example under the error missing-data policy, so callers can still inspect what was produced.

config is the application configuration (configuration.md); its missingData and locale apply. components is a ComponentStore (composites.md). formatters replaces the built-in formatter registry, which is otherwise created from the configuration's locale (registry.md).

The tree

Rdt is plain JSON: { rdtVersion: '1', page, header?, footer?, body }. It contains no bindings, expressions, conditions, repeaters, components or slots. It is the only contract between compilation and output adapters. No layout is computed; flow layout, pagination and measurement belong to the renderer.

page has width and height in millimetres (orientation applied), orientation, margins (top, right, bottom, left, millimetres), direction, mode (flow or fixed), locale and an optional background. header and footer are { height, children }.

Every node has kind, id (unique in the document), a style and optional name, width, height, frame and break. style is the full effective style for that node after the cascade (engine defaults, application, theme, template, section, named style, node style, conditional styles), with tokens resolved to values. The style object is shared between nodes with the same resolution, so serialize it once if size matters.

Kind Fields beyond the base
text runs: { text, bound }[]. bound is true for text that came from data, so renderers can isolate it for bidirectional text. Adjacent runs of the same kind are merged and empty runs dropped
image src (the asset reference, unchanged), alt, fit
divider thickness, color
shape shape, fill, stroke, strokeWidth, radius
pageNumber format
qr, barcode value (bound text), size or format
stack, row, grid, fixed, section children, plus gap, align, justify, columns or size where the template set them
table columns (id, header, width, align, style), rows (cells of { text, bound }), footer (rows of cells with span and align), repeatHeader (default true), zebra (default false), rowBreak (default avoid), emptyText

Evaluation rules

  • Visibility. A node whose when is false is omitted. Missing data in a condition is false.
  • Text. Segments are bound with the configuration's missing-data policy (binding.md). Numbers and booleans display; objects and arrays are the wrong type.
  • Repeaters. The children are emitted once per row, flattened into the parent, with $index, $count, $rows, $parent and $root working at any depth. With no rows (or a missing or wrong-type source) the empty content is emitted. Ids get a #index suffix per level, so they stay unique. The as alias is not used; scopes are positional.
  • Tables. One row per source row. Cell expressions see the row as the current item. Column visible and footer expressions see all rows through $rows. A hidden column is dropped with its cells, and footer spans are trimmed to the visible column count. Empty state: hide omits the table, message sets emptyText, showHeader (and no setting) keeps the header with no rows.
  • Sections. A section enters its configuration scope (direction, tokens, style) for itself and its children; siblings after it are unaffected.
  • Components. The call becomes a stack carrying the call's own style, size, frame and break. Props are expressions evaluated in the caller's scope and form $props for the body. Slot content is evaluated in the caller's scope with the caller's $props, not the component's.
  • Props must be expressions (a literal, a path or a call). An array or object prop is passed by pointing a path at it.
  • Component body data scope. A component body sees the same current data item as its call site; only $props and slots are special.

Metadata

result.meta exists whenever a tree does. It is deterministic: no timestamps, host names or data content.

Field Meaning
rdtVersion, schemaVersion The RDT format and the (migrated) template schema version
migrations The migration steps applied on read
templateHash Canonical SHA-256 (sha256:...) of the migrated template. Independent of key order and of data
configHash Hash of the effective configuration
treeHash Hash of the RDT. On by default; hash: { tree: false } skips it
dataHash Hash of the data. Off by default; hash: { data: true } enables it
components Lock records { ref, name, version, hash } for every component used
stats nodes, tableRows, cells, textRuns in the tree
errors, warnings Diagnostic counts

The same template, configuration, data and component versions always give the same metadata and the same tree. Changing only the data changes treeHash; changing only the configuration changes configHash and treeHash but not templateHash. A hash that cannot be computed (for example beyond the output ceiling) is undefined with a warning. The tree is hashed against the ceiling limits because its size is already bounded by compile. hashTemplate(value) is exported for hashing any JSON value; it returns undefined rather than throwing.

Trace

trace: true adds result.trace = { entries, truncated }. Each entry is the binder trace record from binding.md: pointer, label, kind, value type, state, whether a fallback supplied the value, outcome and a preview. Pointers are template pointers, or ref#pointer inside a component. Entries are capped by maxTraceEntries. Tracing does not change the tree, the diagnostics or the metadata, and with it off nothing is recorded. Previews contain data values, so treat a trace like the data itself.

Limits

Output size is bounded: nodes plus table cells may not exceed maxJsonNodes (default 1,000,000), reported once as LIMIT_EXCEEDED. Rows per repeater or table are bounded by maxRepeaterRows. Template nodes, expansion, expression and evaluation limits apply as described in earlier references.

Performance

Median on macOS (Node 24), full pipeline: a report with a table, computed totals, a conditional section and a repeater takes 2.4 ms for 500 rows and 16.9 ms for 5,000 with the tree hash off. The default tree hash costs about 4.8 ms and 35 ms more (7.2 and 51.9 ms); the data hash about 0.5 and 12 ms; tracing adds about 4.7 and 39 ms. See validation/artifacts/benchmarks/core-m10-compile.json.