forked from superj80820/mermaid-js-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.js
73 lines (63 loc) · 1.84 KB
/
converter.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
const Base64 = require("js-base64");
function mdToSVG(data) {
const matchData = data.match(/```mermaid(.|\n)*?```/gm);
const jsonStrings = matchData
.map((item) => item.replace("```mermaid", "").replace("```", ""))
// Workaround for classdiagram
.map((item) =>
item.startsWith("\nclass") ||
item.startsWith("\ngantt") ||
item.startsWith("\nerDiagram") ||
item.startsWith("\njourney")
? item.substr(1, item.length - 1)
: item
)
.map((item) =>
JSON.stringify({
code: item,
mermaid: {
theme: "default",
},
})
)
.map((item) => {
const jsonString = Base64.encodeURI(item);
return `[![](https://mermaid.ink/img/${jsonString})](https://mermaid-js.github.io/mermaid-live-editor/#/edit/${jsonString})`;
});
let changeMd = data;
matchData.forEach((item, index) => {
changeMd = changeMd.replace(item, jsonStrings[index]);
});
return changeMd;
}
function SVGToMd(data) {
const matchData = data.match(
/\[\!\[\]\(https:\/\/mermaid\.ink\/img\/(.|\n)*?\)\n/gm
);
const encodedURIs = matchData.map((item) => {
item = item.replace("[![](https://mermaid.ink/img/", "");
return item.substr(
0,
item.indexOf(")](https://mermaid-js.github.io/mermaid-live-editor/#/")
);
});
let originMd = data;
matchData.forEach((item, index) => {
// Workaround for classdiagram about assignment let
let { code } = JSON.parse(Base64.decode(encodedURIs[index]));
// Workaround for classdiagram
if (
code.startsWith("class") ||
code.startsWith("gantt") ||
code.startsWith("erDiagram") ||
code.startsWith("journey")
)
code = `\n${code}`;
originMd = originMd.replace(item, "```mermaid" + code + "```\n");
});
return originMd;
}
module.exports = {
mdToSVG,
SVGToMd,
};