yuml2svg v4.0.0-beta6
yUML to SVG
Installation
You can install it with npm:
npm install yuml2svgOr with yarn:
yarn add yuml2svgFeatures
- Embedded rendering engine: No need to call an external web service
yUML syntax
Please refer to the wiki page.
API
The API exports a function that accepts as arguments:
- A
Readablestream, aBufferor astringcontaining the yUML diagram. - An optional plain
objectcontaining the options for the rendering. - An optional plain
objectcontaining the options for Viz.js. Check it out if you are using this package in the browser.
The API returns a Promise which resolves in a string containing SVG document
as a string.
The options for the rendering are:
dir: The direction of the diagram "TB" (default) - topDown, "LR" - leftToRight, "RL" - rightToLefttype: The type of SVG - "class" (default), "usecase", "activity", "state", "deployment", "package".isDark: Option to get dark or light diagram
Here are some examples of a simple usage you can make of the API:
import fs from "fs/promises"; // N.B.: fs/promises is not available before Node 10.0.0
import yuml2svg from "yuml2svg";
/**
* Renders a given file into a SVG string asynchronously
* @param {string} filePath Path to the yUML diagram
* @returns {Promise<string>} callback The SVG document that represents the yUML diagram
*/
const renderFile = filePath => fs.readFile(filePath).then(yuml2svg);
/**
* Renders a given file into a SVG string asynchronously
* @param {string} filePath Path to the yUML diagram
* @param {{dir:string, type: string, isDark: boolean}} [options]
* @param {{Module:Module, render: Function}} [vizOptions] @see https://github.com/mdaines/viz.js/wiki/2.0.0-API
* @returns {Promise<string>} callback The SVG document that represents the yUML diagram
*/
const renderFileWithOptions = (filePath, options, vizOptions) =>
fs.readFile(filePath).then(yuml => yuml2svg(yuml, options, vizOptions));
/**
* Generates a SVG file from a yUML file
* @param {string} inputFile Path to the .yuml document to read
* @param {string} outputFile Path to the .svg file to write
* @returns {Promise<>} Promise that resolves once the SVG file is written
*/
const generateSVG = async (inputFile, outputFile) => {
const yuml = await fs.readFile(inputFile);
const svg = await yuml2svg(yuml);
return await fs.writeFile(outputFile, svg);
};Or, if you don't like Promise nor async/await syntax, you can use it with
good old callbacks:
const fs = require("fs");
const yuml2svg = require("yuml2svg");
/**
* Renders a given file into a SVG string asynchronously
* @param {string} filePath Path to the yUML diagram
* @param {(Error, string)=>any} callback Async callback
*/
function renderFile(filePath, callback) {
yuml2svg(fs.createReadStream(filePath))
.then(function(svg) {
callback(null, svg);
})
.catch(callback);
}
/**
* Renders a given file into a SVG string asynchronously
* @param {string} filePath Path to the yUML diagram
* @param {{dir:string, type: string, isDark: boolean}} [options]
* @param {{Module:Module, render: Function}} [vizOptions] @see https://github.com/mdaines/viz.js/wiki/2.0.0-API
* @param {(Error, string)=>any} callback Async callback
*/
function renderFileWithOptions(filePath, options, vizOptions) {
fs.readFile(filePath, function(err, yuml) {
if (err) {
callback(err);
} else {
yuml2svg(yuml, options, vizOptions)
.then(function(svg) {
callback(null, svg);
})
.catch(callback);
}
});
}
/**
* Generates a SVG file from a yUML file
* @param {string} inputFile Path to the .yuml document to read
* @param {string} outputFile Path to the .svg file to write
* @param {(Error)=>any} callback Async callback
*/
function generateSVG(inputFile, outputFile, callback) {
fs.readFile(filePath, function(err, yuml) {
if (err) {
callback(err);
} else {
yuml2svg(yuml)
.then(function(svg) {
fs.writeFile(outputFile, svg, callback);
})
.catch(callback);
}
});
}Run on the browser
You can find a working example of a browser implementation using webpack here: yuml2svg-playground.
The Stream API in not currently supported on the browser, the API can only deal with strings.
Credits
- Thanks to the jaime-olivares's VSCode extension
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago