1
- function convertYamlToMermaid ( yamlInput ) {
2
- // Parse the YAML input
3
- var rules = jsyaml . load ( yamlInput ) ;
1
+ // yamlToMermaid.js
4
2
5
- // Generate the Mermaid code
6
- var mermaidCode = 'flowchart TD\n' ;
7
- var declaredElements = { } ;
8
- var metadata = { } ;
9
-
10
- function generateConditionDeclaration ( conditionName , description ) {
11
- if ( declaredElements [ conditionName ] ) return "" ;
12
- return ` ${ conditionName } {"\`${ sanitize ( description ) ?? conditionName } \`"}\n` ;
3
+ ( function ( root , factory ) {
4
+ if ( typeof define === 'function' && define . amd ) {
5
+ // AMD. Register as an anonymous module.
6
+ define ( [ 'js-yaml' ] , factory ) ;
7
+ } else if ( typeof module === 'object' && module . exports ) {
8
+ // Node. Does not work with strict CommonJS, but
9
+ // only CommonJS-like environments that support module.exports,
10
+ // like Node.
11
+ module . exports = factory ( require ( 'js-yaml' ) ) ;
12
+ } else {
13
+ // Browser globals (root is window)
14
+ root . convertYamlToMermaid = factory ( root . jsyaml ) ;
13
15
}
16
+ } ( typeof self !== 'undefined' ? self : this , function ( jsyaml ) {
14
17
15
- function generateActionDeclaration ( actionName , description ) {
16
- if ( declaredElements [ conditionName ] ) return "" ;
17
- return ` ${ actionName } ["\`${ sanitize ( description ) ?? actionName } \`"]\n` ;
18
- }
18
+ function convertYamlToMermaid ( yamlInput ) {
19
+ // Parse the YAML input
20
+ const rules = jsyaml . load ( yamlInput ) ;
19
21
20
- function generateTerminateDeclaration ( terminateName ) {
21
- if ( declaredElements [ conditionName ] ) return "" ;
22
- return ` ${ terminateName } ((( )))\n` ;
23
- }
22
+ // Generate the Mermaid code
23
+ let mermaidCode = 'flowchart TD\n' ;
24
+ const declaredElements = { } ;
25
+ const metadata = { } ;
24
26
25
- function generateConnection ( from , to , label ) {
26
- if ( label ) return ` ${ from } -->|${ label } | ${ to } \n` ;
27
- return ` ${ from } --> ${ to } \n` ;
28
- }
27
+ function generateConditionDeclaration ( conditionName , description ) {
28
+ if ( declaredElements [ conditionName ] ) return "" ;
29
+ declaredElements [ conditionName ] = true ;
30
+ return ` ${ conditionName } {"\`${ sanitize ( description ) ?? conditionName } \`"}\n` ;
31
+ }
32
+
33
+ function generateActionDeclaration ( actionName , description ) {
34
+ if ( declaredElements [ actionName ] ) return "" ;
35
+ declaredElements [ actionName ] = true ;
36
+ return ` ${ actionName } ["\`${ sanitize ( description ) ?? actionName } \`"]\n` ;
37
+ }
38
+
39
+ function generateTerminateDeclaration ( terminateName ) {
40
+ if ( declaredElements [ terminateName ] ) return "" ;
41
+ declaredElements [ terminateName ] = true ;
42
+ return ` ${ terminateName } ((( )))\n` ;
43
+ }
44
+
45
+ function generateConnection ( from , to , label ) {
46
+ return label ? ` ${ from } -->|${ label } | ${ to } \n` : ` ${ from } --> ${ to } \n` ;
47
+ }
29
48
30
- function generateDecision ( conditionName , decision , label ) {
31
- if ( ! decision || ! conditionName ) return "" ;
32
-
33
- code = ""
34
-
35
- if ( decision . action ) {
36
- var actionId = conditionName + `_${ label } ` ;
49
+ function generateDecision ( conditionName , decision , label ) {
50
+ if ( ! decision || ! conditionName ) return "" ;
37
51
38
- // add action element declaration; empty if it's already declared
39
- code += generateActionDeclaration ( actionId , decision . description ) ;
52
+ let code = "" ;
40
53
41
- // connection from condition to true action
42
- code += generateConnection ( conditionName , actionId , label ) ;
43
-
44
- if ( decision . next ) {
45
- // connection from action to next condition
46
- code += generateConnection ( actionId , decision . next , label ) ;
47
- } else if ( decision . terminate ) {
48
- var trueTerminateID = conditionName + `_${ label } _end` ;
49
- code += generateTerminateDeclaration ( trueTerminateID ) ;
50
- code += generateConnection ( actionId , trueTerminateID ) ;
51
- }
52
- } else {
53
- if ( decision . next ) {
54
- code += generateConnection ( conditionName , decision . next , label ) ;
55
- } else if ( decision . terminate ) {
56
- var trueTerminateID = conditionName + `_${ label } _end` ;
57
- code += generateTerminateDeclaration ( trueTerminateID ) ;
58
- code += generateConnection ( conditionName , trueTerminateID , label ) ;
54
+ if ( decision . action ) {
55
+ const actionId = conditionName + `_${ label } ` ;
56
+
57
+ code += generateActionDeclaration ( actionId , decision . description ) ;
58
+ code += generateConnection ( conditionName , actionId , label ) ;
59
+
60
+ if ( decision . next ) {
61
+ code += generateConnection ( actionId , decision . next , label ) ;
62
+ } else if ( decision . terminate ) {
63
+ const terminateId = conditionName + `_${ label } _end` ;
64
+ code += generateTerminateDeclaration ( terminateId ) ;
65
+ code += generateConnection ( actionId , terminateId ) ;
66
+ }
67
+ } else {
68
+ if ( decision . next ) {
69
+ code += generateConnection ( conditionName , decision . next , label ) ;
70
+ } else if ( decision . terminate ) {
71
+ const terminateId = conditionName + `_${ label } _end` ;
72
+ code += generateTerminateDeclaration ( terminateId ) ;
73
+ code += generateConnection ( conditionName , terminateId , label ) ;
74
+ }
59
75
}
76
+
77
+ return code ;
60
78
}
61
-
62
- return code
63
- }
64
79
65
- for ( var conditionName in rules . conditions ) {
66
- var condition = rules . conditions [ conditionName ] ;
67
- condition . Name = conditionName ;
80
+ for ( const conditionName in rules . conditions ) {
81
+ const condition = rules . conditions [ conditionName ] ;
82
+ condition . Name = conditionName ;
68
83
69
- mermaidCode += generateConditionDeclaration ( condition . Name , condition . description ) ;
70
- mermaidCode += generateDecision ( conditionName , condition . true , 'true' ) ;
71
- mermaidCode += generateDecision ( conditionName , condition . false , 'false' ) ;
84
+ mermaidCode += generateConditionDeclaration ( condition . Name , condition . description ) ;
85
+ mermaidCode += generateDecision ( conditionName , condition . true , 'true' ) ;
86
+ mermaidCode += generateDecision ( conditionName , condition . false , 'false' ) ;
72
87
73
- // remember metadata
74
- metadata [ conditionName ] = { func : condition . Check , type : "condition" } ;
75
- if ( condition . true && condition . true . action ) {
76
- metadata [ conditionName + '_true' ] = { func : condition . true . action , type : "action" , value : true } ;
77
- }
78
- if ( condition . false && condition . false . action ) {
79
- metadata [ conditionName + '_false' ] = { func : condition . false . action , type : "action" , value : false } ;
88
+ // remember metadata
89
+ metadata [ conditionName ] = { func : condition . Check , type : "condition" } ;
90
+ if ( condition . true && condition . true . action ) {
91
+ metadata [ conditionName + '_true' ] = { func : condition . true . action , type : "action" , value : true } ;
92
+ }
93
+ if ( condition . false && condition . false . action ) {
94
+ metadata [ conditionName + '_false' ] = { func : condition . false . action , type : "action" , value : false } ;
95
+ }
80
96
}
97
+
98
+ return mermaidCode ;
81
99
}
82
100
83
- return mermaidCode ;
84
- }
101
+ function sanitize ( input ) {
102
+ return ! input ? input : input . replaceAll ( `"` , "&quot" ) ;
103
+ }
85
104
86
- function sanitize ( input ) {
87
- return ! input ? input : input . replaceAll ( `"` , "&quot" ) ;
88
- }
105
+ return convertYamlToMermaid ;
106
+ } ) ) ;
0 commit comments