> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# unified

```ts
import { wikitextParse, wikitextRehype, wikitextFinalize } from "react-wikitext/unified";
```

`/unified` loads neither the default parser nor React. It exposes the pipeline as three unified plugins for hosts that already own a processor and need custom transforms in between.

:::warning
Raw mode does not add the security stage or arrange plugin order for you. When you need the fixed order, prefer [`prepareWikitext`](/api/prepare.md).
:::

## A typical processor

```ts
import { unified } from "unified";
import { createWikiparser } from "react-wikitext/parse";
import { wikitextParse, wikitextRehype, wikitextFinalize } from "react-wikitext/unified";

const parser = createWikiparser();

const processor = unified()
  .use(wikitextParse({ parser }))
  .use(semanticPlugin) // runs on the semantic tree
  .use(wikitextRehype({ resolveLink })) // semantic tree → HAST, not yet sanitized
  .use(rehypePlugin) // custom HAST transforms
  .use(wikitextFinalize()); // sanitize + finalize + render snapshot
```

## wikitextParse

```ts
wikitextParse({ parser, parseOptions? }): WikitextUnifiedPlugin<WikitextRoot, WikitextRoot>
```

- Parses the source into `WikitextDocument` and stores it on the VFile.
- Returns an independent working tree; parsing the same VFile again resets library fields but does not clear the caller's other `data`.
- A parser is required; `/unified` never loads `wikiparser-node` implicitly.

## wikitextRehype

```ts
wikitextRehype(options?): WikitextUnifiedPlugin<WikitextRoot, Root>
```

- Reads the parse result from the VFile and uses the tree the processor actually passes in, not a stale cached tree.
- Validates the working tree and its budget, reassigns ids, and converts to HAST.
- Stores the preparation context before the security stage.
- Options contain preparation settings only (resolvers, `policy`, `labels`, `headingIdStrategy`, and `handlers`).
- It does **not** run `wikitextPlugins` / `rehypePlugins`; passing either field (even an empty array) throws `PREPARE_FAILED`. Register plugins on the processor with `.use(...)`.

## wikitextFinalize

```ts
wikitextFinalize({ headingIdStrategy? }): WikitextUnifiedPlugin<Root, Root>
```

- Must run after every rehype plugin.
- Sanitizes and finalizes, then writes `wikitextRenderDocument` to the VFile.
- An explicit `headingIdStrategy` overrides the preparation setting for both the final headings and snapshot meta.
- Nothing can be inserted afterwards; HAST that skips this plugin is not a render document.

## Errors and prerequisites

- Without `wikitextParse`, `wikitextRehype` throws `WikitextPrepareError("INVALID_DOCUMENT")` with `stage: "prepare"`.
- Without `wikitextRehype`, `wikitextFinalize` throws `WikitextPrepareError("PREPARE_FAILED")`.
- These errors do not repair plugin order; inspect the processor chain.

## When not to use raw mode

| Need                              | Use                                        |
| --------------------------------- | ------------------------------------------ |
| Only add semantic or HAST plugins | The plugin options of `prepareWikitext`    |
| A fixed sanitize order            | `prepareWikitext` / `prepareWikitextAsync` |
| Expand templates                  | `/expand` + `/hast`                        |
| React view components             | `/view` or the root entry                  |

## Related

- [Preparation `/hast`](/api/prepare.md): fixed security order, resolvers, and policy.
- [AST `/ast`](/api/ast.md): semantic nodes and queries.
- [Errors and diagnostics](/api/errors.md): `WikitextPrepareError` fields.
