-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
29 lines (26 loc) · 1.2 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
hexo.extend.filter.register('before_post_render', render, 9);
function ignore(data) {
let source = data.source;
let ext = source.substring(source.lastIndexOf('.')).toLowerCase();
return ['.js', '.css', '.html', '.htm'].indexOf(ext) > -1;
}
function render(data) {
let reg = /(\s*)(```) *(mermaid) *\n?([\s\S]+?)\s*(\2)(\n+|$)/g;
if (!ignore(data)) {
let matched = false;
data.content = data.content
.replace(reg, function(raw, start, startQuote, lang, content, endQuote, end) {
matched = true;
return start + '<pre class="mermaid">' + content + '</pre>' + end;
// use this if you also need to display the original code
// return start + '<pre class="mermaid">'+content+'</pre>' + end + raw;
});
if (matched) {
// https://mermaid.js.org/intro/n00b-gettingStarted.html#_3-calling-the-javascript-api
// https://mermaid.js.org/config/setup/modules/mermaidAPI.html
// https://mermaid.js.org/syntax/flowchart.html#styling-line-curves
// always use the latest one
data.content += `\n\n<script type="module"> import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs'; mermaid.initialize({startOnLoad: true, flowchart: {curve: 'linear'}}); </script>`;
}
}
};