-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
57 lines (53 loc) · 1.56 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
const puppeteer = require("puppeteer")
const SCRIPT_ERROR =
"Invalid script identifier. Please see: https://github.com/muhammadmuzzammil1998/headless-mermaid#calling-the-api"
async function execute(
code,
config = {},
script = "mermaid.min@8.5.2",
diagramId = "id1"
) {
let browser = await puppeteer.launch({ args: ["--no-sandbox"] }),
scriptData = script.split("@").map((x) => x.trim())
if (scriptData.filter((x) => x != "").length < 2) {
throw SCRIPT_ERROR
}
let filename = scriptData[0],
version = scriptData[1]
if (version.split(".").length < 3) {
throw SCRIPT_ERROR
}
if (!filename.endsWith(".js")) filename += ".js"
try {
let page = await browser.newPage()
await page.goto(
`data:text/html,<script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/${version}/${filename}"></script>`
)
const result = await page.evaluate(
(config, code, diagramId) => {
window.mermaid.initialize(config)
try {
const svgCode = window.mermaid.mermaidAPI.render(diagramId, code)
return { status: "success", svgCode }
} catch (error) {
return { status: "error", error, message: error.message }
}
},
config,
code,
diagramId
)
if (result.status === "error") {
throw `Mermaid error:\n${result.message}`
}
return result.svgCode.replace(
"</svg>",
"<!-- Rendered using headless-mermaid - npmjs.com/headless-mermaid --> </svg>"
)
} finally {
await browser.close()
}
}
module.exports = {
execute: execute,
}