yUML to SVG
This project is a fork of jaime-olivares/yuml-diagram. You might want to check it out if you are more interested in a synchronous version of the API or you want to use an older version of Node.js.
Allows the creation of offline UML diagrams based on the yUML Syntax.
Support
- Node.js v12+ (yuml2svg v5 uses ECMAScript modules, support is still experimental at the time of writing).
- Browser support through import maps or bundling (WebPack, Rollup, etc.).
- Unofficial support for Node.js v10 LTS (with the
--experimental-modules
and--experimental-worker
CLI flags). - Experimental support for Deno.
Installation
You can install it with yarn:
yarn global add yuml2svg # For CLI usage yarn add yuml2svg # As local dependency
Or with npm:
npm --global install yuml2svg # For CLI usage npm install yuml2svg # As local dependency
Features
- Currently, the following diagram types are supported:
- Class
- Activity
- Use-case
- State
- Deployment
- Package
- Sequence
- Additional directives for altering diagram type and orientation
- Embedded rendering engine: No need to call an external web service
yUML syntax
Please refer to the wiki page.
Usage
CLI
You can use the package to transform yUML diagrams to SVG via the Command-Line Interface.
# You can install the package globally (or use npx) yarn global add yuml2svg # Prints SVG document on the standard output yuml2svg < diagram.yuml # Note: On Windows Powershell, you may need to use cat command instead cat diagram.yuml | yuml2svg # Save SVG file to the disk yuml2svg < diagram.yuml > diagram.svg # Save SVG file to the disk using dark mode yuml2svg --dark < diagram.yuml > diagram.svg
Node.JS API
The API exports a function that accepts as arguments:
- A
Readable
stream, aBuffer
or astring
containing the yUML diagram. - An optional plain
object
containing the options for the rendering. - An optional plain
object
containing the options for Viz.js. Check it out if you are using this package in the browser. - An optional plain
object
containing the render options for Viz.js.
The API returns a Promise
which resolves in a string containing SVG document
as a string
.
The options for the rendering are:
dir
:string
The direction of the diagram "TB" (default) - topDown, "LR" - leftToRight, "RL" - rightToLefttype
:string
The type of SVG - "class" (default), "usecase", "activity", "state", "deployment", "package", "sequence".isDark
:boolean
Option to get dark or light diagramdotHeaderOverrides
:object
Option to customize output (not supported for sequence diagram)Please check out Viz.js wiki to get more the documentation of the last two parameters.
Here are some examples of a simple usage you can make of the API:
;; /** * Renders a string or a Buffer into SVG with dark mode * @param {string | Buffer | Readable} yuml The yUML diagram * @returns */const renderDarkSVG = ; /** * Renders a given file into a SVG string asynchronously * @param * @returns */const renderFile = ; /** * Renders a given file into a SVG string asynchronously * @param * @param {{dir:string, type: string, isDark: boolean}} [options] * @param * @returns */const renderFileWithOptions = ; /** * Generates a SVG file from a yUML file * @param * @param * @returns {Promise<>} Promise that resolves once the SVG file is written */const generateSVG = async { const svg = await ; return await fspromises;};
N.B.: yuml2svg is written using ES modules, it means it cannot be required
(require('yuml2svg')
will throw); although you still can use it from a CJS
script using dynamic import:
var fs = ; /** * Renders a given file into a SVG string asynchronously * @param * @param {(Error, string)=>any} callback Async callback */ { import"yuml2svg" ;}
Run on the browser
You can find a working example of a browser implementation using webpack here: yuml2svg-playground.
If you want to use streams, pass a ReadableStreamDefaultReader
or
ReadableStreamBYOBReader
object to the API:
; const yumlOptions = {};const vizOptions = workerURL: "data:application/javascript,importScripts('https://unpkg.com/viz.js@2.1.2/full.render.js')"; ;
Note: Only UTF-8 is supported when using streams.
Credits
- Thanks to the mdaines's port of Graphviz for JavaScript viz.js.
- Thanks to the jaime-olivares's VSCode extension.