Template Expansion
The default pipeline does not execute templates. When you need template output, use /expand to expand supported templates and parser functions before parsing. The library makes no network requests; the host's resolveTemplate supplies template source.
Basic usage
expandWikitext:
- Parses the original source into
original. - Evaluates templates and functions by call structure and records
records. - Parses the expanded source into
document. - Returns
expansion.status:"complete","partial", or"fallback".
The expanded document is already parsed. Do not parse it again; pass it directly to prepareWikitext.
Resolver requirements
null/undefinedmeans the template is missing; an empty string is a valid template.- A title is parsed once per expansion; the resolver must return stable source for the same
title. request.pageNameis the original page;request.callerTitleis the template that contains the current call and is omitted for calls on the original page.request.invocationpoints at the top-level call range inoriginal.source; nested requests reuse that top-level range.- The library handles newline normalization, include filtering, and parsing; the resolver only fetches source.
Supported syntax
- Template transclusion and parameter substitution, including anonymous, named, and default values.
noinclude/includeonly/onlyinclude.#if,#ifeq,#switch,#iferror.{{!}}and{{=}}.- Implicit newlines around template and function output.
Values are computed only when needed: template names, parameter names, and conditions are evaluated in their own scope. Unselected branches, unread parameters, and unused defaults do not call the resolver or count toward the invocation budget.
The following calls are preserved with diagnostics by default:
#invokeand Lua.- Transclusion modifiers such as
subst. - Functions that depend on site state or an external runtime, such as
#ifexistand#time.
Budgets and errors
- A single over-long call is preserved with
LENGTH_BUDGET_EXCEEDED. - If even the preserved source exceeds
maxExpandedLength, the library throwsEXPANSION_TOO_LARGE. - With
strict: true, budget overruns throwEXPANSION_BUDGET_EXCEEDED. - By default, a final parse failure returns the original snapshot with
status: "fallback". - Cancellation is delivered through
AbortSignal; the original reason is stored incause.
All errors are WikitextExpandError. Common codes include TEMPLATE_NOT_FOUND, TEMPLATE_LOOP, UNSUPPORTED_CALL, FINAL_PARSE_FAILED, and ABORTED.
Results and diagnostics
expansion.recordsrecords each call'skind, name, original range, expanded range, and status (expanded/preserved).expansion.diagnosticscoordinates are based onoriginal.source.document.diagnosticsmerges final parse diagnostics with expansion diagnostics mapped onto the expanded source.prepareWikitext(document)carries these diagnostics into the render result.
Nested calls map only to their top-level record; there is no precise source mapping inside templates.
How preserved calls render
Templates, functions, and arguments preserved after a failed expansion continue to render as placeholders. They are never presented as evaluated content. Template source and expansion results are treated as untrusted wikitext and still go through semantic normalization, HAST conversion, and final sanitization.
Limitations
- Include filtering is a single pass; nested
noinclude/onlyincludeinsideincludeonlycontent is not recognized again. - Lua is not executed and no site API client is built in.
- There is no cross-request template cache; implement caching in the resolver.
- Categories, references, and tables of contents in expanded output follow the normal rendering rules.