-
Notifications
You must be signed in to change notification settings - Fork 1
/
diagrams-extension.js
95 lines (85 loc) · 2.88 KB
/
diagrams-extension.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Support for text format sequence diagrams and flowcharts
*/
(function (extension) {
'use strict';
if (typeof showdown !== 'undefined') {
// global (browser or nodejs global)
extension(showdown);
} else if (typeof define === 'function' && define.amd) {
// AMD
define(['showdown'], extension);
} else if (typeof exports === 'object') {
// Node, CommonJS-like
module.exports = extension(require('showdown'));
} else {
// showdown was not found so we throw
throw Error('Could not find showdown library');
}
}(function (showdown) {
'use strict';
var diagramSeqBlocks = [];
var diagramFlowBlocks = [];
/**
* 支持时序图和流程图的编辑,语法参见:js-sequence-diagrams 和 flowchart。
*
* Support for the editing of sequence diagrams and flowcharts. See the syntax: js-sequence-diagrams and flowchart.
*/
showdown.extension('diagrams', function () {
return [
{
type: 'lang',
regex: '(?:^|\\n)```seq(.*)\\n([\\s\\S]*?)\\n```',
replace: function (match, leadingSlash, codeblock) {
// Check if we matched the leading \ and return nothing changed if so
if (leadingSlash === '\\') {
return match;
} else {
return '\n\n~X' + (diagramSeqBlocks.push({text: match.substring(1), codeblock: codeblock}) - 1) + 'X\n\n';
}
}
},
{
type: 'lang',
regex: '(?:^|\\n)```flow(.*)\\n([\\s\\S]*?)\\n```',
replace: function (match, leadingSlash, codeblock) {
// Check if we matched the leading \ and return nothing changed if so
if (leadingSlash === '\\') {
return match;
} else {
return '~Y' + (diagramFlowBlocks.push({text: match, codeblock: codeblock}) - 1) + 'Y';
}
}
},
{
type: 'output',
regex: '~(X|Y)(\\d+)\\1',
replace: function (match, leadingSlash, index) {
// Check if we matched the leading \ and return nothing changed if so
if (leadingSlash === '\\') {
return match;
} else {
index = Number(index);
if ('X' == match.charAt(1)) {
var seq = diagramSeqBlocks[index].codeblock;
return '<div style="white-space: pre" class="diagram seq", id="diagram_seq_' + index + '">' + seq + '</div>';
} else {
var flow = diagramFlowBlocks[index].codeblock;
return '<div style="white-space: pre" class="diagram flow", id="diagram_flow_' + index + '">' + flow + '</div>';
}
}
}
},
// 清除缓存
// clear cache
{
type: 'output',
filter: function (text, globals_converter, options) {
diagramSeqBlocks = [];
diagramFlowBlocks = [];
return text;
}
},
];
});
}));