# CLI

Render Mermaid files or standard input to SVG, and Mermaid fences in Markdown to images, from the command line.

The package includes the `ondine` binary. The binary compiles with the same module as `ondine/compiler`. The [CLI](/docs/guides/cli) guide shows common tasks.

```sh
# Writes flow.svg beside flow.mmd
npx ondine flow.mmd
```

## Usage

```sh
# Renders the files, or standard input if there are no files
ondine [files...] [--out-dir <dir>] [--ascii] [--print] [--svg] [--worker <url>] [--restore] [--check]
```

## Arguments

### files

* **Type:** paths

The Mermaid files to render. For each file, the command writes an `.svg` file beside the source or in `--out-dir`. The name drops a `.mmd` or `.mermaid` extension. Other extensions stay, so `flow.txt` becomes `flow.txt.svg`.

```sh
# Writes a.svg and b.svg
ondine a.mmd b.mermaid
```

If there are no files, the command reads Mermaid from standard input and writes SVG to standard output.

```sh
# Reads Mermaid from standard input and writes SVG to standard output
ondine < flow.mmd > flow.svg
```

## Markdown

If a file ends in `.md` or `.markdown`, the command rewrites the file in place. Each ` ```mermaid ` fence, at any indentation, becomes a block. A later run recognizes this block and renders it again:

```html title="README.md"
<!-- ondine:eNpLy8kvT85ILCpR8AniUlBwVNDVtVNwAgBUvQYu -->
<picture>
  <source
    media="(prefers-color-scheme: dark)"
    srcset="https://ondine.sh/eNpLy8kvT85ILCpR8AniUlBwVNDVtVNwAgBUvQYu.svg?scheme=dark"
  />
  <img
    alt="Mermaid flowchart diagram"
    src="https://ondine.sh/eNpLy8kvT85ILCpR8AniUlBwVNDVtVNwAgBUvQYu.svg?scheme=light"
  />
</picture>
<!-- /ondine -->
```

* **Payload:** The source as UTF-8, compressed with zlib deflate, then encoded as base64url.
* **Alt text:** The `accTitle` of the diagram, or else its type.
* **Other fences:** The command skips fences in other languages whole. It does not rewrite fences inside blockquotes.
* **MDX:** The command does not rewrite MDX files, because MDX rejects HTML comments. Use [`ondine/remark`](/docs/api/remark) for MDX files.

## Options

### --out-dir

* **Alias:** `-o`
* **Type:** directory

The directory for rendered files. If the directory does not exist, the command creates it.

```sh
# Writes public/flow.svg
ondine flow.mmd -o public
```

### --svg

Links SVG files from Markdown instead of the Worker. For each diagram, the command writes `<name>-<n>.light.svg` and `<name>-<n>.dark.svg` beside the Markdown or in `--out-dir`. The number `<n>` follows document order.

If a previous block linked an SVG file and the Markdown no longer links it, the command removes that SVG file.

```sh
# Writes guide-1.light.svg and guide-1.dark.svg beside guide.md
ondine guide.md --svg
```

### --worker

* **Type:** URL
* **Default:** `https://ondine.sh`

The origin of the image Worker that Markdown images link. If the request path of a diagram is longer than the Worker limit of 4,096 characters, the command reports an error and does not change that diagram.

```sh
# Links a self-hosted Worker
ondine README.md --worker https://diagrams.example.com
```

### --restore

Changes each block from a previous run back into a ` ```mermaid ` fence, and removes the SVG files that the block linked.

```sh
# Restores the editable fences
ondine README.md --restore
```

### --check

Writes and removes nothing. If a run would change a file, the command reports `file is out of date` and exits with code 1.

```sh
# Fails CI if a diagram is out of date
ondine README.md --svg --check
```

### --ascii

* **Alias:** `-a`

Prints each Mermaid file, or standard input, as ASCII text and writes no files. Each character cell is 0.6 em wide and 1 em high, so labels keep their measured columns.

| SVG                     | Text                         |
| ----------------------- | ---------------------------- |
| Strokes                 | `-`, `\|`, `+`, `/`, and `\` |
| Arrowheads              | `>`, `<`, `^`, and `v`       |
| Small circles and marks | `o` and `#`                  |
| Circles and ellipses    | `(` and `)` sides            |
| Background fills        | Hide what they cover         |

Labels are the authored text, with Unicode characters unchanged. Rotated labels, such as axis titles, are left out.

```sh
# Prints the diagram as text
ondine flow.mmd --ascii
```

### --print

Prints each Mermaid file, or standard input, in the terminal and writes no files. The command needs the optional `@resvg/resvg-wasm` peer dependency and a monospace system font.

| Terminal                 | Output                  |
| ------------------------ | ----------------------- |
| Kitty, Ghostty           | Kitty graphics protocol |
| iTerm2, WezTerm, VS Code | iTerm2 inline image     |
| Other terminals          | Braille dots            |

Set `ONDINE_TERMINAL` to `kitty`, `iterm`, or `text` to select the output.

```sh
# Prints the diagram in the terminal
ondine flow.mmd --print
```

### --help

* **Alias:** `-h`

Prints the usage text and exits with code 0. The text lists the arguments, options, and global options.

```sh
# Shows the usage text
ondine --help
```

## Agents

The command also serves coding agents. Each item below runs instead of a render:

* **`--llms-full`:** Prints a Markdown manifest of the arguments and options.
* **`--schema`:** Prints the JSON Schema of the arguments and options. Add `--format json` for JSON.
* **`--mcp`:** Starts a Model Context Protocol server on standard input and output.
* **`mcp add`:** Registers the MCP server with the installed agents.
* **`skills add`:** Installs a skill file that documents the command.

```sh
# Registers ondine as an MCP server for the installed agents
ondine mcp add
```

The names `completions`, `mcp`, and `skills` select these commands. If the first file has a similar name, such as `mcp.md`, give it a path such as `./mcp.md`.

## Output

The command writes each report on standard error:

* **Rendered files:** `source -> output`. A Markdown run reports only the files that it changes.
* **Removed files:** `source: removed file`.
* **Errors:** `file:line:column: message`, or `file: message` if the error has no position. An error in a Markdown fence has a position in the Markdown file.
* **Standard input:** The name `<stdin>`.

```txt
flow.mmd -> flow.svg
bad.mmd:3:1: Parse error on line 3:
```

If an input fails to render or a file is out of date, the command writes one summary line on standard output after the reports. With `--json`, the summary is a JSON object with `code` and `message`.

| Code                | Meaning                                               |
| ------------------- | ----------------------------------------------------- |
| `RENDER_FAILED`     | At least one diagram failed to render.                |
| `OUT_OF_DATE`       | With `--check`, at least one file is out of date.     |
| `PRINT_UNAVAILABLE` | With `--print`, resvg or a monospace font is missing. |

```txt
Error (RENDER_FAILED): 1 diagram failed to render
```

## Exit Codes

| Code | Meaning                                                                |
| ---- | ---------------------------------------------------------------------- |
| `0`  | Every input rendered, or with `--check`, every file is up to date.     |
| `1`  | At least one input failed to render. The remaining files still render. |
| `1`  | With `--check`, at least one file is out of date.                      |
