forked from remcohaszing/gatsby-remark-mermaid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
122 lines (111 loc) · 3.55 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
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
const visit = require('unist-util-visit');
const puppeteer = require('puppeteer');
class HeadlessMermaidError extends Error {
constructor(message, cause) {
const m = `${message}: ${cause.message}\n${cause.stack}`
super(m)
this.name = "HeadlessMermaidError"
this.cause = cause
}
}
async function render(page, id, definition) {
const {success,svgCode,error} = await page.evaluate((id, definition) => {
try {
const svgCode = window.mermaid.mermaidAPI.render(id, definition)
return {
success: true,
svgCode,
error: null
}
;
} catch (e) {
return {
success: false,
svgCode: null,
error: JSON.stringify(e, Object.getOwnPropertyNames(e))
}
}
}, id, definition);
if(success){
return svgCode
} else {
throw new HeadlessMermaidError("failed to render SVG", JSON.parse(error))
}
}
function mermaidNodes(markdownAST, language) {
const result = []
visit(markdownAST, 'code', node => {
if ((node.lang || '').toLowerCase() === language) {
result.push(node)
}
});
return result;
}
async function getMermaidBrowser(theme, viewport, mermaidOptions) {
try{
browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
const page = await browser.newPage();
page.setViewport(viewport);
await page.goto('data:text/html,<html></html>');
await page.addScriptTag({
path: require.resolve('mermaid/dist/mermaid.min.js')
});
const {success, error} = await page.evaluate((theme, mermaidOptions) => {
try {
window.mermaid.initialize({
...mermaidOptions,
theme
})
return {
success: true,
error: null
}
} catch (e) {
return {
success: false,
error: JSON.stringify(e, Object.getOwnPropertyNames(e))
}
}
}, theme, mermaidOptions);
if(success){
return {browser,page}
} else {
throw new HeadlessMermaidError("failed to initialize mermaid", JSON.parse(error))
}
} catch(e) {
if (e instanceof HeadlessMermaidError){
throw e
} else {
throw new HeadlessMermaidError("failed to initialize browser", e)
}
}
}
module.exports = async ({markdownAST},
{
language = 'mermaid',
theme = 'default',
viewport = {height: 200, width: 200},
mermaidOptions = {}
}) => {
// Check if there is a match before launching anything
let nodes = mermaidNodes(markdownAST, language);
if (nodes.length === 0) {
// No nodes to process
return;
}
// Launch virtual browser
const {browser,page} = await getMermaidBrowser(theme, viewport, mermaidOptions)
console.log("browser created.")
let count = 0
try {
await Promise.all(nodes.map(async node => {
node.type = 'html';
const svgCode = await render(page, `mermaid${count}`, node.value);
node.value = svgCode
}));
} finally {
if (browser) {
await browser.close();
}
}
};