For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/concepts.md.
  • English
  • How It Works

    The library splits rendering into stages that can be deployed separately. Understanding the pipeline and the two snapshots makes it obvious when to use the root entry and when to parse on the server.

    Pipeline

    source
      │
      ├─ (optional) /expand ─▶ expanded source
      │
      ▼
    /parse ─▶ WikitextDocument (parse snapshot)
      │
      ▼
    /hast prepare ─▶ WikitextRenderDocument (render snapshot)
      │
      ▼
    /view ─▶ React
    StageEntryInputOutputRequired
    Expansion/expandSource and a template resolverExpanded parse documentOptional
    Parsing/parse, /astSourceWikitextDocumentYes
    Preparation/hastParse snapshotWikitextRenderDocumentBefore rendering
    Rendering/view, root entryRender snapshotReact elementsTo render

    The result of /expand is already a parse document. Do not parse it again; pass it to prepareWikitext.

    Two snapshots

    Parse snapshot WikitextDocument

    interface WikitextDocument {
      readonly source: string;
      readonly ast: WikitextRoot;
      readonly syntax?: WikitextSyntaxRoot;
      readonly meta: WikitextDocumentMeta;
      readonly diagnostics: readonly WikitextDiagnostic[];
    }
    • ast is the normalized semantic tree; preparation never mutates it.
    • syntax exists only with retainSyntax: true, for source positions and the playground.
    • The same source with a different site config or pageName can produce a different AST.

    Render snapshot WikitextRenderDocument

    interface WikitextRenderDocument {
      readonly source: string;
      readonly ast: WikitextRoot; // working tree after plugins
      readonly hast: import("hast").Root; // final sanitized HAST
      readonly headings: readonly WikitextHeadingInfo[];
      readonly meta: WikitextRenderMeta;
      readonly diagnostics: readonly WikitextDiagnostic[];
    }
    • hast already contains the TOC, reference section, and category footer.
    • ast is the working tree after plugins; node ids have been reassigned.
    • meta records the effective policy, labels, heading strategy, and authorized images.
    • WikitextView accepts only a render snapshot, never source text.

    Queries must use the right snapshot: do not look up ids from a render snapshot in the original parse snapshot.

    Why two stages

    Parsing is expensive, while preparation options change often. Cache the parse snapshot and prepare it again with different resolvers, images, labels, or plugins instead of reparsing. Server preparation and client rendering use the same snapshot: the client loads no parser and reruns no plugins.

    Preparation works on a cloned tree, so the parse snapshot stays read-only. Snapshots are plain data: they can be cached, sent through a worker, or transferred over the network, and restore functions revalidate structure and budgets.

    Note

    Preparation is not an optional "touch-up" step. It determines the final HAST content and the security boundary; handing unprocessed HAST to the view is unsupported.

    Semantic normalization

    The upstream parser returns a syntax tree. The library normalizes structure during the semantic stage; the React layer never scans source text.

    Structures that cannot be represented safely or correctly degrade to a source card and a diagnostic; the library does not guess. Templates, parser functions, arguments, and files render explicit placeholders, so "not evaluated" is never mistaken for "evaluated".

    Source HTML is handled by category: allowlisted tags render, forbidden tags are dropped with a readable hint, and unknown tags degrade. Links and images are validated again during finalization: protocol, target, rel, and image authorization.

    The goal is explainable, auditable output, not maximal Wikipedia fidelity.

    Entry boundaries

    EntryLoads parserLoads ReactTypical use
    Root entryYesYesRender source on the client
    /viewNoYesRender a prepared snapshot
    /parseYesNoServer, worker, cache
    /expandYesNoExpand templates before parse
    /hastNoNoPrepare, serialize, restore
    /astNoNoAST queries and data pipelines
    /unifiedNoNoRaw mode with an injected parser
    /configsNoNoSite config constants

    See Entry points for the full export list.

    Warning

    React entries create Context at module load and use useId / useMemo, so they only run in Client Components. The root entry and /view ship a "use client" directive, so they work directly in Next.js App Router and similar environments. On the server, only call React-free entries such as /parse and /hast.

    Glossary

    TermMeaning
    wikitextMediaWiki-style page source
    Parse snapshotThe semantic result of source plus site config (WikitextDocument)
    Render snapshotThe result after plugins, HAST conversion, sanitization, and finalization (WikitextRenderDocument)
    ResolverHost callbacks such as resolveLink, resolveImage, resolveTemplate
    HandlerA prepareOptions.handlers function converting a custom semantic node to HAST
    PolicyURL, tag, and class rules in prepareOptions.policy
    LabelsprepareOptions.labels, every user-visible placeholder and hint

    Next steps