Preparation /hast
/hast loads neither the parser nor React. It turns a parse snapshot into a sanitized render snapshot.
Basic usage
Preparation:
- Clones the semantic tree and runs
wikitextPlugins. - Validates the working tree structure, node count, and depth, reassigns path-style ids, and clears that root's query cache.
- Applies
resolveLink,resolveImage, andhandlers, converting the semantic tree to HAST. - Runs
rehypePlugins. - Runs final sanitization and finalization, adding the TOC, references, category footer, and image authorization, and creates the
WikitextRenderDocument.
The original parse snapshot is never modified.
Options
Plugin lists are structured arrays. Elements can be plugin functions or [plugin, options] tuples and run in order. The signature follows unified's (tree, file), where file is a VFile.
- The synchronous API throws
WikitextPrepareErrorwithASYNC_PLUGIN_IN_SYNC_PATHwhen it sees an async plugin. - The async API supports async plugins; preparation must finish before React renders.
- The prepared working tree still has fixed budgets: 50,000 nodes and depth 1,000. These cannot be changed through parse options.
- Nodes are renumbered after plugins; earlier
flattenNodesresults or id indexes are not updated automatically. - A handler returning
undefinedfalls back to the built-in rule;nullexplicitly drops the node.
Resolvers
Links
Images
nullkeeps the placeholder; returning an object authorizes the resource.srcallows onlyhttp:/https:; relative images are disabled by default.- Dimensions are clamped to 1–4096 and the final
<img>always hasloading="lazy". context.documentis the original parse snapshot;context.astandcontext.nodebelong to the working tree.
Resolvers return plain data, not React elements. To take over appearance, use the view's semanticComponents.
Security policy
Defaults:
- Shallow-merged with defaults.
- Array fields are replaced wholesale; spread
DEFAULT_PREPARE_POLICYto keep the defaults. classis filtered by the[A-Za-z0-9_-]safe character set; there is no policy option to widen or narrow it.DEFAULT_PREPARE_POLICYis frozen.- Invalid fields (policy field types, labels field types,
headingIdStrategyvalues,handlersshape) throwWikitextPrepareError("PREPARE_FAILED")before any work starts. - The final HAST and restored snapshots share the same fixed budget (50,000 nodes, depth 1,000). Prepare fails when it is exceeded, so every snapshot the library produces can be restored.
Labels and headings
labelsare shallow-merged with defaults during preparation and written tometa.labels.- Default fields are listed in the customization guide.
headingIdStrategycontrols how logical anchors map to DOM ids;headingsstoresnodeId,level,text, and optionalid.- Anchors are generated from stripped heading text; semantic headings and source
h1–h6both participate.
Render snapshot
meta includes the effective policy, labels, heading strategy, and resolver-authorized image addresses. The table of contents, reference section, and category footer are already part of hast; they add no extra snapshot fields.
handlers
handlers convert custom semantic nodes to HAST. A node without a handler produces a diagnostic and degrades to a source card.
context provides the original document, working tree, labels, policy, resolvers, handlers, read-only authorizedImages and diagnostics, plus reportDiagnostic, convert, and convertChildren. Handlers take precedence over built-in conversion, so registering a built-in type such as heading overrides the default. Results still go through rehype plugins and final sanitization; final link/image attributes, image authorization, and the table of contents are decided by the finalize stage.
URL and attribute utilities
sanitizeHrefallowshttp:,https:, andmailto:;sanitizeImageUrlallows onlyhttp:andhttps:.allowRelativeis off by default.sanitizedAttributesToPropsfilters by tag whitelist whentagis supplied; otherwise it returns the union of table-related attributes.- Library-reserved attributes such as
data-node-id,data-node-type, anddata-wikitextare removed from source HTML.
Serialization
Restore validates AST / render schema versions, the root, HAST structure, and the fixed node budget, then runs final sanitization and finalization again. It still does not make arbitrary third-party HAST safe to pass to the view.
Diagnostics
Preparation diagnostics are merged into the render snapshot. Their stage may be:
wikitext: semantic tree plugins or conversion.hast: HAST conversion.rehype: rehype plugins.sanitize: sanitization.finalize: finalization.
Use diagnostics with meta for debugging or reporting. A single diagnostic never stops the whole page from rendering.