Mermaid
fromMermaid reads Mermaid text and gives back a nervure spec, or null when it does not know the diagram. An app that receives Mermaid blocks, from a person or from an assistant, can draw them with nervure and keep Mermaid itself for everything else.
npm install nervureimport { createChart } from 'nervure';
import { fromMermaid } from 'nervure/mermaid';
const text = `pie title Pets
"Dogs" : 386
"Cats" : 85
"Rats" : 15`;
createChart(document.querySelector('#chart')!, fromMermaid(text)!);The reader is a separate entry, nervure/mermaid (about 8 kB gzipped): it does not grow the main bundle. It has no dependency and never throws.
The fallback
fromMermaid returns null for a kind of diagram it does not know (class, state, git graph…) and for any line it does not understand. Mermaid then draws the block as it would have:
import { createChart } from 'nervure';
import { fromMermaid } from 'nervure/mermaid';
async function drawWithMermaid(host: HTMLElement, text: string) {
const { default: mermaid } = await import('mermaid');
const { svg } = await mermaid.render(`m${Date.now()}`, text);
host.replaceChildren(new DOMParser().parseFromString(svg, 'image/svg+xml').documentElement);
}
export function drawDiagram(host: HTMLElement, text: string) {
const spec = fromMermaid(text);
return spec ? createChart(host, spec) : drawWithMermaid(host, text);
}In short: fromMermaid(text) ?? drawWithMermaid(text).
What is read
| Mermaid | nervure type | Read | Left out |
|---|---|---|---|
flowchart, graph (TD, TB, BT, LR, RL) | flowchart | nodes and their shapes ([], (), ([]), [[]], [()], (()), {}, the hexagon, [//], >], @{ shape }), links -->, ---, -.->, ==>, <-->, --o, --x, with |text| or -- text -->, chains and &, style and classDef fill colours, ; between statements | subgraph frames (their nodes are kept), ~~~ links, link styles, clicks; BT and RL are drawn top to bottom and left to right |
sequenceDiagram | sequence | participant, actor, aliases (as), messages ->>, -->>, ->, -->, -x, -) (dashed for answers), autonumber, title | notes, activations; loop, alt, opt, par, critical, break, rect, box are drawn as plain messages |
gantt | gantt | dateFormat (YYYY, MM, DD, HH, mm, ss, X, x), tasks with ids, dates, durations (ms, s, m, h, d, w, M, y), after a b, until a, done (progress 1), crit (red), milestone | sections (the tasks keep their order), excludes (every day counts), axis settings |
timeline | events | periods and their events, : event lines under a period | sections |
mindmap | mindmap | the tree from the indentation, every node shape, Markdown strings | icons, classes |
pie | pie | "label" : value, title (on its own line or after pie) | showData; negative slices |
journey | journey | tasks, scores, the people taking part (as touchpoints) | sections |
quadrantChart | quadrant | axes Low --> High, quadrant-1 to quadrant-4, points from 0 to 1 with their colour (color: or a class) | point radius and stroke |
erDiagram | er | entities, aliases (p[Person]), attributes (type, name, PK, FK, UK), relations with both ends (||--o{, and words such as one or more to zero or one), written places (1 to 0..n) | attribute comments |
kanban | kanban | columns and cards, assigned (owner), ticket or priority (tag) | the ticket link |
xychart-beta | xy (bars and lines on one chart) | categories or a numeric x axis, axis titles, the y range, any number of bar and line series, horizontal; also as assistants write it: spaces between thousands ([335 678, 412 000]), unquoted titles, one value per line (bar 335678 "2023"), and no header when the lines are clearly an XY chart (a warning says so) | |
sankey-beta | sankey | CSV lines source,target,value, quoted fields |
Around every diagram: a Markdown fence, front matter (its title becomes the heading), %%{init}%% directives, %% comments, Windows line ends, accTitle and accDescr (the accessible title and description). Labels lose their quotes, <br> and other tags, and character codes such as #quot; are written out.
Warnings
parseMermaid gives the spec, the kind of diagram and what was left out:
import { parseMermaid } from 'nervure/mermaid';
const result = parseMermaid(text);
if (result) {
result.kind; // 'sequence'
result.spec; // { type: 'sequence', actors: [...], messages: [...] }
result.warnings; // ['Notes are left out.']
}An app can choose Mermaid when there are warnings, or show them beside the chart.
Without a bundler
Load cdn/nervure.mermaid.min.js after the main file: nervure.fromMermaid and nervure.parseMermaid are then there.
<script src="https://cdn.jsdelivr.net/npm/nervure@0.14.0/cdn/nervure.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nervure@0.14.0/cdn/nervure.mermaid.min.js"></script>
<script>
const spec = nervure.fromMermaid('pie\n "Yes" : 3\n "No" : 1');
if (spec) nervure.createChart(document.getElementById('chart'), spec);
</script>On a server
With nervure/render, a Mermaid block becomes an SVG string without a browser:
import { fromMermaid } from 'nervure/mermaid';
import { renderToSVG } from 'nervure/render';
const spec = fromMermaid(text);
const svg = spec ? renderToSVG(spec, { width: 800, height: 400 }) : null;