-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
257 lines (199 loc) · 7.1 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* Process inline math */
'use strict';
var ascii2mathml = null;
require('./lib/polyfills');
function scanDelims(state, start, delimLength) {
var pos = start, lastChar, nextChar, count, can_open, can_close,
isLastWhiteSpace, isNextWhiteSpace,
left_flanking = true,
right_flanking = true,
max = state.posMax,
isWhiteSpace = state.md.utils.isWhiteSpace;
// treat beginning of the line as a whitespace
lastChar = start > 0 ? state.src.charCodeAt(start - 1) : 0x20;
if (pos >= max) {
can_open = false;
}
pos += delimLength;
count = pos - start;
// treat end of the line as a whitespace
nextChar = pos < max ? state.src.charCodeAt(pos) : 0x20;
isLastWhiteSpace = isWhiteSpace(lastChar);
isNextWhiteSpace = isWhiteSpace(nextChar);
if (isNextWhiteSpace) {
left_flanking = false;
}
if (isLastWhiteSpace) {
right_flanking = false;
}
can_open = left_flanking;
can_close = right_flanking;
return {
can_open: can_open,
can_close: can_close,
delims: count
};
}
function makeMath_inline(open, close) {
return function math_inline(state, silent) {
var startCount,
found,
res,
token,
closeDelim,
max = state.posMax,
start = state.pos,
openDelim = state.src.slice(start, start + open.length);
if (openDelim !== open) { return false; }
if (silent) { return false; } // Don’t run any pairs in validation mode
res = scanDelims(state, start, openDelim.length);
startCount = res.delims;
if (!res.can_open) {
state.pos += startCount;
// Earlier we checked !silent, but this implementation does not need it
state.pending += state.src.slice(start, state.pos);
return true;
}
state.pos = start + open.length;
while (state.pos < max) {
closeDelim = state.src.slice(state.pos, state.pos + close.length);
if (closeDelim === close) {
res = scanDelims(state, state.pos, close.length);
if (res.can_close) {
found = true;
break;
}
}
state.md.inline.skipToken(state);
}
if (!found) {
// Parser failed to find ending tag, so it is not a valid math
state.pos = start;
return false;
}
// Found!
state.posMax = state.pos;
state.pos = start + close.length;
// Earlier we checked !silent, but this implementation does not need it
token = state.push('math_inline', 'math', 0);
token.content = state.src.slice(state.pos, state.posMax);
token.markup = open;
state.pos = state.posMax + close.length;
state.posMax = max;
return true;
};
}
function makeMath_block(open, close) {
return function math_block(state, startLine, endLine, silent) {
var openDelim, len, params, nextLine, token, firstLine, lastLine, lastLinePos,
haveEndMarker = false,
pos = state.bMarks[startLine] + state.tShift[startLine],
max = state.eMarks[startLine];
if (pos + open.length > max) { return false; }
openDelim = state.src.slice(pos, pos + open.length);
if (openDelim !== open) { return false; }
pos += open.length;
firstLine = state.src.slice(pos, max);
// Since start is found, we can report success here in validation mode
if (silent) { return true; }
if (firstLine.trim().slice(-close.length) === close) {
// Single line expression
firstLine = firstLine.trim().slice(0, -close.length);
haveEndMarker = true;
}
// search end of block
nextLine = startLine;
for (;;) {
if (haveEndMarker) { break; }
nextLine++;
if (nextLine >= endLine) {
// unclosed block should be autoclosed by end of document.
// also block seems to be autoclosed by end of parent
break;
}
pos = state.bMarks[nextLine] + state.tShift[nextLine];
max = state.eMarks[nextLine];
if (pos < max && state.tShift[nextLine] < state.blkIndent) {
// non-empty line with negative indent should stop the list:
break;
}
if (state.src.slice(pos, max).trim().slice(-close.length) !== close) {
continue;
}
if (state.tShift[nextLine] - state.blkIndent >= 4) {
// closing block math should be indented less then 4 spaces
continue;
}
lastLinePos = state.src.slice(0, max).lastIndexOf(close);
lastLine = state.src.slice(pos, lastLinePos);
pos += lastLine.length + close.length;
// make sure tail has spaces only
pos = state.skipSpaces(pos);
if (pos < max) { continue; }
// found!
haveEndMarker = true;
}
// If math block has heading spaces, they should be removed from its inner block
len = state.tShift[startLine];
state.line = nextLine + (haveEndMarker ? 1 : 0);
token = state.push('math_block', 'math', 0);
token.block = true;
token.content = (firstLine && firstLine.trim() ? firstLine + '\n' : '') +
state.getLines(startLine + 1, nextLine, len, true) +
(lastLine && lastLine.trim() ? lastLine : '');
token.info = params;
token.map = [ startLine, state.line ];
token.markup = open;
return true;
};
}
function makeMathRenderer(renderingOptions) {
if (ascii2mathml === null) {
try {
ascii2mathml = require('ascii2mathml').default;
} catch (e) {
return renderingOptions && renderingOptions.display === 'block' ?
function(tokens, idx) {
return '<div class="math block">' + tokens[idx].content + '</div>';
} :
function(tokens, idx) {
return '<span class="math inline">' + tokens[idx].content + '</span>';
};
}
}
var mathml = ascii2mathml(Object.assign({}, renderingOptions));
return renderingOptions && renderingOptions.display === 'block' ?
function(tokens, idx) {
return mathml(tokens[idx].content) + '\n';
} :
function(tokens, idx) {
return mathml(tokens[idx].content);
};
}
module.exports = function math_plugin(md, options) {
// Default options
options = typeof options === 'object' ? options : {};
var inlineOpen = options.inlineOpen || '$$',
inlineClose = options.inlineClose || '$$',
blockOpen = options.blockOpen || '$$$',
blockClose = options.blockClose || '$$$';
var inlineRenderer = options.inlineRenderer ?
function(tokens, idx) {
return options.inlineRenderer(tokens[idx].content, tokens[idx]);
} :
makeMathRenderer(options.renderingOptions);
var blockRenderer = options.blockRenderer ?
function(tokens, idx) {
return options.blockRenderer(tokens[idx].content, tokens[idx]) + '\n';
} :
makeMathRenderer(Object.assign({ display: 'block' },
options.renderingOptions));
var math_inline = makeMath_inline(inlineOpen, inlineClose);
var math_block = makeMath_block(blockOpen, blockClose);
md.inline.ruler.before('escape', 'math_inline', math_inline);
md.block.ruler.after('blockquote', 'math_block', math_block, {
alt: [ 'paragraph', 'reference', 'blockquote', 'list' ]
});
md.renderer.rules.math_inline = inlineRenderer;
md.renderer.rules.math_block = blockRenderer;
};