forked from facebook/docusaurus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
48 lines (43 loc) · 1.24 KB
/
Copy pathindex.ts
File metadata and controls
48 lines (43 loc) · 1.24 KB
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import visit from 'unist-util-visit';
import type {Transformer} from 'unified';
import type {Data, Literal, Node, Parent} from 'unist';
type CodeMermaid = Literal<string> & {
type: 'code';
lang: 'mermaid';
};
function processMermaidNode(
node: CodeMermaid,
index: number,
parent: Parent<Node<Data> | Literal, Data>,
) {
parent.children.splice(index, 1, {
type: 'jsx',
value: `<mermaid value={\`${node.value}\`}/>`,
position: node.position,
});
}
export default function plugin(): Transformer {
return async (root) => {
// Find all the mermaid diagram code blocks. i.e. ```mermaid
const instances: [CodeMermaid, number, Parent<Node<Data>, Data>][] = [];
visit(
root,
{type: 'code', lang: 'mermaid'},
(node: CodeMermaid, index, parent) => {
if (parent) {
instances.push([node, index, parent]);
}
},
);
// Replace each Mermaid code block with the Mermaid component
instances.forEach(([node, index, parent]) => {
processMermaidNode(node, index, parent);
});
};
}