forked from gmunguia/markdown-it-plantuml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (118 loc) · 4.05 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Process block-level uml diagrams
//
'use strict';
module.exports = function umlPlugin(md, options) {
function generateSourceDefault(umlCode, pluginOptions) {
var imageFormat = pluginOptions.imageFormat || 'svg';
var diagramName = pluginOptions.diagramName || 'uml';
var server = pluginOptions.server || 'https://www.plantuml.com/plantuml';
var deflate = require('./lib/deflate.js');
var zippedCode = deflate.encode64(
deflate.zip_deflate(
unescape(encodeURIComponent(
'@start' + diagramName + '\n' + umlCode + '\n@end' + diagramName)),
9
)
);
return server + '/' + imageFormat + '/' + zippedCode;
}
options = options || {};
var openMarker = options.openMarker || '@startuml',
openChar = openMarker.charCodeAt(0),
closeMarker = options.closeMarker || '@enduml',
closeChar = closeMarker.charCodeAt(0),
render = options.render || md.renderer.rules.image,
generateSource = options.generateSource || generateSourceDefault;
function uml(state, startLine, endLine, silent) {
var nextLine, markup, params, token, i,
autoClosed = false,
start = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];
// Check out the first character quickly,
// this should filter out most of non-uml blocks
//
if (openChar !== state.src.charCodeAt(start)) { return false; }
// Check out the rest of the marker string
//
for (i = 0; i < openMarker.length; ++i) {
if (openMarker[i] !== state.src[start + i]) { return false; }
}
markup = state.src.slice(start, start + i);
params = state.src.slice(start + i, max);
// Since start is found, we can report success here in validation mode
//
if (silent) { return true; }
// Search for the end of the block
//
nextLine = startLine;
for (;;) {
nextLine++;
if (nextLine >= endLine) {
// unclosed block should be autoclosed by end of document.
// also block seems to be autoclosed by end of parent
break;
}
start = state.bMarks[nextLine] + state.tShift[nextLine];
max = state.eMarks[nextLine];
if (start < max && state.sCount[nextLine] < state.blkIndent) {
// non-empty line with negative indent should stop the list:
// - ```
// test
break;
}
if (closeChar !== state.src.charCodeAt(start)) {
// didn't find the closing fence
continue;
}
if (state.sCount[nextLine] > state.sCount[startLine]) {
// closing fence should not be indented with respect of opening fence
continue;
}
var closeMarkerMatched = true;
for (i = 0; i < closeMarker.length; ++i) {
if (closeMarker[i] !== state.src[start + i]) {
closeMarkerMatched = false;
break;
}
}
if (!closeMarkerMatched) {
continue;
}
// make sure tail has spaces only
if (state.skipSpaces(start + i) < max) {
continue;
}
// found!
autoClosed = true;
break;
}
var contents = state.src
.split('\n')
.slice(startLine + 1, nextLine)
.join('\n');
// We generate a token list for the alt property, to mimic what the image parser does.
var altToken = [];
// Remove leading space if any.
var alt = params ? params.slice(1) : 'uml diagram';
state.md.inline.parse(
alt,
state.md,
state.env,
altToken
);
token = state.push('uml_diagram', 'img', 0);
// alt is constructed from children. No point in populating it here.
token.attrs = [ [ 'src', generateSource(contents, options) ], [ 'alt', '' ] ];
token.block = true;
token.children = altToken;
token.info = params;
token.map = [ startLine, nextLine ];
token.markup = markup;
state.line = nextLine + (autoClosed ? 1 : 0);
return true;
}
md.block.ruler.before('fence', 'uml_diagram', uml, {
alt: [ 'paragraph', 'reference', 'blockquote', 'list' ]
});
md.renderer.rules.uml_diagram = render;
};