forked from nf-core/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remark-mermaid.ts
30 lines (25 loc) · 904 Bytes
/
remark-mermaid.ts
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
// taken from https://github.com/JuanM04/portfolio/blob/983b0ed0eabdac37bf8b7912d3e8128a443192b9/src/plugins/mermaid.ts
import type { RemarkPlugin } from '@astrojs/markdown-remark';
import { visit } from 'unist-util-visit';
const escapeMap: Record<string, string> = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
};
const escapeHtml = (str: string) => str.replace(/[&<>"']/g, (c) => escapeMap[c]);
export const mermaid: RemarkPlugin<[]> = () => (tree) => {
visit(tree, 'code', (node) => {
if (node.lang !== 'mermaid') return;
// @ts-ignore
node.type = 'html';
node.value = `
<div class="mermaid" data-content="${escapeHtml(node.value)}">
<i class="mt-5 m-auto text-success fa-regular fa-spinner-third fa-spin fa-3x"></i>
<p>Loading graph</p>
</div>
`;
});
};
export default mermaid;