Skip to content

Commit 9b892ef

Browse files
author
jjmr
committed
Add new parameter to the console client to override the svg configuration in sequence diagrams
1 parent 0b2afb8 commit 9b892ef

File tree

8 files changed

+136
-1101
lines changed

8 files changed

+136
-1101
lines changed

dist/mermaid.full.js

Lines changed: 37 additions & 533 deletions
Large diffs are not rendered by default.

dist/mermaid.full.min.js

Lines changed: 11 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/mermaid.slim.js

Lines changed: 32 additions & 528 deletions
Large diffs are not rendered by default.

dist/mermaid.slim.min.js

Lines changed: 6 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/cli.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function cli(options) {
2323
, svg: 's'
2424
, verbose: 'v'
2525
, phantomPath: 'e'
26+
, sequenceConfig: 'c'
2627
}
2728
, 'boolean': ['help', 'png', 'svg']
2829
, 'string': ['outputDir']
@@ -37,13 +38,14 @@ function cli(options) {
3738
, "file The mermaid description file to be rendered"
3839
, ""
3940
, "Options:"
40-
, " -s --svg Output SVG instead of PNG (experimental)"
41-
, " -p --png If SVG was selected, and you also want PNG, set this flag"
42-
, " -o --outputDir Directory to save files, will be created automatically, defaults to `cwd`"
43-
, " -e --phantomPath Specify the path to the phantomjs executable"
44-
, " -h --help Show this message"
45-
, " -v --verbose Show logging"
46-
, " --version Print version and quit"
41+
, " -s --svg Output SVG instead of PNG (experimental)"
42+
, " -p --png If SVG was selected, and you also want PNG, set this flag"
43+
, " -o --outputDir Directory to save files, will be created automatically, defaults to `cwd`"
44+
, " -e --phantomPath Specify the path to the phantomjs executable"
45+
, " -c --sequenceConfig Specify the path to the file with the configuration to be applied in the sequence diagram"
46+
, " -h --help Show this message"
47+
, " -v --verbose Show logging"
48+
, " --version Print version and quit"
4749
]
4850

4951
return this
@@ -70,7 +72,7 @@ cli.prototype.parse = function(argv, next) {
7072
}
7173

7274
// ensure that parameter-expecting options have parameters
73-
;['outputDir', 'phantomPath'].forEach(function(i) {
75+
;['outputDir', 'phantomPath', 'sequenceConfig'].forEach(function(i) {
7476
if(typeof options[i] !== 'undefined') {
7577
if (typeof options[i] !== 'string' || options[i].length < 1) {
7678
this.errors.push(new Error(i + " expects a value."))
@@ -86,6 +88,10 @@ cli.prototype.parse = function(argv, next) {
8688
options.png = true
8789
}
8890

91+
if (options.sequenceConfig) {
92+
options.sequenceConfig = checkConfig(options.sequenceConfig)
93+
}
94+
8995
this.checkPhantom = createCheckPhantom(options.phantomPath)
9096

9197
this.checkPhantom(function(err, path) {
@@ -102,6 +108,16 @@ cli.prototype.parse = function(argv, next) {
102108
}
103109
}
104110

111+
function checkConfig(configPath) {
112+
try {
113+
var text = fs.readFileSync(configPath, 'utf8')
114+
JSON.parse(text)
115+
return text
116+
} catch (e) {
117+
return null;
118+
}
119+
}
120+
105121
function createCheckPhantom(_phantomPath) {
106122
var phantomPath = _phantomPath
107123
, phantomVersion
@@ -128,7 +144,7 @@ function createCheckPhantom(_phantomPath) {
128144
, "details."
129145
].join('\n')
130146
)
131-
147+
132148
next(err)
133149
return
134150
}

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function processMermaid(files, _options, _next) {
1818
, outputDir
1919
, options.png
2020
, options.svg
21+
, options.sequenceConfig
2122
, options.verbose
2223
]
2324

lib/phantomscript.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ var system = require('system')
2929
, webpage = require('webpage')
3030

3131
var page = webpage.create()
32-
, files = phantom.args.slice(4, phantom.args.length)
32+
, files = phantom.args.slice(5, phantom.args.length)
3333
, options = {
3434
outputDir: phantom.args[0]
3535
, png: phantom.args[1] === 'true' ? true : false
3636
, svg: phantom.args[2] === 'true' ? true : false
37-
, verbose: phantom.args[3] === 'true' ? true : false
37+
, sequenceConfig: phantom.args[3]
38+
, verbose: phantom.args[4] === 'true' ? true : false
3839
}
3940
, log = logger(options.verbose)
4041

@@ -51,7 +52,9 @@ page.content = [
5152
].join('\n')
5253

5354
page.injectJs('../dist/mermaid.full.js')
54-
55+
page.onConsoleMessage = function(msg, lineNum, sourceId) {
56+
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
57+
};
5558
files.forEach(function(file) {
5659
var contents = fs.read(file)
5760
, filename = file.split(fs.separator).slice(-1)
@@ -63,7 +66,10 @@ files.forEach(function(file) {
6366
// this JS is executed in this statement is sandboxed, even though it doesn't
6467
// look like it. we need to serialize then unserialize the svgContent that's
6568
// taken from the DOM
66-
svgContent = page.evaluate(executeInPage, contents)
69+
svgContent = page.evaluate(executeInPage, {
70+
contents: contents,
71+
sequenceConfig: options.sequenceConfig
72+
})
6773
oDOM = oParser.parseFromString(svgContent, "text/xml")
6874

6975
resolveSVGElement(oDOM.firstChild)
@@ -182,8 +188,10 @@ function resolveForeignObjects(element) {
182188
}
183189

184190
// The sandboxed function that's executed in-page by phantom
185-
function executeInPage(contents) {
191+
function executeInPage(data) {
186192
var xmlSerializer = new XMLSerializer()
193+
, contents = data.contents
194+
, sequenceConfig = data.sequenceConfig
187195
, toRemove
188196
, el
189197
, elContent
@@ -204,7 +212,7 @@ function executeInPage(contents) {
204212

205213
document.body.appendChild(el)
206214

207-
mermaid.init()
215+
mermaid.init(sequenceConfig)
208216

209217
svg = document.querySelector('svg')
210218
svgValue = xmlSerializer.serializeToString(svg)

src/main.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ var he = require('he');
1818
* c-->|No |d(Transform);
1919
* ```
2020
*/
21-
var init = function () {
21+
var init = function (sequenceConfig) {
2222
var arr = document.querySelectorAll('.mermaid');
2323
var i;
2424

25+
if (sequenceConfig) {
26+
seq.setConf(JSON.parse(sequenceConfig));
27+
}
28+
2529
var cnt = 0;
2630
for (i = 0; i < arr.length; i++) {
2731
var element = arr[i];
@@ -51,18 +55,18 @@ var init = function () {
5155
var classes = {};
5256

5357
switch(graphType){
54-
case 'graph':
58+
case 'graph':
5559
classes = flowRenderer.getClasses(txt, false);
5660
flowRenderer.draw(txt, id, false);
5761
utils.cloneCssStyles(element.firstChild, classes);
5862
graph.bindFunctions();
5963
break;
60-
case 'dotGraph':
64+
case 'dotGraph':
6165
classes = flowRenderer.getClasses(txt, true);
6266
flowRenderer.draw(txt, id, true);
6367
utils.cloneCssStyles(element.firstChild, classes);
6468
break;
65-
case 'sequenceDiagram':
69+
case 'sequenceDiagram':
6670
seq.draw(txt,id);
6771
// TODO - Get styles for sequence diagram
6872
utils.cloneCssStyles(element.firstChild, []);
@@ -94,8 +98,8 @@ var equals = function (val, variable){
9498

9599
global.mermaid = {
96100
startOnLoad:true,
97-
init:function(){
98-
init();
101+
init:function(sequenceConfig){
102+
init(sequenceConfig);
99103
},
100104
version:function(){
101105
return exports.version();

0 commit comments

Comments
 (0)