Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
json-canvas-to-mermaid
Advanced tools
A module to generate Mermaid Graph syntax from JSON Canvas data
Transform your JSON Canvas data into Mermaid flowcharts with ease. This lightweight and powerful library bridges the gap between JSON Canvas and Mermaid, enabling you to visualize your canvas data in a whole new way.
[!TIP] Give it a try! Convert
.canvas
files to Mermaid syntax right in your browser. https://alexwiench.github.io/json-canvas-to-mermaid-demo/
npm install json-canvas-to-mermaid
JSON Canvas to Mermaid works in three main steps:
Here's a quick example of how to use the library:
import convertToMermaid from 'json-canvas-to-mermaid';
const jsonCanvasData = {
nodes: [
{ id: 'node1', type: 'text', text: 'Hello', x: 0, y: 0, width: 100, height: 50 },
{ id: 'node2', type: 'text', text: 'World', x: 200, y: 0, width: 100, height: 50 },
],
edges: [{ id: 'edge1', fromNode: 'node1', toNode: 'node2' }],
};
const mermaidSyntax = convertToMermaid(jsonCanvasData);
console.log(mermaidSyntax);
convertToMermaid(data, customColors, graphDirection)
Converts JSON Canvas data to Mermaid flowchart syntax.
data
: The JSON Canvas data object containing nodes and edges.customColors
(optional): An object mapping color identifiers to hex color codes. Maximum of 6 colors.graphDirection
(optional): The direction of the graph. Valid options are 'TB', 'LR', 'BT', 'RL'. Default is 'TB'.Returns a string containing the Mermaid flowchart syntax.
The library also exports some additional utilities that were developed as part of this project. While not necessary for the primary use case, they're available if you need them for other projects:
createNodeTree(data)
: Creates a hierarchical structure from flat JSON Canvas data.validateJsonCanvasData(data)
: Validates the structure and content of JSON Canvas data.validateCustomColors(customColors)
: Validates the custom colors object.validateGraphDirection(graphDirection)
: Validates the graph direction parameter.These functions are not required for normal use of the library but are exposed for developers who might find them useful in other contexts.
import convertToMermaid from 'json-canvas-to-mermaid';
const data = {
nodes: [
{ id: 'node1', type: 'text', text: 'Node 1', x: 0, y: 0, width: 100, height: 50 },
{ id: 'node2', type: 'text', text: 'Node 2', x: 200, y: 0, width: 100, height: 50 },
],
edges: [{ id: 'edge1', fromNode: 'node1', toNode: 'node2' }],
};
const mermaidSyntax = convertToMermaid(data);
console.log(mermaidSyntax);
const customColors = {
1: '#ff0000',
2: '#00ff00',
3: '#0000ff',
};
const customDirection = 'LR';
const data = {
nodes: [
{ id: 'node1', type: 'text', text: 'Red Node', x: 0, y: 0, width: 100, height: 50, color: '1' },
{
id: 'node2',
type: 'text',
text: 'Green Node',
x: 200,
y: 0,
width: 100,
height: 50,
color: '2',
},
],
edges: [{ id: 'edge1', fromNode: 'node1', toNode: 'node2', color: '3' }],
};
const mermaidSyntax = convertToMermaid(data, customColors, customDirection);
While not necessary for the primary use case, the validation and createNodeTree utilities can be useful in certain scenarios. Here's how you can use them:
You can use the validation functions to check your JSON Canvas data or other parameters:
import { jsonCanvas } from 'json-canvas-to-mermaid';
const data = {
nodes: [
{ id: 'node1', type: 'text', text: 'Hello', x: 0, y: 0, width: 100, height: 50 },
{ id: 'node2', type: 'text', text: 'World', x: 200, y: 0, width: 100, height: 50 },
],
edges: [{ id: 'edge1', fromNode: 'node1', toNode: 'node2' }],
};
try {
jsonCanvas.validate.data(data);
console.log('JSON Canvas data is valid');
} catch (error) {
console.error('Invalid JSON Canvas data:', error.message);
}
// Validate custom colors
const customColors = { 1: '#ff0000', 2: '#00ff00' };
try {
jsonCanvas.validate.colors(customColors);
console.log('Custom colors are valid');
} catch (error) {
console.error('Invalid custom colors:', error.message);
}
// Validate graph direction
const direction = 'LR';
try {
jsonCanvas.validate.direction(direction);
console.log('Graph direction is valid');
} catch (error) {
console.error('Invalid graph direction:', error.message);
}
The createNodeTree function can be used to generate a hierarchical structure from flat JSON Canvas data:
import { jsonCanvas } from 'json-canvas-to-mermaid';
const data = {
nodes: [
{ id: 'group1', type: 'group', x: 0, y: 0, width: 300, height: 200 },
{ id: 'node1', type: 'text', text: 'Inside Group', x: 50, y: 50, width: 100, height: 50 },
{ id: 'node2', type: 'text', text: 'Outside Group', x: 400, y: 50, width: 100, height: 50 },
],
edges: [],
};
const hierarchicalData = jsonCanvas.createNodeTree(data);
console.log(JSON.stringify(hierarchicalData, null, 2));
// Output:
// {
// "nodes": [
// {
// "id": "group1",
// "type": "group",
// "x": 0,
// "y": 0,
// "width": 300,
// "height": 200,
// "children": ["node1"]
// },
// {
// "id": "node1",
// "type": "text",
// "text": "Inside Group",
// "x": 50,
// "y": 50,
// "width": 100,
// "height": 50,
// "children": null
// },
// {
// "id": "node2",
// "type": "text",
// "text": "Outside Group",
// "x": 400,
// "y": 50,
// "width": 100,
// "height": 50,
// "children": null
// }
// ],
// "edges": []
// }
This hierarchical structure can be useful if you need to process the JSON Canvas data in a tree-like format for other purposes in your application.
Learn more about those projects here:
This project is licensed under the MIT License - see the LICENSE file for details.
FAQs
A module to generate Mermaid Graph syntax from JSON Canvas data
The npm package json-canvas-to-mermaid receives a total of 9 weekly downloads. As such, json-canvas-to-mermaid popularity was classified as not popular.
We found that json-canvas-to-mermaid demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.