diff --git a/.gitignore b/.gitignore index 009c6dfac..6a1cc85e5 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,6 @@ stats/ **/contributor-names.json .pnpm-store .nyc_output + +demos/dev/** +!/demos/dev/example.html diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 307bd038b..02733ee2e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -26,9 +26,14 @@ Install required packages: ```bash # npx is required for first install as volta support for pnpm is not added yet. npx pnpm install -pnpm test +pnpm test # run unit tests +pnpm dev # starts a dev server ``` +Open in your browser after starting the dev server. +You can also duplicate the `example.html` file in `demos/dev`, rename it and add your own mermaid code to it. +That will be served at . + ### Docker If you are using docker and docker-compose, you have self-documented `run` bash script, which is a convenient alias for docker-compose commands: diff --git a/cypress/integration/rendering/flowchart-v2.spec.js b/cypress/integration/rendering/flowchart-v2.spec.js index 0f4b079c9..aac4a31b1 100644 --- a/cypress/integration/rendering/flowchart-v2.spec.js +++ b/cypress/integration/rendering/flowchart-v2.spec.js @@ -449,7 +449,7 @@ flowchart TD { htmlLabels: true, flowchart: { htmlLabels: true }, securityLevel: 'loose' } ); }); - it('65: text-color from classes', () => { + it('65-1: text-color from classes', () => { imgSnapshotTest( ` flowchart LR @@ -460,6 +460,31 @@ flowchart TD { htmlLabels: true, flowchart: { htmlLabels: true }, securityLevel: 'loose' } ); }); + it('65-2: bold text from classes', () => { + imgSnapshotTest( + ` + flowchart + classDef cat fill:#f9d5e5, stroke:#233d4d,stroke-width:2px, font-weight:bold; + CS(A long bold text to be viewed):::cat + `, + { htmlLabels: true, flowchart: { htmlLabels: true }, securityLevel: 'loose' } + ); + }); + it('65-3: bigger font from classes', () => { + imgSnapshotTest( + ` +flowchart + Node1:::class1 --> Node2:::class2 + Node1:::class1 --> Node3:::class2 + Node3 --> Node4((I am a circle)):::larger + + classDef class1 fill:lightblue + classDef class2 fill:pink + classDef larger font-size:30px,fill:yellow + `, + { htmlLabels: true, flowchart: { htmlLabels: true }, securityLevel: 'loose' } + ); + }); it('66: More nested subgraph cases (TB)', () => { imgSnapshotTest( ` diff --git a/cypress/platform/knsv2.html b/cypress/platform/knsv2.html index 13de19ba0..f9a9f3756 100644 --- a/cypress/platform/knsv2.html +++ b/cypress/platform/knsv2.html @@ -58,12 +58,21 @@
----
-title: Simple flowchart with invisible edges
----
-flowchart TD
-A ~~~ B
-  
+
+flowchart
+Node1:::class1 --> Node2:::class2
+Node1:::class1 --> Node3:::class2
+Node3 --> Node4((I am a circle)):::larger
+
+classDef class1 fill:lightblue
+classDef class2 fill:pink
+classDef larger font-size:30px,fill:yellow
+      
 stateDiagram-v2
diff --git a/demos/dev/example.html b/demos/dev/example.html
new file mode 100644
index 000000000..482343014
--- /dev/null
+++ b/demos/dev/example.html
@@ -0,0 +1,34 @@
+
+
+
+  
+    Mermaid development page
+  
+  
+    
+graph TB
+      a --> b
+      a --> c
+      b --> d
+      c --> d
+    
+ +
+ + + + diff --git a/docs/community/development.md b/docs/community/development.md index 0634759f5..f8c8b0638 100644 --- a/docs/community/development.md +++ b/docs/community/development.md @@ -70,7 +70,21 @@ pnpm test The `test` script and others are in the top-level `package.json` file. -All tests should run successfully without any errors or failures. (You might see _lint_ or _formatting_ warnings; those are ok during this step.) +All tests should run successfully without any errors or failures. (You might see _lint_ or _formatting_ "warnings"; those are ok during this step.) + +#### 4. Make your changes + +Now you are ready to make your changes! +Edit whichever files in `src` as required. + +#### 5. See your changes + +Open in your browser, after starting the dev server. +There is a list of demos that can be used to see and test your changes. + +If you need a specific diagram, you can duplicate the `example.html` file in `/demos/dev` and add your own mermaid code to your copy. +That will be served at . +After making code changes, the dev server will rebuild the mermaid library. You will need to reload the browser page yourself to see the changes. (PRs for auto reload are welcome!) ### Docker diff --git a/docs/community/newDiagram.md b/docs/community/newDiagram.md index ba52999f4..5dd616e66 100644 --- a/docs/community/newDiagram.md +++ b/docs/community/newDiagram.md @@ -10,7 +10,7 @@ #### Grammar -This would be to define a jison grammar for the new diagram type. That should start with a way to identify that the text in the mermaid tag is a diagram of that type. Create a new folder under diagrams for your new diagram type and a parser folder in it. This leads us to step 2. +This would be to define a JISON grammar for the new diagram type. That should start with a way to identify that the text in the mermaid tag is a diagram of that type. Create a new folder under diagrams for your new diagram type and a parser folder in it. This leads us to step 2. For instance: @@ -60,7 +60,7 @@ Place the renderer in the diagram folder. ### Step 3: Detection of the new diagram type -The second thing to do is to add the capability to detect the new diagram to type to the detectType in utils.js. The detection should return a key for the new diagram type. +The second thing to do is to add the capability to detect the new diagram to type to the detectType in `diagram-api/detectType.ts`. The detection should return a key for the new diagram type. [This key will be used to as the aria roledescription](#aria-roledescription), so it should be a word that clearly describes the diagram type. For example, if your new diagram use a UML deployment diagram, a good key would be "UMLDeploymentDiagram" because assistive technologies such as a screen reader would voice that as "U-M-L Deployment diagram." Another good key would be "deploymentDiagram" because that would be voiced as "Deployment Diagram." A bad key would be "deployment" because that would not sufficiently describe the diagram. diff --git a/docs/config/setup/modules/mermaidAPI.md b/docs/config/setup/modules/mermaidAPI.md index 330c136dc..1160a5dda 100644 --- a/docs/config/setup/modules/mermaidAPI.md +++ b/docs/config/setup/modules/mermaidAPI.md @@ -96,7 +96,7 @@ mermaid.initialize(config); #### Defined in -[mermaidAPI.ts:669](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L669) +[mermaidAPI.ts:668](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L668) ## Functions @@ -127,7 +127,7 @@ Return the last node appended #### Defined in -[mermaidAPI.ts:310](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L310) +[mermaidAPI.ts:309](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L309) --- @@ -153,7 +153,7 @@ the cleaned up svgCode #### Defined in -[mermaidAPI.ts:256](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L256) +[mermaidAPI.ts:255](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L255) --- @@ -179,7 +179,7 @@ the string with all the user styles #### Defined in -[mermaidAPI.ts:185](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L185) +[mermaidAPI.ts:184](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L184) --- @@ -202,7 +202,7 @@ the string with all the user styles #### Defined in -[mermaidAPI.ts:233](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L233) +[mermaidAPI.ts:232](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L232) --- @@ -229,7 +229,7 @@ with an enclosing block that has each of the cssClasses followed by !important; #### Defined in -[mermaidAPI.ts:169](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L169) +[mermaidAPI.ts:168](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L168) --- @@ -249,7 +249,7 @@ with an enclosing block that has each of the cssClasses followed by !important; #### Defined in -[mermaidAPI.ts:155](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L155) +[mermaidAPI.ts:154](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L154) --- @@ -269,7 +269,7 @@ with an enclosing block that has each of the cssClasses followed by !important; #### Defined in -[mermaidAPI.ts:126](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L126) +[mermaidAPI.ts:125](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L125) --- @@ -295,7 +295,7 @@ Put the svgCode into an iFrame. Return the iFrame code #### Defined in -[mermaidAPI.ts:287](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L287) +[mermaidAPI.ts:286](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L286) --- @@ -320,4 +320,4 @@ Remove any existing elements from the given document #### Defined in -[mermaidAPI.ts:360](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L360) +[mermaidAPI.ts:359](https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/mermaidAPI.ts#L359) diff --git a/packages/mermaid/package.json b/packages/mermaid/package.json index 15379d68a..cf9e5af94 100644 --- a/packages/mermaid/package.json +++ b/packages/mermaid/package.json @@ -24,6 +24,7 @@ ], "scripts": { "clean": "rimraf dist", + "dev": "pnpm -w dev", "docs:code": "typedoc src/defaultConfig.ts src/config.ts src/mermaidAPI.ts && prettier --write ./src/docs/config/setup", "docs:build": "rimraf ../../docs && pnpm docs:spellcheck && pnpm docs:code && ts-node-esm scripts/docs.cli.mts", "docs:verify": "pnpm docs:spellcheck && pnpm docs:code && ts-node-esm scripts/docs.cli.mts --verify", diff --git a/packages/mermaid/src/Diagram.ts b/packages/mermaid/src/Diagram.ts index 4fb329b28..13fd3232b 100644 --- a/packages/mermaid/src/Diagram.ts +++ b/packages/mermaid/src/Diagram.ts @@ -4,8 +4,9 @@ import { getDiagram, registerDiagram } from './diagram-api/diagramAPI.js'; import { detectType, getDiagramLoader } from './diagram-api/detectType.js'; import { extractFrontMatter } from './diagram-api/frontmatter.js'; import { UnknownDiagramError } from './errors.js'; -import { DetailedError } from './utils.js'; import { cleanupComments } from './diagram-api/comments.js'; +import type { DetailedError } from './utils.js'; +import type { DiagramDefinition } from './diagram-api/types.js'; export type ParseErrorFunction = (err: string | DetailedError | unknown, hash?: any) => void; @@ -15,9 +16,11 @@ export type ParseErrorFunction = (err: string | DetailedError | unknown, hash?: */ export class Diagram { type = 'graph'; - parser; - renderer; - db; + parser: DiagramDefinition['parser']; + renderer: DiagramDefinition['renderer']; + db: DiagramDefinition['db']; + private init?: DiagramDefinition['init']; + private detectError?: UnknownDiagramError; constructor(public text: string) { this.text += '\n'; @@ -32,7 +35,6 @@ export class Diagram { log.debug('Type ' + this.type); // Setup diagram this.db = diagram.db; - this.db.clear?.(); this.renderer = diagram.renderer; this.parser = diagram.parser; const originalParse = this.parser.parse.bind(this.parser); @@ -49,10 +51,7 @@ export class Diagram { originalParse(cleanupComments(extractFrontMatter(text, this.db))); this.parser.parser.yy = this.db; - if (diagram.init) { - diagram.init(cnf); - log.info('Initialized diagram ' + this.type, cnf); - } + this.init = diagram.init; this.parse(); } @@ -61,6 +60,7 @@ export class Diagram { throw this.detectError; } this.db.clear?.(); + this.init?.(configApi.getConfig()); this.parser.parse(this.text); } diff --git a/packages/mermaid/src/assignWithDepth.js b/packages/mermaid/src/assignWithDepth.ts similarity index 65% rename from packages/mermaid/src/assignWithDepth.js rename to packages/mermaid/src/assignWithDepth.ts index 6f2e706ab..831825779 100644 --- a/packages/mermaid/src/assignWithDepth.js +++ b/packages/mermaid/src/assignWithDepth.ts @@ -1,32 +1,36 @@ -'use strict'; +/* eslint-disable @typescript-eslint/no-explicit-any */ + /** - * @function assignWithDepth Extends the functionality of {@link ObjectConstructor.assign} with the + * assignWithDepth Extends the functionality of {@link ObjectConstructor.assign} with the * ability to merge arbitrary-depth objects For each key in src with path `k` (recursively) * performs an Object.assign(dst[`k`], src[`k`]) with a slight change from the typical handling of - * undefined for dst[`k`]: instead of raising an error, dst[`k`] is auto-initialized to {} and + * undefined for dst[`k`]: instead of raising an error, dst[`k`] is auto-initialized to `{}` and * effectively merged with src[`k`]

Additionally, dissimilar types will not clobber unless the * config.clobber parameter === true. Example: * - * ```js - * let config_0 = { foo: { bar: 'bar' }, bar: 'foo' }; - * let config_1 = { foo: 'foo', bar: 'bar' }; - * let result = assignWithDepth(config_0, config_1); - * console.log(result); - * //-> result: { foo: { bar: 'bar' }, bar: 'bar' } - * ``` + * ``` + * const config_0 = { foo: { bar: 'bar' }, bar: 'foo' }; + * const config_1 = { foo: 'foo', bar: 'bar' }; + * const result = assignWithDepth(config_0, config_1); + * console.log(result); + * //-> result: { foo: { bar: 'bar' }, bar: 'bar' } + * ``` * * Traditional Object.assign would have clobbered foo in config_0 with foo in config_1. If src is a * destructured array of objects and dst is not an array, assignWithDepth will apply each element * of src to dst in order. - * @param {any} dst - The destination of the merge - * @param {any} src - The source object(s) to merge into destination - * @param {{ depth: number; clobber: boolean }} [config] - Depth: depth - * to traverse within src and dst for merging - clobber: should dissimilar types clobber (default: - * { depth: 2, clobber: false }). Default is `{ depth: 2, clobber: false }` - * @returns {any} + * @param dst - The destination of the merge + * @param src - The source object(s) to merge into destination + * @param config - + * * depth: depth to traverse within src and dst for merging + * * clobber: should dissimilar types clobber */ -const assignWithDepth = function (dst, src, config) { - const { depth, clobber } = Object.assign({ depth: 2, clobber: false }, config); +const assignWithDepth = ( + dst: any, + src: any, + { depth = 2, clobber = false }: { depth?: number; clobber?: boolean } = {} +): any => { + const config: { depth: number; clobber: boolean } = { depth, clobber }; if (Array.isArray(src) && !Array.isArray(dst)) { src.forEach((s) => assignWithDepth(dst, s, config)); return dst; diff --git a/packages/mermaid/src/dagre-wrapper/nodes.js b/packages/mermaid/src/dagre-wrapper/nodes.js index 6c6733358..e7988c8b5 100644 --- a/packages/mermaid/src/dagre-wrapper/nodes.js +++ b/packages/mermaid/src/dagre-wrapper/nodes.js @@ -8,8 +8,25 @@ import note from './shapes/note.js'; import { parseMember } from '../diagrams/class/svgDraw.js'; import { evaluate } from '../diagrams/common/common.js'; +const formatClass = (str) => { + if (str) { + return ' ' + str; + } + return ''; +}; +const getClassesFromNode = (node, otherClasses) => { + return `${otherClasses ? otherClasses : 'node default'}${formatClass(node.classes)} ${formatClass( + node.class + )}`; +}; + const question = async (parent, node) => { - const { shapeSvg, bbox } = await labelHelper(parent, node, undefined, true); + const { shapeSvg, bbox } = await labelHelper( + parent, + node, + getClassesFromNode(node, undefined), + true + ); const w = bbox.width + node.padding; const h = bbox.height + node.padding; @@ -70,7 +87,12 @@ const choice = (parent, node) => { }; const hexagon = async (parent, node) => { - const { shapeSvg, bbox } = await labelHelper(parent, node, undefined, true); + const { shapeSvg, bbox } = await labelHelper( + parent, + node, + getClassesFromNode(node, undefined), + true + ); const f = 4; const h = bbox.height + node.padding; @@ -97,7 +119,12 @@ const hexagon = async (parent, node) => { }; const rect_left_inv_arrow = async (parent, node) => { - const { shapeSvg, bbox } = await labelHelper(parent, node, undefined, true); + const { shapeSvg, bbox } = await labelHelper( + parent, + node, + getClassesFromNode(node, undefined), + true + ); const w = bbox.width + node.padding; const h = bbox.height + node.padding; @@ -123,7 +150,7 @@ const rect_left_inv_arrow = async (parent, node) => { }; const lean_right = async (parent, node) => { - const { shapeSvg, bbox } = await labelHelper(parent, node, undefined, true); + const { shapeSvg, bbox } = await labelHelper(parent, node, getClassesFromNode(node), true); const w = bbox.width + node.padding; const h = bbox.height + node.padding; @@ -146,7 +173,12 @@ const lean_right = async (parent, node) => { }; const lean_left = async (parent, node) => { - const { shapeSvg, bbox } = await labelHelper(parent, node, undefined, true); + const { shapeSvg, bbox } = await labelHelper( + parent, + node, + getClassesFromNode(node, undefined), + true + ); const w = bbox.width + node.padding; const h = bbox.height + node.padding; @@ -169,7 +201,12 @@ const lean_left = async (parent, node) => { }; const trapezoid = async (parent, node) => { - const { shapeSvg, bbox } = await labelHelper(parent, node, undefined, true); + const { shapeSvg, bbox } = await labelHelper( + parent, + node, + getClassesFromNode(node, undefined), + true + ); const w = bbox.width + node.padding; const h = bbox.height + node.padding; @@ -192,7 +229,12 @@ const trapezoid = async (parent, node) => { }; const inv_trapezoid = async (parent, node) => { - const { shapeSvg, bbox } = await labelHelper(parent, node, undefined, true); + const { shapeSvg, bbox } = await labelHelper( + parent, + node, + getClassesFromNode(node, undefined), + true + ); const w = bbox.width + node.padding; const h = bbox.height + node.padding; @@ -215,7 +257,12 @@ const inv_trapezoid = async (parent, node) => { }; const rect_right_inv_arrow = async (parent, node) => { - const { shapeSvg, bbox } = await labelHelper(parent, node, undefined, true); + const { shapeSvg, bbox } = await labelHelper( + parent, + node, + getClassesFromNode(node, undefined), + true + ); const w = bbox.width + node.padding; const h = bbox.height + node.padding; @@ -239,7 +286,12 @@ const rect_right_inv_arrow = async (parent, node) => { }; const cylinder = async (parent, node) => { - const { shapeSvg, bbox } = await labelHelper(parent, node, undefined, true); + const { shapeSvg, bbox } = await labelHelper( + parent, + node, + getClassesFromNode(node, undefined), + true + ); const w = bbox.width + node.padding; const rx = w / 2; @@ -314,7 +366,7 @@ const rect = async (parent, node) => { const { shapeSvg, bbox, halfPadding } = await labelHelper( parent, node, - 'node ' + node.classes, + 'node ' + node.classes + ' ' + node.class, true ); @@ -360,7 +412,7 @@ const rect = async (parent, node) => { const labelRect = async (parent, node) => { const { shapeSvg } = await labelHelper(parent, node, 'label', true); - log.trace('Classes = ', node.classes); + log.trace('Classes = ', node.class); // add the rect const rect = shapeSvg.insert('rect', ':first-child'); @@ -545,7 +597,12 @@ const rectWithTitle = (parent, node) => { }; const stadium = async (parent, node) => { - const { shapeSvg, bbox } = await labelHelper(parent, node, undefined, true); + const { shapeSvg, bbox } = await labelHelper( + parent, + node, + getClassesFromNode(node, undefined), + true + ); const h = bbox.height + node.padding; const w = bbox.width + h / 4 + node.padding; @@ -571,7 +628,12 @@ const stadium = async (parent, node) => { }; const circle = async (parent, node) => { - const { shapeSvg, bbox, halfPadding } = await labelHelper(parent, node, undefined, true); + const { shapeSvg, bbox, halfPadding } = await labelHelper( + parent, + node, + getClassesFromNode(node, undefined), + true + ); const circle = shapeSvg.insert('circle', ':first-child'); // center the circle around its coordinate @@ -596,7 +658,12 @@ const circle = async (parent, node) => { }; const doublecircle = async (parent, node) => { - const { shapeSvg, bbox, halfPadding } = await labelHelper(parent, node, undefined, true); + const { shapeSvg, bbox, halfPadding } = await labelHelper( + parent, + node, + getClassesFromNode(node, undefined), + true + ); const gap = 5; const circleGroup = shapeSvg.insert('g', ':first-child'); const outerCircle = circleGroup.insert('circle'); @@ -634,7 +701,12 @@ const doublecircle = async (parent, node) => { }; const subroutine = async (parent, node) => { - const { shapeSvg, bbox } = await labelHelper(parent, node, undefined, true); + const { shapeSvg, bbox } = await labelHelper( + parent, + node, + getClassesFromNode(node, undefined), + true + ); const w = bbox.width + node.padding; const h = bbox.height + node.padding; diff --git a/packages/mermaid/src/dagre-wrapper/shapes/util.js b/packages/mermaid/src/dagre-wrapper/shapes/util.js index 3eaedb4b9..95b82ddc0 100644 --- a/packages/mermaid/src/dagre-wrapper/shapes/util.js +++ b/packages/mermaid/src/dagre-wrapper/shapes/util.js @@ -13,6 +13,7 @@ export const labelHelper = async (parent, node, _classes, isNode) => { } else { classes = _classes; } + // Add outer g element const shapeSvg = parent .insert('g') @@ -49,7 +50,6 @@ export const labelHelper = async (parent, node, _classes, isNode) => { ) ); } - // Get the size of the label let bbox = text.getBBox(); const halfPadding = node.padding / 2; diff --git a/packages/mermaid/src/diagram-api/diagramAPI.ts b/packages/mermaid/src/diagram-api/diagramAPI.ts index 7e89d9cd7..3edd982bb 100644 --- a/packages/mermaid/src/diagram-api/diagramAPI.ts +++ b/packages/mermaid/src/diagram-api/diagramAPI.ts @@ -69,11 +69,11 @@ export const getDiagram = (name: string): DiagramDefinition => { if (name in diagrams) { return diagrams[name]; } - throw new Error(`Diagram ${name} not found.`); + throw new DiagramNotFoundError(name); }; export class DiagramNotFoundError extends Error { - constructor(message: string) { - super(`Diagram ${message} not found.`); + constructor(name: string) { + super(`Diagram ${name} not found.`); } } diff --git a/packages/mermaid/src/diagrams/er/erRenderer.js b/packages/mermaid/src/diagrams/er/erRenderer.js index 63fb05633..b992afd65 100644 --- a/packages/mermaid/src/diagrams/er/erRenderer.js +++ b/packages/mermaid/src/diagrams/er/erRenderer.js @@ -555,7 +555,6 @@ const drawRelationshipFromLayout = function (svg, rel, g, insert, diagObj) { export const draw = function (text, id, _version, diagObj) { conf = getConfig().er; log.info('Drawing ER diagram'); - // diag.db.clear(); const securityLevel = getConfig().securityLevel; // Handle root and Document for when rendering in sandbox mode let sandboxElement; diff --git a/packages/mermaid/src/diagrams/error/errorRenderer.ts b/packages/mermaid/src/diagrams/error/errorRenderer.ts index ebe0d0d73..a8e738e5f 100644 --- a/packages/mermaid/src/diagrams/error/errorRenderer.ts +++ b/packages/mermaid/src/diagrams/error/errorRenderer.ts @@ -14,8 +14,8 @@ export const draw = (_text: string, id: string, version: string) => { log.debug('renering svg for syntax error\n'); const svg: SVG = selectSvgElement(id); - svg.attr('viewBox', '768 0 912 512'); - configureSvgSize(svg, 100, 500, true); + svg.attr('viewBox', '0 0 2412 512'); + configureSvgSize(svg, 100, 512, true); const g: Group = svg.append('g'); g.append('path') diff --git a/packages/mermaid/src/diagrams/flowchart/elk/flowRenderer-elk.js b/packages/mermaid/src/diagrams/flowchart/elk/flowRenderer-elk.js index 5ed06723e..c7bfdf524 100644 --- a/packages/mermaid/src/diagrams/flowchart/elk/flowRenderer-elk.js +++ b/packages/mermaid/src/diagrams/flowchart/elk/flowRenderer-elk.js @@ -655,14 +655,7 @@ const addMarkersToEdge = function (svgPath, edgeData, diagramType, arrowMarkerAb */ export const getClasses = function (text, diagObj) { log.info('Extracting classes'); - diagObj.db.clear('ver-2'); - try { - // Parse the graph definition - diagObj.parse(text); - return diagObj.db.getClasses(); - } catch (e) { - return {}; - } + return diagObj.db.getClasses(); }; const addSubGraphs = function (db) { @@ -766,14 +759,8 @@ const insertChildren = (nodeArray, parentLookupDb) => { */ export const draw = async function (text, id, _version, diagObj) { - // Add temporary render element - diagObj.db.clear(); nodeDb = {}; portPos = {}; - diagObj.db.setGen('gen-2'); - // Parse the graph definition - diagObj.parser.parse(text); - const renderEl = select('body').append('div').attr('style', 'height:400px').attr('id', 'cy'); let graph = { id: 'root', diff --git a/packages/mermaid/src/diagrams/flowchart/flowRenderer-v2.js b/packages/mermaid/src/diagrams/flowchart/flowRenderer-v2.js index 23f94942c..4a3b7a8ce 100644 --- a/packages/mermaid/src/diagrams/flowchart/flowRenderer-v2.js +++ b/packages/mermaid/src/diagrams/flowchart/flowRenderer-v2.js @@ -1,10 +1,7 @@ import * as graphlib from 'dagre-d3-es/src/graphlib/index.js'; import { select, curveLinear, selectAll } from 'd3'; - -import flowDb from './flowDb.js'; import { getConfig } from '../../config.js'; import utils from '../../utils.js'; - import { render } from '../../dagre-wrapper/index.js'; import { addHtmlLabel } from 'dagre-d3-es/src/dagre-js/label/add-html-label.js'; import { log } from '../../logger.js'; @@ -344,15 +341,7 @@ export const addEdges = function (edges, g, diagObj) { * @returns {object} ClassDef styles */ export const getClasses = function (text, diagObj) { - log.info('Extracting classes'); - diagObj.db.clear(); - try { - // Parse the graph definition - diagObj.parse(text); - return diagObj.db.getClasses(); - } catch (e) { - return; - } + return diagObj.db.getClasses(); }; /** @@ -364,10 +353,6 @@ export const getClasses = function (text, diagObj) { export const draw = async function (text, id, _version, diagObj) { log.info('Drawing flowchart'); - diagObj.db.clear(); - flowDb.setGen('gen-2'); - // Parse the graph definition - diagObj.parser.parse(text); // Fetch the default direction, use TD if none was found let dir = diagObj.db.getDirection(); diff --git a/packages/mermaid/src/diagrams/flowchart/flowRenderer.js b/packages/mermaid/src/diagrams/flowchart/flowRenderer.js index 4382aa9a4..fc06cacd4 100644 --- a/packages/mermaid/src/diagrams/flowchart/flowRenderer.js +++ b/packages/mermaid/src/diagrams/flowchart/flowRenderer.js @@ -273,15 +273,7 @@ export const addEdges = function (edges, g, diagObj) { */ export const getClasses = function (text, diagObj) { log.info('Extracting classes'); - diagObj.db.clear(); - try { - // Parse the graph definition - diagObj.parse(text); - return diagObj.db.getClasses(); - } catch (e) { - log.error(e); - return {}; - } + return diagObj.db.getClasses(); }; /** @@ -294,7 +286,6 @@ export const getClasses = function (text, diagObj) { */ export const draw = function (text, id, _version, diagObj) { log.info('Drawing flowchart'); - diagObj.db.clear(); const { securityLevel, flowchart: conf } = getConfig(); let sandboxElement; if (securityLevel === 'sandbox') { diff --git a/packages/mermaid/src/diagrams/sequence/sequenceDiagram.spec.js b/packages/mermaid/src/diagrams/sequence/sequenceDiagram.spec.js index 8b7d6f8d0..0b84fbe35 100644 --- a/packages/mermaid/src/diagrams/sequence/sequenceDiagram.spec.js +++ b/packages/mermaid/src/diagrams/sequence/sequenceDiagram.spec.js @@ -172,14 +172,11 @@ describe('more than one sequence diagram', () => { describe('when parsing a sequenceDiagram', function () { beforeEach(function () { - // diagram.db = sequenceDb; - // diagram.db.clear(); diagram = new Diagram(` sequenceDiagram Alice->Bob:Hello Bob, how are you? Note right of Bob: Bob thinks Bob-->Alice: I am good thanks!`); - diagram.db.clear(); }); it('should handle a sequenceDiagram definition', async function () { const str = ` @@ -1482,8 +1479,6 @@ describe('when checking the bounds in a sequenceDiagram', function () { let conf; beforeEach(function () { mermaidAPI.reset(); - // diagram.db = sequenceDb; - // diagram.db.clear(); diagram.renderer.bounds.init(); conf = diagram.db.getConfig(); }); @@ -1635,7 +1630,6 @@ sequenceDiagram Alice->Bob:Hello Bob, how are you? Note right of Bob: Bob thinks Bob-->Alice: I am good thanks!`); - diagram.db.clear(); }); ['tspan', 'fo', 'old', undefined].forEach(function (textPlacement) { it(` @@ -2009,8 +2003,6 @@ describe('when rendering a sequenceDiagram with actor mirror activated', () => { let conf; beforeEach(function () { mermaidAPI.reset(); - // diagram.db = sequenceDb; - diagram.db.clear(); conf = diagram.db.getConfig(); diagram.renderer.bounds.init(); }); @@ -2052,12 +2044,8 @@ describe('when rendering a sequenceDiagram with directives', () => { mermaidAPI.initialize({ sequence: conf }); }); - let conf; beforeEach(function () { mermaidAPI.reset(); - // diagram.db = sequenceDb; - diagram.db.clear(); - conf = diagram.db.getConfig(); diagram.renderer.bounds.init(); }); @@ -2069,10 +2057,7 @@ sequenceDiagram participant Alice `; diagram = new Diagram(str); - diagram.renderer.bounds.init(); - await mermaidAPI.parse(str); - diagram.renderer.draw(str, 'tst', '1.2.3', diagram); const { bounds, models } = diagram.renderer.bounds.getBounds(); @@ -2093,7 +2078,7 @@ sequenceDiagram participant Alice `; - diagram.parse(str); + diagram = new Diagram(str); diagram.renderer.draw(str, 'tst', '1.2.3', diagram); const { bounds, models } = diagram.renderer.bounds.getBounds(); @@ -2114,7 +2099,7 @@ Alice->Bob:Hello Bob, how are you? Note right of Bob: Bob thinks Bob-->Alice: I am good thanks!`; - await mermaidAPI.parse(str1); + diagram = new Diagram(str1); diagram.renderer.draw(str1, 'tst', '1.2.3', diagram); // needs to be rendered for the correct value of visibility auto numbers expect(diagram.db.showSequenceNumbers()).toBe(true); @@ -2124,7 +2109,7 @@ Alice->Bob:Hello Bob, how are you? Note right of Bob: Bob thinks Bob-->Alice: I am good thanks!`; - await mermaidAPI.parse(str2); + diagram = new Diagram(str2); diagram.renderer.draw(str2, 'tst', '1.2.3', diagram); expect(diagram.db.showSequenceNumbers()).toBe(false); }); diff --git a/packages/mermaid/src/diagrams/sequence/sequenceRenderer.ts b/packages/mermaid/src/diagrams/sequence/sequenceRenderer.ts index 4f8b1889b..f6fde5001 100644 --- a/packages/mermaid/src/diagrams/sequence/sequenceRenderer.ts +++ b/packages/mermaid/src/diagrams/sequence/sequenceRenderer.ts @@ -749,9 +749,6 @@ function adjustCreatedDestroyedData( export const draw = function (_text: string, id: string, _version: string, diagObj: Diagram) { const { securityLevel, sequence } = configApi.getConfig(); conf = sequence; - diagObj.db.clear(); - // Parse the graph definition - diagObj.parser.parse(_text); // Handle root and Document for when rendering in sandbox mode let sandboxElement; if (securityLevel === 'sandbox') { diff --git a/packages/mermaid/src/diagrams/state/stateRenderer-v2.js b/packages/mermaid/src/diagrams/state/stateRenderer-v2.js index 592cb43cc..1c9b2d1d3 100644 --- a/packages/mermaid/src/diagrams/state/stateRenderer-v2.js +++ b/packages/mermaid/src/diagrams/state/stateRenderer-v2.js @@ -84,17 +84,8 @@ export const setConf = function (cnf) { * @returns {object} ClassDef styles (a Map with keys = strings, values = ) */ export const getClasses = function (text, diagramObj) { - log.trace('Extracting classes'); - diagramObj.db.clear(); - try { - // Parse the graph definition - diagramObj.parser.parse(text); - // must run extract() to turn the parsed statements into states, relationships, classes, etc. - diagramObj.db.extract(diagramObj.db.getRootDocV2()); - return diagramObj.db.getClasses(); - } catch (e) { - return e; - } + diagramObj.db.extract(diagramObj.db.getRootDocV2()); + return diagramObj.db.getClasses(); }; /** @@ -384,7 +375,6 @@ const getDir = (parsedItem, defaultDir = DEFAULT_NESTED_DOC_DIR) => { */ export const draw = async function (text, id, _version, diag) { log.info('Drawing state diagram (v2)', id); - // diag.sb.clear(); nodeDb = {}; // Fetch the default direction, use TD if none was found let dir = diag.db.getDirection(); diff --git a/packages/mermaid/src/docs/community/development.md b/packages/mermaid/src/docs/community/development.md index 93146f0c3..35a01e589 100644 --- a/packages/mermaid/src/docs/community/development.md +++ b/packages/mermaid/src/docs/community/development.md @@ -64,7 +64,21 @@ pnpm test The `test` script and others are in the top-level `package.json` file. -All tests should run successfully without any errors or failures. (You might see _lint_ or _formatting_ warnings; those are ok during this step.) +All tests should run successfully without any errors or failures. (You might see _lint_ or _formatting_ "warnings"; those are ok during this step.) + +#### 4. Make your changes + +Now you are ready to make your changes! +Edit whichever files in `src` as required. + +#### 5. See your changes + +Open in your browser, after starting the dev server. +There is a list of demos that can be used to see and test your changes. + +If you need a specific diagram, you can duplicate the `example.html` file in `/demos/dev` and add your own mermaid code to your copy. +That will be served at . +After making code changes, the dev server will rebuild the mermaid library. You will need to reload the browser page yourself to see the changes. (PRs for auto reload are welcome!) ### Docker diff --git a/packages/mermaid/src/docs/community/newDiagram.md b/packages/mermaid/src/docs/community/newDiagram.md index 3393396ee..7fb47dd30 100644 --- a/packages/mermaid/src/docs/community/newDiagram.md +++ b/packages/mermaid/src/docs/community/newDiagram.md @@ -4,7 +4,7 @@ #### Grammar -This would be to define a jison grammar for the new diagram type. That should start with a way to identify that the text in the mermaid tag is a diagram of that type. Create a new folder under diagrams for your new diagram type and a parser folder in it. This leads us to step 2. +This would be to define a JISON grammar for the new diagram type. That should start with a way to identify that the text in the mermaid tag is a diagram of that type. Create a new folder under diagrams for your new diagram type and a parser folder in it. This leads us to step 2. For instance: @@ -55,7 +55,7 @@ Place the renderer in the diagram folder. ### Step 3: Detection of the new diagram type -The second thing to do is to add the capability to detect the new diagram to type to the detectType in utils.js. The detection should return a key for the new diagram type. +The second thing to do is to add the capability to detect the new diagram to type to the detectType in `diagram-api/detectType.ts`. The detection should return a key for the new diagram type. [This key will be used to as the aria roledescription](#aria-roledescription), so it should be a word that clearly describes the diagram type. For example, if your new diagram use a UML deployment diagram, a good key would be "UMLDeploymentDiagram" because assistive technologies such as a screen reader would voice that as "U-M-L Deployment diagram." Another good key would be "deploymentDiagram" because that would be voiced as "Deployment Diagram." A bad key would be "deployment" because that would not sufficiently describe the diagram. diff --git a/packages/mermaid/src/mermaidAPI.ts b/packages/mermaid/src/mermaidAPI.ts index 70b314daa..f8a36f88e 100644 --- a/packages/mermaid/src/mermaidAPI.ts +++ b/packages/mermaid/src/mermaidAPI.ts @@ -108,8 +108,7 @@ export interface RenderResult { async function parse(text: string, parseOptions?: ParseOptions): Promise { addDiagrams(); try { - const diagram = await getDiagramFromText(text); - diagram.parse(); + await getDiagramFromText(text); } catch (error) { if (parseOptions?.suppressErrors) { return false;