Skip to content
glyphcss

Mermaid and JSON diagrams as ASCII text

@glyphcss/diagrams turns Mermaid source and JSON data into monospace text. It measures labels, lays out nodes, routes connections, and paints character cells. It needs no scene, canvas element, or WebGL context.

Terminal window
npm i @glyphcss/diagrams
import { renderGlyphDiagram } from "@glyphcss/diagrams";
const result = await renderGlyphDiagram(
`flowchart LR
Push[Push commit] --> Build[Build app]
Build --> Test{Tests pass?}
Test -->|yes| Stage[Deploy staging]
Stage --> Smoke[Smoke tests]
Smoke --> Prod[Deploy prod]
Test -->|no| Fix[Fix build]`,
{ target: "chat", width: 72, height: 19, overflow: "expand" },
);
console.log(result.text);
        ┌─────────────┐    ┌───────────┐    ╭───────────────╮           
        │ Push commit │────▶ Build app │────▶ ◆ Tests pass? │           
        └─────────────┘    └───────────┘    ╰───────────────╯           
                                                   │ │                  
                ┌──────────────────────────────────┘ │ no               
                │             yes                    │                  
                │                             ┌──────▼────┐             
                │                             │ Fix build │             
                │                             └───────────┘             
                │                                                       
        ┌───────▼────────┐    ┌─────────────┐    ┌─────────────┐        
        │ Deploy staging │────▶ Smoke tests │────▶ Deploy prod │        
        └────────────────┘    └─────────────┘    └─────────────┘        

Targets set default cell budgets, character sets, and color encodings. A cell is one monospace character position; width counts columns and height counts rows. These previews use explicit compact sizes and color: "none" so terminal escape codes do not appear in HTML.

TargetDefault columns × rowsCharsetColor
chat72 × 24boxnone
terminal80 × 24brailletruecolor
web (API default)96 × 32braillecss
                                 yes ┌────────────┐  ┌──────────────┐   
                                  ┌──▶ Load order │──▶ Return order │   
   ┌───────────────┐  ╭─────────╮ │  └────────────┘  └──────────────┘   
   │ Order request │──▶ ◆ Token ├─┤                                     
   └───────────────┘  │ valid?  │ │  ┌────────────┐                     
                      ╰─────────╯ └──▶ Return 401 │                     
                                  no └────────────┘                     
Code
import { renderGlyphDiagram } from "@glyphcss/diagrams";

const { text } = await renderGlyphDiagram(`
flowchart LR; Request[Order request] --> Auth{Token valid?}; Auth -->|yes| Load[Load order]; Load --> Reply[Return order]; Auth -->|no| Deny[Return 401]
`, {
  target: "chat",
  color: "none",
  width: 72,
  height: 12
});
console.log(text);
                                 yes ┌────────────┐  ┌──────────────┐   
                                  ┌──▶ Load order │──▶ Return order │   
   ┌───────────────┐  ╭─────────╮ │  └────────────┘  └──────────────┘   
   │ Order request │──▶ ◆ Token ├─┤                                     
   └───────────────┘  │ valid?  │ │  ┌────────────┐                     
                      ╰─────────╯ └──▶ Return 401 │                     
                                  no └────────────┘                     
Code
import { renderGlyphDiagram } from "@glyphcss/diagrams";

const { text } = await renderGlyphDiagram(`
flowchart LR; Request[Order request] --> Auth{Token valid?}; Auth -->|yes| Load[Load order]; Load --> Reply[Return order]; Auth -->|no| Deny[Return 401]
`, {
  target: "terminal",
  color: "none",
  width: 72,
  height: 12
});
console.log(text);
                                 yes ┌────────────┐  ┌──────────────┐   
                                  ┌──▶ Load order │──▶ Return order │   
   ┌───────────────┐  ╭─────────╮ │  └────────────┘  └──────────────┘   
   │ Order request │──▶ ◆ Token ├─┤                                     
   └───────────────┘  │ valid?  │ │  ┌────────────┐                     
                      ╰─────────╯ └──▶ Return 401 │                     
                                  no └────────────┘                     
Code
import { renderGlyphDiagram } from "@glyphcss/diagrams";

const { text } = await renderGlyphDiagram(`
flowchart LR; Request[Order request] --> Auth{Token valid?}; Auth -->|yes| Load[Load order]; Load --> Reply[Return order]; Auth -->|no| Deny[Return 401]
`, {
  target: "web",
  width: 72,
  height: 12
});
console.log(text);

Diagram borders use whole cells in every charset. Targets do not change a diagram into a raster image.

InputImport and function
Mermaid graph families, including gitGraph; JSON graph@glyphcss/diagrams: renderGlyphDiagram
Mermaid sequence; JSON sequence@glyphcss/diagrams/sequence: renderGlyphSequence
JSON lane DAG; git-log text@glyphcss/diagrams/lanes: renderGlyphLaneDag

Every renderer returns text, optional html, pages, meta, and report. The fidelity report describes adaptations made while reading and fitting the source. Its ledger is a list of { code, message, detail? } entries. Read it when labels, shapes, or panel boundaries differ from your source.