-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
165 lines (147 loc) · 4.91 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
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
#!/usr/bin/env node
"use strict";
const path = require("path");
const puppeteer = require("puppeteer");
/**
* decode HTML entities
* @param {string} encodedString
* @returns {string} The decoded string
*/
function decodeHTMLEntities(encodedString) {
const translate_regex = /&(nbsp|amp|quot|lt|gt);/g;
const translate_map = {
nbsp: " ",
amp: "&",
quot: "\"",
lt: "<",
gt: ">",
};
return encodedString
.replace(translate_regex, (_, entity) => translate_map[entity] || entity)
.replace(/&#(\w+);/gi, (_, charCode) =>
String.fromCharCode(parseInt(charCode))
)
}
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
let info;
let value;
try {
info = gen[key](arg);
value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
}
function _asyncToGenerator(fn) {
return function () {
const self = this, args = arguments;
return new Promise(function (resolve, reject) {
const gen = fn.apply(self, args);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
const parseMMD = /*#__PURE__*/function () {
const ref2 = _asyncToGenerator(function* (browser, definition, config) {
const page = yield browser.newPage();
page.setViewport({
width: 800, height: 600, deviceScaleFactor: 1
}).catch(e => {
throw new Error(e);
});
yield page.goto(`file://${path.join(__dirname, "index.html")}`);
yield page.evaluate(`document.body.style.background = 'transparent'`);
/* istanbul ignore next */
yield page.$eval("#container", (container, definition) => {
container.textContent = definition;
window.mermaid.initialize({ theme: "default" });
try {
window.mermaid.init(undefined, container);
return {
status: "success"
};
} catch (error) {
return {
status: "error", error, message: error.message
};
}
}, definition, config);
let extension = (config && config.extension) || "svg";
switch (extension) {
case "png":
/* istanbul ignore next */
const clip = yield page.$eval("svg", svg => {
const react = svg.getBoundingClientRect();
return {
x: Math.floor(react.left),
y: Math.floor(react.top),
width: Math.ceil(react.width),
height: Math.ceil(react.height)
};
});
yield page.setViewport({
width: clip.x + clip.width,
height: clip.y + clip.height,
deviceScaleFactor: 1
});
/** @type {Buffer} buffer */
const buffer = yield page.screenshot({
clip,
omitBackground: true
});
const base64Url = Buffer.from(buffer).toString("base64");
return `<img style="max-width: 100%" alt="Diagram" src="data:image/png;base64,${base64Url}" />`;
case "svg":
/* istanbul ignore next */
const svg = yield page.$eval("#container", (container) => {
let _container$getElement, _container$getElement2;
const svg = (_container$getElement = container.getElementsByTagName) === null || _container$getElement === void 0 ? void 0 : (_container$getElement2 = _container$getElement.call(container, "svg")) === null || _container$getElement2 === void 0 ? void 0 : _container$getElement2[0];
if (svg.style) {
svg.style.backgroundColor = "transparent";
}
return container.innerHTML;
});
const svg_xml = svg.replace(/<br>/gi, "<br/>");
return svg_xml;
default:
throw new Error(`Unsupported extension: ${extension}`);
break;
}
});
return function parseMMD(_x2, _x3, _x4) {
return ref2.apply(this, arguments);
};
}();
module.exports = function mermaidParse(definition, config, puppeteerConfig = undefined) {
return _asyncToGenerator(function* () {
const browser = yield puppeteer.launch(puppeteerConfig);
/** @type {string} */
const output = yield parseMMD(browser, decodeHTMLEntities(definition), config);
yield browser.close();
if (typeof output === "string" && output.trim().startsWith("<div id=\"dmermaid-")) {
const errorTextStart = `<text.*?class=".*?error-text.*?".*?>`;
const errorTextEnd = `</text>`;
const errorTextRegEx = new RegExp(`${errorTextStart}(.|\\n)*?${errorTextEnd}`);
const errorBlock = output.match(errorTextRegEx)[0];
const error = new Error(errorBlock
.replace(new RegExp(errorTextStart), "")
.replace(new RegExp(errorTextEnd), "")
.trim());
error.output = output;
throw error;
}
return output;
})();
}