-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathmermaid.js
192 lines (168 loc) · 5.21 KB
/
mermaid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* Web page integration module for the mermaid framework. It uses the mermaidAPI for mermaid functionality and to render
* the diagrams to svg code.
*/
import he from 'he'
import mermaidAPI from './mermaidAPI'
import { logger } from './logger'
import pkg from '../package.json'
let nextId = 0
/**
* ## init
* Function that goes through the document to find the chart definitions in there and render them.
*
* The function tags the processed attributes with the attribute data-processed and ignores found elements with the
* attribute already set. This way the init function can be triggered several times.
*
* Optionally, `init` can accept in the second argument one of the following:
* - a DOM Node
* - an array of DOM nodes (as would come from a jQuery selector)
* - a W3C selector, a la `.mermaid`
*
* ```mermaid
* graph LR;
* a(Find elements)-->b{Processed}
* b-->|Yes|c(Leave element)
* b-->|No |d(Transform)
* ```
* Renders the mermaid diagrams
* @param nodes a css selector or an array of nodes
*/
const init = function () {
const conf = mermaidAPI.getConfig()
logger.debug('Starting rendering diagrams')
let nodes
if (arguments.length >= 2) {
/*! sequence config was passed as #1 */
if (typeof arguments[0] !== 'undefined') {
mermaid.sequenceConfig = arguments[0]
}
nodes = arguments[1]
} else {
nodes = arguments[0]
}
// if last argument is a function this is the callback function
let callback
if (typeof arguments[arguments.length - 1] === 'function') {
callback = arguments[arguments.length - 1]
logger.debug('Callback function found')
} else {
if (typeof conf.mermaid !== 'undefined') {
if (typeof conf.mermaid.callback === 'function') {
callback = conf.mermaid.callback
logger.debug('Callback function found')
} else {
logger.debug('No Callback function found')
}
}
}
nodes = nodes === undefined ? document.querySelectorAll('.mermaid')
: typeof nodes === 'string' ? document.querySelectorAll(nodes)
: nodes instanceof window.Node ? [nodes]
: nodes // Last case - sequence config was passed pick next
if (typeof global.mermaid_config !== 'undefined') {
mermaidAPI.initialize(global.mermaid_config)
}
logger.debug('Start On Load before: ' + mermaid.startOnLoad)
if (typeof mermaid.startOnLoad !== 'undefined') {
logger.debug('Start On Load inner: ' + mermaid.startOnLoad)
mermaidAPI.initialize({ startOnLoad: mermaid.startOnLoad })
}
if (typeof mermaid.ganttConfig !== 'undefined') {
mermaidAPI.initialize({ gantt: mermaid.ganttConfig })
}
let txt
for (let i = 0; i < nodes.length; i++) {
const element = nodes[i]
/*! Check if previously processed */
if (!element.getAttribute('data-processed')) {
element.setAttribute('data-processed', true)
} else {
continue
}
const id = 'mermaidChart' + nextId++
// Fetch the graph definition including tags
txt = element.innerHTML
// transforms the html to pure text
txt = he.decode(txt).trim()
mermaidAPI.render(id, txt, (svgCode, bindFunctions) => {
element.innerHTML = svgCode
if (typeof callback !== 'undefined') {
callback(id)
}
bindFunctions(element)
}, element)
}
}
const version = function () {
return 'v' + pkg.version
}
const initialize = function (config) {
logger.debug('Initializing mermaid')
if (typeof config.mermaid !== 'undefined') {
if (typeof config.mermaid.startOnLoad !== 'undefined') {
mermaid.startOnLoad = config.mermaid.startOnLoad
}
if (typeof config.mermaid.htmlLabels !== 'undefined') {
mermaid.htmlLabels = config.mermaid.htmlLabels
}
}
mermaidAPI.initialize(config)
}
/**
* ##contentLoaded
* Callback function that is called when page is loaded. This functions fetches configuration for mermaid rendering and
* calls init for rendering the mermaid diagrams on the page.
*/
const contentLoaded = function () {
let config
// Check state of start config mermaid namespace
if (typeof global.mermaid_config !== 'undefined') {
if (global.mermaid_config.htmlLabels === false) {
mermaid.htmlLabels = false
}
}
if (mermaid.startOnLoad) {
// For backwards compatability reasons also check mermaid_config variable
if (typeof global.mermaid_config !== 'undefined') {
// Check if property startOnLoad is set
if (global.mermaid_config.startOnLoad === true) {
mermaid.init()
}
} else {
// No config found, do check API config
config = mermaidAPI.getConfig()
if (config.startOnLoad) {
mermaid.init()
}
}
} else {
if (typeof mermaid.startOnLoad === 'undefined') {
logger.debug('In start, no config')
config = mermaidAPI.getConfig()
if (config.startOnLoad) {
mermaid.init()
}
}
}
}
if (typeof document !== 'undefined') {
/*!
* Wait for document loaded before starting the execution
*/
window.addEventListener('load', function () {
contentLoaded()
}, false)
}
const mermaid = {
startOnLoad: true,
htmlLabels: true,
mermaidAPI,
parse: mermaidAPI.parse,
render: mermaidAPI.render,
init,
initialize,
version,
contentLoaded
}
export default mermaid