-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (39 loc) · 991 Bytes
/
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
//@ts-check
const visit = require('unist-util-visit')
const PINTORA = require('@pintora/cli')
function pintoraNodes(markdownAST, language) {
const result = []
visit(markdownAST, 'code', (/** @type {any} */node) => {
if ((node.lang || '').toLowerCase() === language) {
result.push(node)
}
})
return result
}
module.exports = async (
{ markdownAST },
{ language = 'pintora', theme = 'default', backgroundColor }
) => {
// Check if there is a match before launching anything
let nodes = pintoraNodes(markdownAST, language)
if (nodes.length === 0) {
// No nodes to process
return
}
PINTORA.setConfig({
themeConfig: {
theme,
}
})
await Promise.all(
nodes.map(async (node) => {
node.type = 'html'
const svgString = await PINTORA.render({
code: node.value,
mimeType: 'image/svg+xml',
backgroundColor,
})
node.value = `<div class="pintora-diagram">${svgString}</div>`
})
)
}