npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-canvas-to-mermaid

v1.0.2

Published

A module to generate Mermaid Graph syntax from JSON Canvas data

Downloads

26

Readme

JSON Canvas to Mermaid

Quicky convert JSON Canvas to a Mermaid diagram!

This package exports two functions. The generateMermaidFlowchart() function converts your canvas into Mermaid syntax. The buildJsonCanvasHierarchy() function extends the JSON Canvas data with a children property and adds the IDs of a group's children to it. This is useful for building tools utilizing JSON Canvas, as an explicit hierarchy is not included as part of the JSON Canvas spec.

Install with npm i json-canvas-to-mermaid

Live demo site: https://alexwiench.github.io/json-canvas-to-mermaid-demo/

Learn More:

Avalible Functions

generateMermaidFlowchart(data, customColors, graphDirection)

The generateMermaidFlowchart function accepts the following parameters:

  • data (required): An object that contains the JSON Canvas format.

  • customColors (optional): An object that allows you to specify custom color mappings for nodes and edges. The keys of the object represent the color identifiers, and the values are the corresponding color codes. If not provided, the function will use the default color map.

  • graphDirection (optional): A string that specifies the direction of the graph. It determines the orientation of the flowchart. The valid options for graphDirection are:

    • 'TB' (default): Top to bottom
    • 'LR': Left to right
    • 'BT': Bottom to top
    • 'RL': Right to left

Return Value

The generateMermaidFlowchart function returns the generated Mermaid Flowchart syntax as a string. This syntax can be used to render the flowchart using Mermaid.

Error Handling

If an invalid graphDirection is provided, the function will throw an error.

(buildJsonCanvasHierarchy(data)

  • data (required): An object that contains the JSON Canvas format.

Return Value

The buildJsonCanvasHierarchy function returns the generated the inputed data with an additional children property on nodes.

Usage

import generateMermaidFlowchart, { buildJsonCanvasHierarchy } from './index.js';

let data = {
	nodes: [
		{
			id: '6f002d2b0257ffa4',
			x: -1601,
			y: -826,
			width: 631,
			height: 100,
			color: '#248a42',
			type: 'group',
			label: 'Group One',
		},
		{
			id: '5696e6f4d7feef3b',
			x: -1581,
			y: -806,
			width: 250,
			height: 60,
			color: '4',
			type: 'text',
			text: 'Node One',
		},
		{
			id: 'eb886f14ff2b15a1',
			x: -1240,
			y: -806,
			width: 250,
			height: 60,
			color: '6',
			type: 'text',
			text: 'Node Two',
		},
	],
	edges: [
		{
			id: 'ed452a5525485f24',
			fromNode: '5696e6f4d7feef3b',
			fromSide: 'right',
			toNode: 'eb886f14ff2b15a1',
			toSide: 'left',
			color: '2',
		},
	],
};

// Output json data with `children` property added
console.log(buildJsonCanvasHierarchy(data));

// OPTIONAL - Overwrite any or all of the 6 default colors
const customColors = {
	2: '#ff0000',
	4: '#00ff00',
};

// OPTIONAL - Change Mermaid graph direction
const graphDirection = 'LR';

// Output mermaid flowchart
console.log(generateMermaidFlowchart(data, customColors, graphDirection));

Mermaid output:

graph LR
subgraph 6f002d2b0257ffa4["Group One"]
5696e6f4d7feef3b["Node One"]
eb886f14ff2b15a1["Node Two"]

end
5696e6f4d7feef3b["Node One"]
eb886f14ff2b15a1["Node Two"]
5696e6f4d7feef3b -->  eb886f14ff2b15a1
style 6f002d2b0257ffa4 fill:#248a42, stroke:#00570f
style 5696e6f4d7feef3b fill:#00ff00, stroke:#00cc00
style eb886f14ff2b15a1 fill:#a882ff, stroke:#754fcc
style 5696e6f4d7feef3b fill:#00ff00, stroke:#00cc00
style eb886f14ff2b15a1 fill:#a882ff, stroke:#754fcc
linkStyle 0 stroke:#ff0000

For working sample, download this repository and run ExampleUsage.js

Roadmap

  • Add validation for data and color paramaters
  • Update as the JSON Canvas spec evolves.