›
byrcsc/laravel-cartographer · 1.x
Render SVG and PNG files through a local mermaid-cli installation.
Text output is the default because it reviews well and every host draws it.
When you need a picture instead, --format=svg and --format=png render one
through a locally installed mermaid-cli.
npm i -D @mermaid-js/mermaid-cli
php artisan cartographer:erd --format=svg INFO Generated 9 entities and 18 edges. Written to [/srv/blog/docs/erd.svg].The package never installs a renderer. It shells out to one you already have,
and an export either finds a binary or fails saying how to get one. That keeps
composer require --dev byrcsc/laravel-cartographer free of node, a headless
browser, and a Mermaid build, none of which a Markdown run needs.
Three locations are searched, in order:
| Order | Location | When it applies |
|---|---|---|
| 1 | cartographer.export.mermaid_cli | Set, and only this path is tried |
| 2 | node_modules/.bin/mmdc in the project | The configured path is null |
| 3 | mmdc on your PATH | Nothing was found in node_modules |
mmdc, mmdc.cmd, and mmdc.exe are all recognised, so an npm install on
Windows resolves the same way.
Setting export.mermaid_cli stops the search rather than adding to it. A
configured path that does not exist or is not executable fails immediately,
instead of silently falling through to a different binary than the one you
named:
ERROR The mermaid-cli binary configured at [/opt/mmdc] is missing or not
executable. Correct [cartographer.export.mermaid_cli], or remove it
to search the project and PATH.With nothing configured and nothing found:
ERROR No mermaid-cli binary was found in [node_modules/.bin/mmdc] or on
PATH. Install it with [npm i -D @mermaid-js/mermaid-cli], or point
[cartographer.export.mermaid_cli] at the binary.php artisan cartographer:erd --format=svg --output=docs/erd.mddocs/erd.svg the rendered diagram
docs/erd.svg.mmd the Mermaid source it came fromTwo things happen to the path. The extension is replaced with the format, so
pointing --format=svg at the default docs/erd.md cannot leave image bytes
in a file named .md. Then the source is written beside the image, at the
image path plus .mmd.
The source file is not a convenience. It is what
cartographer:check compares, and the next section
is why.
mermaid-cli output is not byte-stable between its own versions. The same diagram rendered by two releases differs in generated identifiers, spacing, and sometimes layout, none of which reflect a change to your schema.
If the check compared the image, every renderer upgrade would report every
diagram as stale, and the signal would be gone within a release or two.
Comparing the .mmd beside it fixes that: the source moves only when your
models or your schema move, which is the thing the check exists to catch.
The consequence is worth stating plainly. cartographer:check never runs
mermaid-cli, so a CI job that verifies an exported diagram needs no node and no
browser. Only the job that regenerates one does.
- name: Check the ERD is current
run: php artisan cartographer:check # no node neededCommit both files. The .mmd is what makes the image checkable, and dropping
it turns the check into a permanent "missing" failure.
Cartographer supplies the renderer configuration for an export and raises
maxTextSize and maxEdges far past anything a schema produces. An export
therefore succeeds on diagrams GitHub refuses to draw, and no size
warning is printed for one.
This is the escape hatch when a diagram is too big for a hosted renderer and splitting it into groups is not what you want.
php artisan cartographer:erd --format=svg --stdout > diagram.svg--stdout writes the rendered artifact to the stream and no source file, so it
is for one-off piping rather than for something you commit. A file you intend
to check needs the .mmd beside it, which means writing it normally.
The exporter runs mermaid-cli in a temporary directory and reports what it said. A non-zero exit is passed through with the renderer's own output attached:
ERROR mermaid-cli failed to render the png diagram (exit code 1).
Error: Failed to launch the browser processThat class of message is a mermaid-cli or Puppeteer problem rather than a Cartographer one, and it reproduces outside the package:
npx -y @mermaid-js/mermaid-cli -i docs/erd.svg.mmd -o /tmp/erd.svgA render that hangs is stopped after five minutes:
ERROR mermaid-cli did not finish within 300 seconds. Raise the limit by
rendering fewer entities, or check that the binary at
[/srv/blog/node_modules/.bin/mmdc] works.On a diagram large enough to reach that, --columns=keys or a
group is a better answer than a longer wait.
| Format | Use it for |
|---|---|
svg | Documentation sites, anything zoomable, anything with a lot of text |
png | Slides, issue comments, and tools that will not embed an SVG |
Prefer svg where you have the choice. A large ERD in PNG is either unreadable
or enormous, and text in an SVG stays selectable and searchable.
Fonts are the one place they differ in kind. An SVG references a whole font stack and the viewer falls through it; a PNG rasterizes with whatever fonts the rendering machine has. See themes and fonts.
Committing text is the default recommendation for a reason: a Markdown diagram diffs, and the diff is most of the value. An image does not diff, so a committed PNG tells a reviewer that something changed and nothing about what.
Export when you need a picture for a place that cannot render Mermaid, and keep
the Markdown diagram as the reviewable artifact. If you do commit an image,
commit its .mmd too and let cartographer:check cover both.