-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
31 lines (27 loc) · 861 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
const visit = require('unist-util-visit');
const { escapeScript } = require('markmap-common');
const { Transformer } = require('markmap-lib');
const transformer = new Transformer();
const getId = (
(id) => () =>
++id
)(0);
function createMarkmap(content) {
const { root, frontmatter } = transformer.transform(content);
return [
`<div id="markmap-${getId()}" class="gatsby-markmap">`,
'<script type="text/markmap">',
escapeScript(JSON.stringify({ data: root, options: frontmatter?.markmap })),
'</script>',
'</div>',
].join('');
}
module.exports = ({ markdownAST }, pluginOptions) => {
visit(markdownAST, 'code', (node) => {
if (node.lang === 'markmap' || node.lang === 'markdown' && node.meta === 'markmap') {
node.type = 'html';
node.value = createMarkmap(node.value);
}
});
return markdownAST;
};