Skip to content
grucloudPublic

About

Render PlantUML mindmap diagrams to SVG in plain JavaScript

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

14 Commits

Folders and files

Repository files navigation

@grucloud/mindmap

Render PlantUML mindmap diagrams to SVG in plain JavaScript. It needs no Java, no PlantUML server and no dependencies.

Install

npm install @grucloud/mindmap

The package needs Node.js 22.12 or later. It has no Node-specific code outside the CLI, so it also runs in browsers through a bundler.

Usage

import { render } from '@grucloud/mindmap';

const svg = render()(`
@startmindmap
* Root
** Child
left side
** Other child
@endmindmap
`);

render(options) returns a function from source to SVG, so it drops straight into a pipeline, for example with rubico:

import { pipe } from 'rubico';
import { readFile, writeFile } from 'node:fs/promises';
import { render } from '@grucloud/mindmap';

const toSvgFile = pipe([
  (path) => readFile(path, 'utf8'),
  render({ theme: { nodeFill: '#E3F2FD' } }),
  (svg) => writeFile('mindmap.svg', svg),
]);

await toSvgFile('mindmap.puml');

From CommonJS, Node's built-in require() of ES modules works:

const { render } = require('@grucloud/mindmap');
const svg = render()(source);

API

Export Description
render(options?)(source) Returns a function that turns PlantUML text into an SVG string. options.theme overrides defaultTheme; options.measureText replaces the text-width estimate.
parse(source) PlantUML text → document (node tree, title, legend, style, direction…).
defaultTheme The default theme values.
estimateText(text, font) The default text-width estimate, so a custom measureText can fall back to it.

Invalid input throws a SyntaxError that names the line. TypeScript declarations are included. Nothing else is public: the modules under src/ can change in any release.

CLI

The CLI reads from a file or from stdin, and writes to a file or to stdout:

npx @grucloud/mindmap examples/basic.puml -o basic.svg
cat diagram.puml | npx @grucloud/mindmap > diagram.svg

Once the package is installed, the command is grucloud-mindmap.

Architecture

source ──▶ parser.js ──▶ document ──▶ layout.js ──▶ boxes + edges ──▶ svg.js ──▶ SVG string
                            │             ▲    ▲
                            │   creole.js ┘    └ theme.js + style.js (computed styles)
                            └ <style> text ──▶ style.js (rules)
Module Role
src/parser.js PlantUML text → document (node tree, title, legend, style text…)
src/creole.js Creole markup → styled runs; measures, wraps and positions text
src/style.js <style> blocks → rules; matching for nodes, arrows and elements
src/theme.js Defaults (override with options.theme) and style resolution
src/measure.js Text width estimate (replace with options.measureText)
src/layout.js Two-sided tree layout, left/right or top-to-bottom
src/svg.js Boxes, edges, rich text, icons and decorations → SVG
src/icons.js Open Iconic icons for <&name> (MIT, generated)

Supported syntax

Feature Syntax
Nodes * OrgMode bullets, + / - arithmetic notation
Sides left side / right side
Inline color **[#Orange] text, **[#FFBBCC] text
Boxless nodes **_ text
Multiline nodes **:line 1 … line n;, also with color, _ and a trailing <<stereotype>>. Use <U+003B> for a literal ;
Stereotypes ** text <<green>>, styled by .green { … }
Comments ' comment
Decorations title, caption, [left|center|right] header, [left|center|right] footer, legend [top|bottom] [left|center|right], each single-line or multi-line (end title, endheader, endfooter, endlegend)
Direction top to bottom direction, right to left direction, left to right direction
Skinparam skinparam monochrome true (other skinparams are parsed and ignored)

Creole

  • Inline: **bold**, //italic//, ""mono"", --stroke--, __underline__, ~~wave~~, ~ to escape a character.
  • HTML-style tags: <b> <i> <u> <s> <w> <plain> <color:red> <size:18> <back:yellow> <font:serif>.
  • Icons: <&icon-name> uses Open Iconic names. Unknown names stay as text.
  • In multiline text: * bullets, # numbered, = headings, ---- / ==== / .... separators, and <code>…</code> verbatim blocks.

<style>

<style>
mindmapDiagram {
  node { BackgroundColor lightGreen }
  :depth(1) { BackgroundColor white }      /* the root is :depth(0) */
  rootNode { FontStyle bold }
  leafNode { RoundCorner 20 }
  boxless { FontColor darkgreen }
  .green { BackgroundColor #C8E6C9 }       /* ** text <<green>> */
  arrow { LineColor #546E7A }
  :depth(2) { arrow { LineThickness 2 } }  /* arrows into depth-2 nodes */
}
title { FontSize 20 }
legend { BackgroundColor #FFF8E1 }
document { BackgroundColor #FAFAF5 }
</style>

Properties: BackgroundColor, LineColor, LineThickness, FontColor, FontSize, FontName, FontStyle, RoundCorner, Padding, MaximumWidth (wraps text).

Precedence: theme < node < rootNode / leafNode / boxless / :depth(n) < .stereotype < inline [#color]. When two rules are equally specific, the later one wins.

Limitations

  • Text widths are estimated, because Node has no font engine. Pass options.measureText for exact metrics.
  • No gradients, shadows or <img>, and no <:emoji:>. Skinparams other than monochrome are ignored.
  • In top to bottom direction, left side branches grow upwards.

Development

npm test             # node --test
npm run test:update  # regenerate fixture snapshots
npm run example      # writes examples/basic.svg

Snapshot fixtures

test/fixtures.test.js renders every test/fixtures/*.puml and compares the output byte for byte with the .svg stored next to it. It also checks that no node boxes overlap.

To add a case, drop a .puml file into test/fixtures/ and run:

npm run test:update   # writes missing .svg snapshots and refreshes changed ones

Look at the new or changed .svg files before committing them: the snapshot is the expected output.

Releasing

Releases are published from GitHub Actions (.github/workflows/publish.yml) through npm trusted publishing, so no npm token is stored anywhere. Provenance is attached automatically.

npm version patch        # or minor / major: bumps package.json, commits, tags vX.Y.Z
git push --follow-tags   # the tag triggers the publish workflow

The workflow checks that the tag matches package.json, runs the tests and publishes.

One-time setup:

  1. Publish the first version by hand: npm publish (with your 2FA code).
  2. On npmjs.com, open the package's Settings, then Trusted Publisher, and add GitHub Actions with organization grucloud, repository mindmap and workflow publish.yml. Allow direct npm publish, or change the workflow's last step to npm stage publish so each release waits for your approval.

About

Render PlantUML mindmap diagrams to SVG in plain JavaScript

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages