# @unified-latex/unified-latex-util-replace

> Functions for modifying a unified-latex AST

Latest version **1.8.4** (published 2026-04-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install @unified-latex/unified-latex-util-replace
pnpm add @unified-latex/unified-latex-util-replace
yarn add @unified-latex/unified-latex-util-replace
bun add @unified-latex/unified-latex-util-replace
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.8.4 |
| Published | 2026-04-03 |
| First published | 2022-05-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 6 |
| Unpacked size | 105.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 127 |
| Author | Jason Siefken |
| Maintainers | siefkenj |
| Keywords | pegjs, latex, parser, prettier, unified-latex, unified |

## Links

- npm: https://www.npmjs.com/package/@unified-latex/unified-latex-util-replace
- Repository: https://github.com/siefkenj/unified-latex
- Homepage: https://github.com/siefkenj/unified-latex#readme
- Issues: https://github.com/siefkenj/unified-latex/issues
- npm.io page: https://npm.io/package/@unified-latex/unified-latex-util-replace

## Dependencies (6)

- [unified](https://npm.io/package/unified.md) ^10.1.2
- [@unified-latex/unified-latex-types](https://npm.io/package/@unified-latex/unified-latex-types.md) ^1.8.4
- [@unified-latex/unified-latex-util-trim](https://npm.io/package/@unified-latex/unified-latex-util-trim.md) ^1.8.4
- [@unified-latex/unified-latex-util-match](https://npm.io/package/@unified-latex/unified-latex-util-match.md) ^1.8.4
- [@unified-latex/unified-latex-util-split](https://npm.io/package/@unified-latex/unified-latex-util-split.md) ^1.8.4
- [@unified-latex/unified-latex-util-visit](https://npm.io/package/@unified-latex/unified-latex-util-visit.md) ^1.8.4

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 1.8.4 (latest) — 2026-04-03
- 1.8.3 — 2025-06-16
- 1.8.2 — 2025-02-09
- 1.8.0 — 2024-08-22
- 1.7.1 — 2024-03-19
- 1.7.0 — 2024-02-25
- 1.6.1 — 2024-02-18
- 1.6.0 — 2024-01-17
- 1.4.2 — 2023-09-30
- 1.4.0 — 2023-06-20
- 1.3.2 — 2023-04-26
- 1.3.1 — 2023-03-05
- 1.3.0 — 2023-02-06
- 1.2.2 — 2022-12-15
- 1.2.1 — 2022-11-23
- … 11 more at https://npm.io/package/@unified-latex/unified-latex-util-replace/versions

## README

<!-- DO NOT MODIFY -->
<!-- This file was autogenerated by build-docs.ts -->
<!-- Edit the docstring in index.ts and regenerate -->
<!-- rather than editing this file directly. -->
# unified-latex-util-replace

## What is this?

Functions to help modify a `unified-latex` Abstract Syntax Tree (AST).

## When should I use this?

If you want to recursively replace particular AST nodes.

## Install

```bash
npm install @unified-latex/unified-latex-util-replace
```

This package contains both esm and commonjs exports. To explicitly access the esm export,
import the `.js` file. To explicitly access the commonjs export, import the `.cjs` file.

# Plugins

## `unifiedLatexReplaceStreamingCommands`

Unified plugin to replace all found streaming commands with their argument-style equivalents.
This only applies to sections of the tree with no math ancestor.

### Usage

`unified().use(unifiedLatexReplaceStreamingCommands[, options])`

#### options

```typescript
PluginOptions
```

### Type

`Plugin<PluginOptions[], Ast.Root, Ast.Root>`

```typescript
function unifiedLatexReplaceStreamingCommands(
  options: PluginOptions
): (tree: Ast.Root) => void;
```

where

```typescript
type PluginOptions = {
  replacers: Record<
    string,
    (content: Ast.Node[], streamingCommand: Ast.Macro) => Ast.Node | Ast.Node[]
  >;
};
```

# Functions

## `firstSignificantNode(nodes, parbreaksAreInsignificant)`

Returns the first non-whitespace/non-comment node in `nodes`. If there is no such
node, `null` is returned.

```typescript
function firstSignificantNode(
  nodes: Ast.Node[],
  parbreaksAreInsignificant: Boolean
): Ast.Node;
```

**Parameters**

| Param                     | Type         |
| :------------------------ | :----------- |
| nodes                     | `Ast.Node[]` |
| parbreaksAreInsignificant | `Boolean`    |

## `firstSignificantNodeIndex(nodes, parbreaksAreInsignificant)`

Returns the index of the first non-whitespace/non-comment node in `nodes`. If there is no such
node, `null` is returned.

```typescript
function firstSignificantNodeIndex(
  nodes: Ast.Node[],
  parbreaksAreInsignificant: Boolean
): number;
```

**Parameters**

| Param                     | Type         |
| :------------------------ | :----------- |
| nodes                     | `Ast.Node[]` |
| parbreaksAreInsignificant | `Boolean`    |

## `lastSignificantNode(nodes, parbreaksAreInsignificant)`

Returns the last non-whitespace/non-comment node in `nodes`. If there is no such
node, `null` is returned.

```typescript
function lastSignificantNode(
  nodes: Ast.Node[],
  parbreaksAreInsignificant: Boolean
): Ast.Node;
```

**Parameters**

| Param                     | Type         |
| :------------------------ | :----------- |
| nodes                     | `Ast.Node[]` |
| parbreaksAreInsignificant | `Boolean`    |

## `lastSignificantNodeIndex(nodes, parbreaksAreInsignificant)`

Returns the index of the last non-whitespace/non-comment node in `nodes`. If there is no such
node, `null` is returned.

```typescript
function lastSignificantNodeIndex(
  nodes: Ast.Node[],
  parbreaksAreInsignificant: Boolean
): number;
```

**Parameters**

| Param                     | Type         |
| :------------------------ | :----------- |
| nodes                     | `Ast.Node[]` |
| parbreaksAreInsignificant | `Boolean`    |

## `replaceNode(ast, visitor)`

Recursively replace nodes in `ast`. The `visitor` function is called on each node. If
`visitor` returns a node or an array of nodes, those nodes replace the node passed to `visitor`.
If `null` is returned, the node is deleted. If `undefined` is returned, no replacement happens.

```typescript
function replaceNode(
  ast: Ast.Ast,
  visitor: (
    node: Ast.Node | Ast.Argument,
    info: VisitInfo
  ) =>
    | Ast.Node
    | Ast.Argument
    | (Ast.Node | Ast.Argument)[]
    | null
    | undefined
    | void
): void;
```

**Parameters**

| Param   | Type                              |
| :------ | :-------------------------------- |
| ast     | `Ast.Ast`                         |
| visitor | <span color='gray'>Omitted</span> |

## `replaceNodeDuringVisit(replacement, info)`

Replaces the current node with `replacement`. It is assumed that the current
node is in an array that is a child of a parent element. If this is not the case,
the function will error.

```typescript
function replaceNodeDuringVisit(
  replacement: Ast.Node | Ast.Argument | (Ast.Node | Ast.Argument)[],
  info: VisitInfo
): void;
```

**Parameters**

| Param       | Type                              |
| :---------- | :-------------------------------- |
| replacement | <span color='gray'>Omitted</span> |
| info        | `VisitInfo`                       |

where

```typescript
type VisitInfo = {
  /**
   * If the element was accessed via an attribute, the attribute key is specified.
   */
  readonly key: string | undefined;
  /**
   * If the element was accessed in an array, the index is specified.
   */
  readonly index: number | undefined;
  /**
   * A list of ancestor nodes, `[parent, grandparent, great-grandparent, ...]`
   */
  readonly parents: (Ast.Node | Ast.Argument)[];
  /**
   * If the element was accessed in an array, the array that it is part of.
   */
  readonly containingArray: (Ast.Node | Ast.Argument)[] | undefined;
  /**
   * The LaTeX context of the current match.
   */
  readonly context: VisitorContext;
};
```

## `replaceStreamingCommand(ast, isStreamingCommand, replacer, options)`

Given a group or a node array, look for streaming commands (e.g., `\bfseries`) and replace them
with the specified macro. The "arguments" of the streaming command are passed to `replacer` and the return
value of `replacer` is inserted into the stream.

By default, this command will split at parbreaks (since commands like `\textbf{...} do not accept parbreaks in their
contents) and call `replacer\` multiple times, once per paragraph.

Commands are also split at environments and at any macros listed in `macrosThatBreakPars`.

```typescript
function replaceStreamingCommand(
  ast: Ast.Group | Ast.Node[],
  isStreamingCommand: (node: any) => node is Ast.Macro,
  replacer: (
    content: Ast.Node[],
    streamingCommand: Ast.Macro
  ) => Ast.Node | Ast.Node[],
  options: {
    macrosThatBreakPars?: string[];
    environmentsThatDontBreakPars?: string[];
  }
): Ast.Node[];
```

**Parameters**

| Param              | Type                              |
| :----------------- | :-------------------------------- |
| ast                | `Ast.Group \| Ast.Node[]`         |
| isStreamingCommand | <span color='gray'>Omitted</span> |
| replacer           | <span color='gray'>Omitted</span> |
| options            | <span color='gray'>Omitted</span> |

## `replaceStreamingCommandInGroup(group, isStreamingCommand, replacer, options)`

Process streaming commands in a group. If needed, "escape" the group.
For example, `{\bfseries xx}` -> `\textbf{xx}`, but `{foo \bfseries xx}` -> `{foo \textbf{xx}}`.

```typescript
function replaceStreamingCommandInGroup(
  group: Ast.Group,
  isStreamingCommand: (node: any) => node is Ast.Macro,
  replacer: (
    content: Ast.Node[],
    streamingCommand: Ast.Macro
  ) => Ast.Node | Ast.Node[],
  options: {
    macrosThatBreakPars?: string[];
    environmentsThatDontBreakPars?: string[];
  }
): Ast.Node[];
```

**Parameters**

| Param              | Type                              |
| :----------------- | :-------------------------------- |
| group              | `Ast.Group`                       |
| isStreamingCommand | <span color='gray'>Omitted</span> |
| replacer           | <span color='gray'>Omitted</span> |
| options            | <span color='gray'>Omitted</span> |

---
_Source: https://npm.io/package/@unified-latex/unified-latex-util-replace · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
