json to mermaid markdown -FlowChart Mermaid html populate
by bilbobaggins
HTML
<script src="https://cdn.jsdelivr.net/gh/knsv/[email protected]/dist/mermaid.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/knsv/[email protected]/dist/mermaid.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<div class="mermaid">
</div>
JavaScript
/**
* Validates the structure and content of JSON Canvas data.
* @param {Object} data - The JSON Canvas data to validate.
* @throws {Error} If the data or structure is invalid.
*/
function validateJsonCanvasData(data) {
if (typeof data !== 'object' || data === null) {
throw new Error('Invalid data: must be a non-null object');
}
if (!Array.isArray(data.nodes)) {
throw new Error('Invalid data: nodes must be an array');
}
if (!Array.isArray(data.edges)) {
throw new Error('Invalid data: edges must be an array');
}
// Validate nodes
const nodeIds = new Set();
data.nodes.forEach((node, index) => {
if (typeof node !== 'object' || node === null) {
throw new Error(`Invalid node at index ${index}: must be a non-null object`);
}
if (typeof node.id !== 'string' || node.id.trim() === '') {
throw new Error(`Invalid node at index ${index}: id must be a non-empty string`);
}
if (nodeIds.has(node.id)) {
throw new Error(`Duplicate node id: ${node.id}`);
}
nodeIds.add(node.id);
if (!['text', 'file', 'link', 'group'].includes(node.type)) {
throw new Error(`Invalid node type at index ${index}: ${node.type}`);
}
if (
typeof node.x !== 'number' ||
typeof node.y !== 'number' ||
typeof node.width !== 'number' ||
typeof node.height !== 'number'
) {
throw new Error(`Invalid node dimensions at index ${index}`);
}
if (node.color && typeof node.color !== 'string') {
throw new Error(`Invalid node color at index ${index}: must be a string`);
}
// Type-specific validations
switch (node.type) {
case 'text':
if (typeof node.text !== 'string') {
throw new Error(`Invalid text node at index ${index}: text must be a string`);
}
break;
case 'file':
if (typeof node.file !== 'string' || node.file.trim() === '') {
throw new Error(`Invalid file node at index ${index}: file must be a non-empty string`);
}
if (node.subpath && typeof node.subpath !== 'string') {
throw...