-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathindex.js
42 lines (34 loc) · 1.04 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
const visit = require(`unist-util-visit`)
const Viz = require(`viz.js`)
const { Module, render } = require(`viz.js/full.render.js`)
const viz = new Viz({ Module, render })
const validLanguages = [`dot`, `circo`]
module.exports = async ({ markdownAST }, pluginOptions = {}) => {
let codeNodes = []
visit(markdownAST, `code`, node => {
// Only act on languages supported by graphviz
if (validLanguages.includes(node.lang)) {
codeNodes.push(node)
}
return node
})
await Promise.all(
codeNodes.map(async node => {
const { value, lang } = node
try {
// Perform actual render
const svgString = await viz.renderString(value, { engine: lang })
// Mutate the current node. Converting from a code block to
// HTML (with svg content)
node.type = `html`
node.value = svgString
} catch (error) {
console.log(
`Error during viz.js execution. Leaving code block unchanged`
)
console.log(error)
}
return node
})
)
}