-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
mathjax-editing_writing.js
375 lines (346 loc) · 13.3 KB
/
mathjax-editing_writing.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Comes from: http://dev.stackoverflow.com/content/js/mathjax-editing.js (MIT-License)
// Version downloaded 2016-11-21
//
// Two things modified:
//
// - StackExchange.mathjaxEditing = (function () {
// + function mjpd() { this.mathjaxEditing = (function () {
// - converterObject.hooks.chain("preSafe", replaceMath);
// + converterObject.hooks.chain("postConversion", replaceMath);
// - return { prepareWmdForMathJax: prepareWmdForMathJax };})();
// + return { prepareWmdForMathJax: prepareWmdForMathJax } })(); }
"use strict";
function mjpd() {
this.mathjaxEditing = (function () {
var ready = false; // true after initial typeset is complete
var pending = null; // non-null when typesetting has been queued
var inline = "$"; // the inline math delimiter
var blocks, start, end, last, braces, indent; // used in searching for math
var math; // stores math until markdone is done
var HUB = MathJax.Hub, TEX, NOERRORS;
//
// Runs after initial typeset
//
HUB.Queue(function () {
TEX = MathJax.InputJax.TeX;
NOERRORS = TEX.config.noErrors;
ready = true;
HUB.processUpdateTime = 50; // reduce update time so that we can cancel easier
HUB.processSectionDelay = 0; // don't pause between input and output phases
MathJax.Extension["fast-preview"].Disable(); // disable fast-preview
HUB.Config({
// reduce chunk for more frequent updates
"HTML-CSS": {
EqnChunk: 10,
EqnChunkFactor: 1
},
CommonHTML: {
EqnChunk: 10,
EqnChunkFactor: 1
},
SVG: {
EqnChunk: 10,
EqnChunkFactor: 1
}
});
if (pending) return RestartMJ(pending, "Typeset");
});
//
// These get called before and after typsetting
//
function preTypeset() {
NOERRORS.disabled = true; // disable noErrors (error will be shown)
TEX.resetEquationNumbers(); // reset labels
}
function postTypeset() {
NOERRORS.disabled = false; // don't show errors when not editing
}
//
// The pattern for math delimiters and special symbols
// needed for searching for math in the page.
//
var SPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[\\{}$]|[{}]|(?:\n\s*)+|@@\d+@@|`+)/i;
//
// The math is in blocks i through j, so
// collect it into one block and clear the others.
// Replace &, <, and > by named entities.
// For IE, put <br> at the ends of comments since IE removes \n.
// Clear the current math positions and store the index of the
// math, then push the math string onto the storage array.
//
function processMath(i, j) {
var block = blocks.slice(i, j + 1).join("")
.replace(/&/g, "&") // use HTML entity for &
.replace(/</g, "<") // use HTML entity for <
.replace(/>/g, ">") // use HTML entity for >
;
if (indent) block = block.replace(/\n /g, "\n");
if (HUB.Browser.isMSIE) {
block = block.replace(/(%[^\n]*)\n/g, "$1<br/>\n");
}
while (j > i) blocks[j--] = "";
blocks[i] = "@@" + math.length + "@@";
math.push(block);
start = end = last = null;
}
var capturingStringSplit;
if ("aba".split(/(b)/).length === 3) {
capturingStringSplit = function (str, regex) { return str.split(regex); };
}
else { // IE8
capturingStringSplit = function (str, regex) {
var result = [], match;
if (!regex.global) {
var source = regex.toString(),
flags = "";
source = source.replace(/^\/(.*)\/([im]*)$/, function (wholematch, re, fl) { flags = fl; return re; });
regex = new RegExp(source, flags + "g");
}
regex.lastIndex = 0;
var lastPos = 0;
while ((match = regex.exec(str))) {
result.push(str.substring(lastPos, match.index));
result.push.apply(result, match.slice(1));
lastPos = match.index + match[0].length;
}
result.push(str.substring(lastPos));
return result;
};
}
//
// Break up the text into its component parts and search
// through them for math delimiters, braces, linebreaks, etc.
// Math delimiters must match and braces must balance.
// Don't allow math to pass through a double linebreak
// (which will be a paragraph).
// Handle backticks (don't do math inside them)
//
function removeMath(text) {
start = end = last = indent = null; // for tracking math delimiters
math = []; // stores math strings for latter
blocks = capturingStringSplit(text.replace(/\r\n?/g, "\n"), SPLIT);
for (var i = 1, m = blocks.length; i < m; i += 2) {
var block = blocks[i];
if (block.charAt(0) === "@") {
//
// Things that look like our math markers will get
// stored and then retrieved along with the math.
//
blocks[i] = "@@" + math.length + "@@";
math.push(block);
}
else if (start) {
//
// If we are in math or backticks,
// look for the end delimiter,
// but don't go past double line breaks,
// and balance braces within the math,
// but don't process math inside backticks.
//
if (block === end) {
if (braces > 0) {
last = i;
}
else if (braces === 0) {
processMath(start, i);
}
else {
start = end = last = null;
}
}
else if (block.match(/\n.*\n/) || i + 2 >= m) {
if (last) {
i = last;
if (braces >= 0) processMath(start, i);
}
start = end = last = null;
braces = 0;
}
else if (block === "{" && braces >= 0) {
braces++;
}
else if (block === "}" && braces > 0) {
braces--;
}
}
else {
//
// Look for math start delimiters and when
// found, set up the end delimiter.
//
if (block === inline || block === "$$") {
start = i;
end = block;
braces = 0;
}
else if (block.substr(1, 5) === "begin") {
start = i;
end = "\\end" + block.substr(6);
braces = 0;
}
else if (block.charAt(0) === "`") {
start = last = i;
end = block;
braces = -1; // no brace balancing
}
else if (block.charAt(0) === "\n") {
if (block.match(/ $/)) indent = true;
}
}
}
if (last) processMath(start, last);
return blocks.join("");
}
//
// Put back the math strings that were saved,
// and clear the math array (no need to keep it around).
//
function replaceMath(text) {
text = text.replace(/@@(\d+)@@/g, function (match, n) {
return math[n];
});
math = null;
return text;
}
//
// This is run to restart MathJax after it has finished
// the previous run (that may have been canceled)
//
function RestartMJ(preview, method) {
pending = false;
HUB.cancelTypeset = false; // won't need to do this in the future
HUB.Queue(
preTypeset,
[method, HUB, preview],
postTypeset
);
}
//
// When the preview changes, cancel MathJax and restart,
// if we haven't done that already.
//
function UpdateMJ(preview, method) {
if (!pending) {
pending = preview;
if (ready) {
HUB.Cancel();
HUB.Queue([RestartMJ, preview, method]);
}
}
}
//
// Save the preview ID and the inline math delimiter.
// Create a converter for the editor and register a preConversion hook
// to handle escaping the math.
// Create a preview refresh hook to handle starting MathJax.
// Check if any errors are being displayed (in case there were
// errors in the initial display, which doesn't go through
// onPreviewRefresh), and reprocess if there are.
//
function prepareWmdForMathJax(editorObject, wmdId, delimiters) {
var preview = document.getElementById("wmd-preview" + wmdId);
inline = delimiters[0][0];
var converterObject = editorObject.getConverter();
converterObject.hooks.chain("preConversion", removeMath);
converterObject.hooks.chain("postConversion", replaceMath);
editorObject.hooks.chain("onPreviewRefresh", function () {
UpdateMJ(preview, "Typeset");
});
HUB.Queue(function () {
if (preview && preview.querySelector(".mjx-noError")) {
RestartMJ(preview, "Reprocess");
}
});
}
return {
prepareWmdForMathJax: prepareWmdForMathJax
}
})();
}
//
// Set up MathJax to allow canceling of typesetting, if it
// doesn't already have that.
//
(function () {
var HUB = MathJax.Hub;
if (!HUB.Cancel) {
HUB.cancelTypeset = false;
var CANCELMESSAGE = "MathJax Canceled";
HUB.Register.StartupHook("HTML-CSS Jax Config", function () {
var HTMLCSS = MathJax.OutputJax["HTML-CSS"],
TRANSLATE = HTMLCSS.Translate;
HTMLCSS.Augment({
Translate: function (script, state) {
if (HUB.cancelTypeset || state.cancelled) {
throw Error(CANCELMESSAGE)
}
return TRANSLATE.call(HTMLCSS, script, state);
}
});
});
HUB.Register.StartupHook("SVG Jax Config", function () {
var SVG = MathJax.OutputJax["SVG"],
TRANSLATE = SVG.Translate;
SVG.Augment({
Translate: function (script, state) {
if (HUB.cancelTypeset || state.cancelled) {
throw Error(CANCELMESSAGE)
}
return TRANSLATE.call(SVG, script, state);
}
});
});
HUB.Register.StartupHook("CommonHTML Jax Config", function () {
var CHTML = MathJax.OutputJax.CommonHTML,
TRANSLATE = CHTML.Translate;
CHTML.Augment({
Translate: function (script, state) {
if (HUB.cancelTypeset || state.cancelled) {
throw Error(CANCELMESSAGE);
}
return TRANSLATE.call(CHTML, script, state);
}
});
});
HUB.Register.StartupHook("PreviewHTML Jax Config", function () {
var PHTML = MathJax.OutputJax.PreviewHTML,
TRANSLATE = PHTML.Translate;
PHTML.Augment({
Translate: function (script, state) {
if (HUB.cancelTypeset || state.cancelled) {
throw Error(CANCELMESSAGE);
}
return TRANSLATE.call(PHTML, script, state);
}
});
});
HUB.Register.StartupHook("TeX Jax Config", function () {
var TEX = MathJax.InputJax.TeX,
TRANSLATE = TEX.Translate;
TEX.Augment({
Translate: function (script, state) {
if (HUB.cancelTypeset || state.cancelled) {
throw Error(CANCELMESSAGE)
}
return TRANSLATE.call(TEX, script, state);
}
});
});
var PROCESSERROR = HUB.processError;
HUB.processError = function (error, state, type) {
if (error.message !== CANCELMESSAGE) {
return PROCESSERROR.call(HUB, error, state, type)
}
MathJax.Message.Clear(0, 0);
state.jaxIDs = [];
state.jax = {};
state.scripts = [];
state.i = state.j = 0;
state.cancelled = true;
return null;
};
HUB.Cancel = function () {
this.cancelTypeset = true;
};
}
})();