# chenyanchen/mermaid-formatter architecture

> Architecture diagram of the GitHub repository chenyanchen/mermaid-formatter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chenyanchen/mermaid-formatter
- Repository: https://github.com/chenyanchen/mermaid-formatter

## Overview

mermaid-formatter is a lightweight Mermaid syntax formatter with a CLI and programmatic API, plus Prettier integration. The main workflow accepts diagram text, identifies whether its indentation is semantics-sensitive, parses ordinary diagrams into a statement AST, then formats that AST. Markdown fenced blocks reuse the API. The source excerpts show the API orchestration and core parser/formatter calls; relationships for the separately registered Prettier plugin are documented by its source. No runtime storage or external service is present.

Main entry points: [`src/index.ts`](https://github.com/chenyanchen/mermaid-formatter/blob/main/src/index.ts) (Public API), [`src/parser.ts`](https://github.com/chenyanchen/mermaid-formatter/blob/main/src/parser.ts) (Diagram Parser), [`src/cli.ts`](https://github.com/chenyanchen/mermaid-formatter/blob/main/src/cli.ts) (CLI), [`src/prettier-plugin.ts`](https://github.com/chenyanchen/mermaid-formatter/blob/main/src/prettier-plugin.ts) (Prettier Plugin)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["User Entry Points"]
  node_cli["CLI<br/>[cli.ts]"]
  node_plugin["Prettier Plugin<br/>[prettier-plugin.ts]"]
  node_api["Public API<br/>[index.ts]"]
  node_markdown["Markdown Blocks<br/>[index.ts]"]
end

subgraph group_core["Formatting Core"]
  node_typepolicy["Indent Policy<br/>[rules.ts]"]
  node_formatter["AST Formatter<br/>[formatter.ts]"]
end

subgraph group_syntax["Syntax Model"]
  node_parser["Diagram Parser<br/>[parser.ts]"]
  node_grammar["Grammar Rules<br/>[rules.ts]"]
  node_ast["Diagram AST<br/>[types.ts]"]
end

node_user(("User"))
node_prettier["Prettier"]

node_user -->|"invokes"| node_cli
node_cli -->|"calls"| node_api
node_user -->|"calls"| node_api
node_prettier -->|"loads"| node_plugin
node_plugin -->|"calls"| node_api
node_api -->|"checks policy"| node_typepolicy
node_api -->|"parses with"| node_parser
node_parser -->|"matches syntax"| node_grammar
node_api -->|"formats with"| node_formatter
node_parser -->|"produces"| node_ast
node_formatter -->|"reads"| node_ast
node_api -->|"formats blocks"| node_markdown
node_markdown -->|"formats Mermaid"| node_api

click node_cli "https://github.com/chenyanchen/mermaid-formatter/blob/main/src/cli.ts"
click node_plugin "https://github.com/chenyanchen/mermaid-formatter/blob/main/src/prettier-plugin.ts"
click node_api "https://github.com/chenyanchen/mermaid-formatter/blob/main/src/index.ts"
click node_markdown "https://github.com/chenyanchen/mermaid-formatter/blob/main/src/index.ts"
click node_typepolicy "https://github.com/chenyanchen/mermaid-formatter/blob/main/src/rules.ts"
click node_parser "https://github.com/chenyanchen/mermaid-formatter/blob/main/src/parser.ts"
click node_grammar "https://github.com/chenyanchen/mermaid-formatter/blob/main/src/rules.ts"
click node_ast "https://github.com/chenyanchen/mermaid-formatter/blob/main/src/types.ts"
click node_formatter "https://github.com/chenyanchen/mermaid-formatter/blob/main/src/formatter.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_plugin,node_api,node_markdown,node_user toneBlue
class node_typepolicy,node_formatter toneAmber
class node_parser,node_grammar,node_ast toneMint
class node_prettier toneIndigo
```

## Components

### User Entry Points

- [CLI](https://github.com/chenyanchen/mermaid-formatter/blob/main/src/cli.ts): `src/cli.ts`
- [Prettier Plugin](https://github.com/chenyanchen/mermaid-formatter/blob/main/src/prettier-plugin.ts): `src/prettier-plugin.ts`
- [Public API](https://github.com/chenyanchen/mermaid-formatter/blob/main/src/index.ts): `src/index.ts`
- [Markdown Blocks](https://github.com/chenyanchen/mermaid-formatter/blob/main/src/index.ts): `src/index.ts`

### Formatting Core

- [Indent Policy](https://github.com/chenyanchen/mermaid-formatter/blob/main/src/rules.ts): `src/rules.ts`
- [AST Formatter](https://github.com/chenyanchen/mermaid-formatter/blob/main/src/formatter.ts): `src/formatter.ts`

### Syntax Model

- [Diagram Parser](https://github.com/chenyanchen/mermaid-formatter/blob/main/src/parser.ts): `src/parser.ts`
- [Grammar Rules](https://github.com/chenyanchen/mermaid-formatter/blob/main/src/rules.ts): `src/rules.ts`
- [Diagram AST](https://github.com/chenyanchen/mermaid-formatter/blob/main/src/types.ts): `src/types.ts`

### Other components

- **User**
- **Prettier**

## Connections

- User → CLI: invokes (evidence: `README.md`)
- CLI → Public API: calls (evidence: `src/cli.ts`)
- User → Public API: calls (evidence: `README.md`)
- Prettier → Prettier Plugin: loads (evidence: `README.md`)
- Prettier Plugin → Public API: calls (evidence: `src/prettier-plugin.ts`)
- Public API → Indent Policy: checks policy (evidence: `src/index.ts`)
- Public API → Diagram Parser: parses with (evidence: `src/index.ts`)
- Diagram Parser → Grammar Rules: matches syntax (evidence: `src/parser.ts`)
- Public API → AST Formatter: formats with (evidence: `src/index.ts`)
- Diagram Parser → Diagram AST: produces (evidence: `src/parser.ts`)
- AST Formatter → Diagram AST: reads (evidence: `src/formatter.ts`)
- Public API → Markdown Blocks: formats blocks (evidence: `src/index.ts`)
- Markdown Blocks → Public API: formats Mermaid (evidence: `src/index.ts`)
