-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
70 lines (57 loc) · 1.64 KB
/
index.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
var path = require('path');
var readFileSync = require('fs').readFileSync;
var url = require('url');
var phantom = require('phantom');
var Q = require('q');
const PHANTOMJS_MODULE = require.resolve('phantomjs')
const PHANTOMJS_BIN = path.resolve(PHANTOMJS_MODULE, '../../bin', 'phantomjs')
module.exports = {
blocks: {
mermaid: {
process: function(block) {
var body = block.body;
var src = block.kwargs.src;
if(src) {
var relativeSrcPath = url.resolve(this.ctx.file.path, src)
var absoluteSrcPath = decodeURI(path.resolve(this.book.root, relativeSrcPath))
body = readFileSync(absoluteSrcPath, 'utf8')
}
return processBlock(body);
}
}
}
};
function processBlock(body) {
return convertToSvg(body)
.then(function (svgCode) {
return svgCode.replace(/mermaidChart1/g, getId());
});
}
function convertToSvg(mermaidCode) {
var deferred = Q.defer();
phantom.create({binary: PHANTOMJS_BIN}, function (ph) {
ph.createPage(function (page) {
var htmlPagePath = path.join(__dirname, 'convert/converter.html');
page.open(htmlPagePath, function (status) {
page.evaluate(
function (code) {
return renderToSvg(code);
},
function (result) {
ph.exit();
deferred.resolve(result);
},
mermaidCode);
});
});
});
return deferred.promise;
}
function getId() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return "mermaidChart-" + s4() + s4();
}