katex 0.10.0-rc → 0.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +214 -126
- package/README.md +18 -17
- package/cli.js +5 -1
- package/contrib/auto-render/README.md +1 -1
- package/contrib/auto-render/auto-render.js +4 -1
- package/contrib/auto-render/test/auto-render-spec.js +17 -0
- package/contrib/copy-tex/README.md +8 -2
- package/contrib/copy-tex/copy-tex.js +0 -1
- package/contrib/copy-tex/copy-tex.webpack.js +6 -0
- package/contrib/mathtex-script-type/README.md +10 -6
- package/contrib/mhchem/README.md +19 -0
- package/contrib/mhchem/mhchem.js +1695 -0
- package/contrib/mhchem/mhchem.patch +235 -0
- package/dist/README.md +18 -17
- package/dist/contrib/auto-render.js +179 -161
- package/dist/contrib/auto-render.min.js +1 -1
- package/dist/contrib/auto-render.mjs +215 -0
- package/dist/contrib/copy-tex.js +84 -62
- package/dist/contrib/copy-tex.min.css +1 -1
- package/dist/contrib/copy-tex.min.js +1 -1
- package/dist/contrib/copy-tex.mjs +85 -0
- package/dist/contrib/mathtex-script-type.js +17 -14
- package/dist/contrib/mathtex-script-type.mjs +24 -0
- package/dist/contrib/mhchem.js +3241 -0
- package/dist/contrib/mhchem.min.js +1 -0
- package/dist/contrib/mhchem.mjs +3109 -0
- package/dist/fonts/KaTeX_AMS-Regular.ttf +0 -0
- package/dist/fonts/KaTeX_AMS-Regular.woff +0 -0
- package/dist/fonts/KaTeX_AMS-Regular.woff2 +0 -0
- package/dist/fonts/KaTeX_Caligraphic-Bold.ttf +0 -0
- package/dist/fonts/KaTeX_Caligraphic-Bold.woff +0 -0
- package/dist/fonts/KaTeX_Caligraphic-Bold.woff2 +0 -0
- package/dist/fonts/KaTeX_Caligraphic-Regular.ttf +0 -0
- package/dist/fonts/KaTeX_Caligraphic-Regular.woff +0 -0
- package/dist/fonts/KaTeX_Caligraphic-Regular.woff2 +0 -0
- package/dist/fonts/KaTeX_Fraktur-Bold.ttf +0 -0
- package/dist/fonts/KaTeX_Fraktur-Bold.woff +0 -0
- package/dist/fonts/KaTeX_Fraktur-Bold.woff2 +0 -0
- package/dist/fonts/KaTeX_Fraktur-Regular.ttf +0 -0
- package/dist/fonts/KaTeX_Fraktur-Regular.woff +0 -0
- package/dist/fonts/KaTeX_Fraktur-Regular.woff2 +0 -0
- package/dist/fonts/KaTeX_Main-Bold.ttf +0 -0
- package/dist/fonts/KaTeX_Main-Bold.woff +0 -0
- package/dist/fonts/KaTeX_Main-Bold.woff2 +0 -0
- package/dist/fonts/KaTeX_Main-BoldItalic.ttf +0 -0
- package/dist/fonts/KaTeX_Main-BoldItalic.woff +0 -0
- package/dist/fonts/KaTeX_Main-BoldItalic.woff2 +0 -0
- package/dist/fonts/KaTeX_Main-Italic.ttf +0 -0
- package/dist/fonts/KaTeX_Main-Italic.woff +0 -0
- package/dist/fonts/KaTeX_Main-Italic.woff2 +0 -0
- package/dist/fonts/KaTeX_Main-Regular.ttf +0 -0
- package/dist/fonts/KaTeX_Main-Regular.woff +0 -0
- package/dist/fonts/KaTeX_Main-Regular.woff2 +0 -0
- package/dist/fonts/KaTeX_Math-BoldItalic.ttf +0 -0
- package/dist/fonts/KaTeX_Math-BoldItalic.woff +0 -0
- package/dist/fonts/KaTeX_Math-BoldItalic.woff2 +0 -0
- package/dist/fonts/KaTeX_Math-Italic.ttf +0 -0
- package/dist/fonts/KaTeX_Math-Italic.woff +0 -0
- package/dist/fonts/KaTeX_Math-Italic.woff2 +0 -0
- package/dist/fonts/KaTeX_SansSerif-Bold.ttf +0 -0
- package/dist/fonts/KaTeX_SansSerif-Bold.woff +0 -0
- package/dist/fonts/KaTeX_SansSerif-Bold.woff2 +0 -0
- package/dist/fonts/KaTeX_SansSerif-Italic.ttf +0 -0
- package/dist/fonts/KaTeX_SansSerif-Italic.woff +0 -0
- package/dist/fonts/KaTeX_SansSerif-Italic.woff2 +0 -0
- package/dist/fonts/KaTeX_SansSerif-Regular.ttf +0 -0
- package/dist/fonts/KaTeX_SansSerif-Regular.woff +0 -0
- package/dist/fonts/KaTeX_SansSerif-Regular.woff2 +0 -0
- package/dist/fonts/KaTeX_Script-Regular.ttf +0 -0
- package/dist/fonts/KaTeX_Script-Regular.woff +0 -0
- package/dist/fonts/KaTeX_Script-Regular.woff2 +0 -0
- package/dist/fonts/KaTeX_Size1-Regular.ttf +0 -0
- package/dist/fonts/KaTeX_Size1-Regular.woff +0 -0
- package/dist/fonts/KaTeX_Size1-Regular.woff2 +0 -0
- package/dist/fonts/KaTeX_Size2-Regular.ttf +0 -0
- package/dist/fonts/KaTeX_Size2-Regular.woff +0 -0
- package/dist/fonts/KaTeX_Size2-Regular.woff2 +0 -0
- package/dist/fonts/KaTeX_Size3-Regular.ttf +0 -0
- package/dist/fonts/KaTeX_Size3-Regular.woff +0 -0
- package/dist/fonts/KaTeX_Size3-Regular.woff2 +0 -0
- package/dist/fonts/KaTeX_Size4-Regular.ttf +0 -0
- package/dist/fonts/KaTeX_Size4-Regular.woff +0 -0
- package/dist/fonts/KaTeX_Size4-Regular.woff2 +0 -0
- package/dist/fonts/KaTeX_Typewriter-Regular.ttf +0 -0
- package/dist/fonts/KaTeX_Typewriter-Regular.woff +0 -0
- package/dist/fonts/KaTeX_Typewriter-Regular.woff2 +0 -0
- package/dist/katex.css +24 -9
- package/dist/katex.js +13295 -12413
- package/dist/katex.min.css +1 -1
- package/dist/katex.min.js +1 -1
- package/dist/katex.mjs +13388 -11826
- package/katex.js +1 -2
- package/package.json +60 -48
- package/src/Lexer.js +25 -25
- package/src/MacroExpander.js +0 -1
- package/src/Options.js +11 -75
- package/src/Parser.js +231 -313
- package/src/Settings.js +6 -0
- package/src/buildCommon.js +140 -103
- package/src/buildHTML.js +125 -121
- package/src/buildMathML.js +14 -4
- package/src/buildTree.js +16 -10
- package/src/delimiter.js +4 -3
- package/src/domTree.js +91 -44
- package/src/environments/array.js +120 -7
- package/src/fontMetrics.js +3 -2
- package/src/functions/arrow.js +21 -7
- package/src/functions/color.js +2 -37
- package/src/functions/delimsizing.js +18 -11
- package/src/functions/enclose.js +19 -4
- package/src/functions/environment.js +35 -4
- package/src/functions/font.js +1 -2
- package/src/functions/genfrac.js +35 -20
- package/src/functions/href.js +5 -3
- package/src/functions/includegraphics.js +146 -0
- package/src/functions/mclass.js +1 -0
- package/src/functions/op.js +21 -32
- package/src/functions/operatorname.js +1 -2
- package/src/functions/ordgroup.js +4 -0
- package/src/functions/phantom.js +7 -3
- package/src/functions/rule.js +20 -9
- package/src/functions/sizing.js +2 -4
- package/src/functions/smash.js +5 -2
- package/src/functions/sqrt.js +1 -4
- package/src/functions/styling.js +0 -1
- package/src/functions/supsub.js +6 -2
- package/src/functions/symbolsOp.js +4 -0
- package/src/functions/symbolsSpacing.js +29 -6
- package/src/functions/tag.js +20 -4
- package/src/functions/text.js +6 -4
- package/src/functions/verb.js +16 -4
- package/src/functions.js +2 -0
- package/src/katex.less +35 -12
- package/src/macros.js +161 -36
- package/src/mathMLTree.js +17 -19
- package/src/parseNode.js +27 -1
- package/src/stretchy.js +3 -1
- package/src/svgGeometry.js +1 -1
- package/src/symbols.js +39 -17
- package/src/tree.js +0 -4
- package/src/types.js +4 -3
- package/src/unicodeMake.js +1 -1
- package/src/utils.js +1 -62
- package/src/wide-character.js +2 -2
|
@@ -114,95 +114,93 @@ var external_katex_default = /*#__PURE__*/__webpack_require__.n(external_katex_)
|
|
|
114
114
|
// CONCATENATED MODULE: ./contrib/auto-render/splitAtDelimiters.js
|
|
115
115
|
/* eslint no-constant-condition:0 */
|
|
116
116
|
var findEndOfMath = function findEndOfMath(delimiter, text, startIndex) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
braceLevel--;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
index++;
|
|
117
|
+
// Adapted from
|
|
118
|
+
// https://github.com/Khan/perseus/blob/master/src/perseus-markdown.jsx
|
|
119
|
+
var index = startIndex;
|
|
120
|
+
var braceLevel = 0;
|
|
121
|
+
var delimLength = delimiter.length;
|
|
122
|
+
|
|
123
|
+
while (index < text.length) {
|
|
124
|
+
var character = text[index];
|
|
125
|
+
|
|
126
|
+
if (braceLevel <= 0 && text.slice(index, index + delimLength) === delimiter) {
|
|
127
|
+
return index;
|
|
128
|
+
} else if (character === "\\") {
|
|
129
|
+
index++;
|
|
130
|
+
} else if (character === "{") {
|
|
131
|
+
braceLevel++;
|
|
132
|
+
} else if (character === "}") {
|
|
133
|
+
braceLevel--;
|
|
138
134
|
}
|
|
139
135
|
|
|
140
|
-
|
|
136
|
+
index++;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return -1;
|
|
141
140
|
};
|
|
142
141
|
|
|
143
142
|
var splitAtDelimiters = function splitAtDelimiters(startData, leftDelim, rightDelim, display) {
|
|
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
|
-
currIndex = nextIndex;
|
|
177
|
-
} else {
|
|
178
|
-
nextIndex = findEndOfMath(rightDelim, text, currIndex + leftDelim.length);
|
|
179
|
-
if (nextIndex === -1) {
|
|
180
|
-
break;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
finalData.push({
|
|
184
|
-
type: "math",
|
|
185
|
-
data: text.slice(currIndex + leftDelim.length, nextIndex),
|
|
186
|
-
rawData: text.slice(currIndex, nextIndex + rightDelim.length),
|
|
187
|
-
display: display
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
currIndex = nextIndex + rightDelim.length;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
lookingForLeft = !lookingForLeft;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
finalData.push({
|
|
197
|
-
type: "text",
|
|
198
|
-
data: text.slice(currIndex)
|
|
199
|
-
});
|
|
143
|
+
var finalData = [];
|
|
144
|
+
|
|
145
|
+
for (var i = 0; i < startData.length; i++) {
|
|
146
|
+
if (startData[i].type === "text") {
|
|
147
|
+
var text = startData[i].data;
|
|
148
|
+
var lookingForLeft = true;
|
|
149
|
+
var currIndex = 0;
|
|
150
|
+
var nextIndex = void 0;
|
|
151
|
+
nextIndex = text.indexOf(leftDelim);
|
|
152
|
+
|
|
153
|
+
if (nextIndex !== -1) {
|
|
154
|
+
currIndex = nextIndex;
|
|
155
|
+
finalData.push({
|
|
156
|
+
type: "text",
|
|
157
|
+
data: text.slice(0, currIndex)
|
|
158
|
+
});
|
|
159
|
+
lookingForLeft = false;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
while (true) {
|
|
163
|
+
if (lookingForLeft) {
|
|
164
|
+
nextIndex = text.indexOf(leftDelim, currIndex);
|
|
165
|
+
|
|
166
|
+
if (nextIndex === -1) {
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
finalData.push({
|
|
171
|
+
type: "text",
|
|
172
|
+
data: text.slice(currIndex, nextIndex)
|
|
173
|
+
});
|
|
174
|
+
currIndex = nextIndex;
|
|
200
175
|
} else {
|
|
201
|
-
|
|
176
|
+
nextIndex = findEndOfMath(rightDelim, text, currIndex + leftDelim.length);
|
|
177
|
+
|
|
178
|
+
if (nextIndex === -1) {
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
finalData.push({
|
|
183
|
+
type: "math",
|
|
184
|
+
data: text.slice(currIndex + leftDelim.length, nextIndex),
|
|
185
|
+
rawData: text.slice(currIndex, nextIndex + rightDelim.length),
|
|
186
|
+
display: display
|
|
187
|
+
});
|
|
188
|
+
currIndex = nextIndex + rightDelim.length;
|
|
202
189
|
}
|
|
190
|
+
|
|
191
|
+
lookingForLeft = !lookingForLeft;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
finalData.push({
|
|
195
|
+
type: "text",
|
|
196
|
+
data: text.slice(currIndex)
|
|
197
|
+
});
|
|
198
|
+
} else {
|
|
199
|
+
finalData.push(startData[i]);
|
|
203
200
|
}
|
|
201
|
+
}
|
|
204
202
|
|
|
205
|
-
|
|
203
|
+
return finalData;
|
|
206
204
|
};
|
|
207
205
|
|
|
208
206
|
/* harmony default export */ var auto_render_splitAtDelimiters = (splitAtDelimiters);
|
|
@@ -211,107 +209,127 @@ var splitAtDelimiters = function splitAtDelimiters(startData, leftDelim, rightDe
|
|
|
211
209
|
|
|
212
210
|
|
|
213
211
|
|
|
214
|
-
|
|
215
212
|
var auto_render_splitWithDelimiters = function splitWithDelimiters(text, delimiters) {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
213
|
+
var data = [{
|
|
214
|
+
type: "text",
|
|
215
|
+
data: text
|
|
216
|
+
}];
|
|
217
|
+
|
|
218
|
+
for (var i = 0; i < delimiters.length; i++) {
|
|
219
|
+
var delimiter = delimiters[i];
|
|
220
|
+
data = auto_render_splitAtDelimiters(data, delimiter.left, delimiter.right, delimiter.display || false);
|
|
221
|
+
}
|
|
223
222
|
|
|
223
|
+
return data;
|
|
224
|
+
};
|
|
224
225
|
/* Note: optionsCopy is mutated by this method. If it is ever exposed in the
|
|
225
226
|
* API, we should copy it before mutating.
|
|
226
227
|
*/
|
|
228
|
+
|
|
229
|
+
|
|
227
230
|
var auto_render_renderMathInText = function renderMathInText(text, optionsCopy) {
|
|
228
|
-
|
|
229
|
-
|
|
231
|
+
var data = auto_render_splitWithDelimiters(text, optionsCopy.delimiters);
|
|
232
|
+
var fragment = document.createDocumentFragment();
|
|
233
|
+
|
|
234
|
+
for (var i = 0; i < data.length; i++) {
|
|
235
|
+
if (data[i].type === "text") {
|
|
236
|
+
fragment.appendChild(document.createTextNode(data[i].data));
|
|
237
|
+
} else {
|
|
238
|
+
var span = document.createElement("span");
|
|
239
|
+
var math = data[i].data; // Override any display mode defined in the settings with that
|
|
240
|
+
// defined by the text itself
|
|
241
|
+
|
|
242
|
+
optionsCopy.displayMode = data[i].display;
|
|
243
|
+
|
|
244
|
+
try {
|
|
245
|
+
if (optionsCopy.preProcess) {
|
|
246
|
+
math = optionsCopy.preProcess(math);
|
|
247
|
+
}
|
|
230
248
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
var span = document.createElement("span");
|
|
236
|
-
var math = data[i].data;
|
|
237
|
-
// Override any display mode defined in the settings with that
|
|
238
|
-
// defined by the text itself
|
|
239
|
-
optionsCopy.displayMode = data[i].display;
|
|
240
|
-
try {
|
|
241
|
-
external_katex_default.a.render(math, span, optionsCopy);
|
|
242
|
-
} catch (e) {
|
|
243
|
-
if (!(e instanceof external_katex_default.a.ParseError)) {
|
|
244
|
-
throw e;
|
|
245
|
-
}
|
|
246
|
-
optionsCopy.errorCallback("KaTeX auto-render: Failed to parse `" + data[i].data + "` with ", e);
|
|
247
|
-
fragment.appendChild(document.createTextNode(data[i].rawData));
|
|
248
|
-
continue;
|
|
249
|
-
}
|
|
250
|
-
fragment.appendChild(span);
|
|
249
|
+
external_katex_default.a.render(math, span, optionsCopy);
|
|
250
|
+
} catch (e) {
|
|
251
|
+
if (!(e instanceof external_katex_default.a.ParseError)) {
|
|
252
|
+
throw e;
|
|
251
253
|
}
|
|
254
|
+
|
|
255
|
+
optionsCopy.errorCallback("KaTeX auto-render: Failed to parse `" + data[i].data + "` with ", e);
|
|
256
|
+
fragment.appendChild(document.createTextNode(data[i].rawData));
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
fragment.appendChild(span);
|
|
252
261
|
}
|
|
262
|
+
}
|
|
253
263
|
|
|
254
|
-
|
|
264
|
+
return fragment;
|
|
255
265
|
};
|
|
256
266
|
|
|
257
267
|
var renderElem = function renderElem(elem, optionsCopy) {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
})();
|
|
268
|
+
for (var i = 0; i < elem.childNodes.length; i++) {
|
|
269
|
+
var childNode = elem.childNodes[i];
|
|
270
|
+
|
|
271
|
+
if (childNode.nodeType === 3) {
|
|
272
|
+
// Text node
|
|
273
|
+
var frag = auto_render_renderMathInText(childNode.textContent, optionsCopy);
|
|
274
|
+
i += frag.childNodes.length - 1;
|
|
275
|
+
elem.replaceChild(frag, childNode);
|
|
276
|
+
} else if (childNode.nodeType === 1) {
|
|
277
|
+
(function () {
|
|
278
|
+
// Element node
|
|
279
|
+
var className = ' ' + childNode.className + ' ';
|
|
280
|
+
var shouldRender = optionsCopy.ignoredTags.indexOf(childNode.nodeName.toLowerCase()) === -1 && optionsCopy.ignoredClasses.every(function (x) {
|
|
281
|
+
return className.indexOf(' ' + x + ' ') === -1;
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
if (shouldRender) {
|
|
285
|
+
renderElem(childNode, optionsCopy);
|
|
277
286
|
}
|
|
278
|
-
|
|
279
|
-
}
|
|
287
|
+
})();
|
|
288
|
+
} // Otherwise, it's something else, and ignore it.
|
|
289
|
+
|
|
290
|
+
}
|
|
280
291
|
};
|
|
281
292
|
|
|
282
293
|
var renderMathInElement = function renderMathInElement(elem, options) {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
294
|
+
if (!elem) {
|
|
295
|
+
throw new Error("No element provided to render");
|
|
296
|
+
}
|
|
286
297
|
|
|
287
|
-
|
|
298
|
+
var optionsCopy = {}; // Object.assign(optionsCopy, option)
|
|
288
299
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
optionsCopy[option] = options[option];
|
|
293
|
-
}
|
|
300
|
+
for (var option in options) {
|
|
301
|
+
if (options.hasOwnProperty(option)) {
|
|
302
|
+
optionsCopy[option] = options[option];
|
|
294
303
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
304
|
+
} // default options
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
optionsCopy.delimiters = optionsCopy.delimiters || [{
|
|
308
|
+
left: "$$",
|
|
309
|
+
right: "$$",
|
|
310
|
+
display: true
|
|
311
|
+
}, {
|
|
312
|
+
left: "\\(",
|
|
313
|
+
right: "\\)",
|
|
314
|
+
display: false
|
|
315
|
+
}, // LaTeX uses $…$, but it ruins the display of normal `$` in text:
|
|
316
|
+
// {left: "$", right: "$", display: false},
|
|
317
|
+
// \[…\] must come last in this array. Otherwise, renderMathInElement
|
|
318
|
+
// will search for \[ before it searches for $$ or \(
|
|
319
|
+
// That makes it susceptible to finding a \\[0.3em] row delimiter and
|
|
320
|
+
// treating it as if it were the start of a KaTeX math zone.
|
|
321
|
+
{
|
|
322
|
+
left: "\\[",
|
|
323
|
+
right: "\\]",
|
|
324
|
+
display: true
|
|
325
|
+
}];
|
|
326
|
+
optionsCopy.ignoredTags = optionsCopy.ignoredTags || ["script", "noscript", "style", "textarea", "pre", "code"];
|
|
327
|
+
optionsCopy.ignoredClasses = optionsCopy.ignoredClasses || [];
|
|
328
|
+
optionsCopy.errorCallback = optionsCopy.errorCallback || console.error; // Enable sharing of global macros defined via `\gdef` between different
|
|
329
|
+
// math elements within a single call to `renderMathInElement`.
|
|
330
|
+
|
|
331
|
+
optionsCopy.macros = optionsCopy.macros || {};
|
|
332
|
+
renderElem(elem, optionsCopy);
|
|
315
333
|
};
|
|
316
334
|
|
|
317
335
|
/* harmony default export */ var auto_render = __webpack_exports__["default"] = (renderMathInElement);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("katex")):"function"==typeof define&&define.amd?define(["katex"],t):"object"==typeof exports?exports.renderMathInElement=t(require("katex")):e.renderMathInElement=t(e.katex)}("undefined"!=typeof self?self:this,function(e){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=1)}([function(t,r){t.exports=e},function(e,t,r){"use strict";r.r(t);var n=r(0),o=r.n(n),a=function(e,t,r){for(var n=r,o=0,a=e.length;n<t.length;){var i=t[n];if(o<=0&&t.slice(n,n+a)===e)return n;"\\"===i?n++:"{"===i?o++:"}"===i&&o--,n++}return-1},i=function(e,t,r,n){for(var o=[],i=0;i<e.length;i++)if("text"===e[i].type){var l=e[i].data,d=!0,
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("katex")):"function"==typeof define&&define.amd?define(["katex"],t):"object"==typeof exports?exports.renderMathInElement=t(require("katex")):e.renderMathInElement=t(e.katex)}("undefined"!=typeof self?self:this,function(e){return function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=1)}([function(t,r){t.exports=e},function(e,t,r){"use strict";r.r(t);var n=r(0),o=r.n(n),a=function(e,t,r){for(var n=r,o=0,a=e.length;n<t.length;){var i=t[n];if(o<=0&&t.slice(n,n+a)===e)return n;"\\"===i?n++:"{"===i?o++:"}"===i&&o--,n++}return-1},i=function(e,t,r,n){for(var o=[],i=0;i<e.length;i++)if("text"===e[i].type){var l=e[i].data,d=!0,s=0,f=void 0;for(-1!==(f=l.indexOf(t))&&(s=f,o.push({type:"text",data:l.slice(0,s)}),d=!1);;){if(d){if(-1===(f=l.indexOf(t,s)))break;o.push({type:"text",data:l.slice(s,f)}),s=f}else{if(-1===(f=a(r,l,s+t.length)))break;o.push({type:"math",data:l.slice(s+t.length,f),rawData:l.slice(s,f+r.length),display:n}),s=f+r.length}d=!d}o.push({type:"text",data:l.slice(s)})}else o.push(e[i]);return o},l=function(e,t){for(var r=function(e,t){for(var r=[{type:"text",data:e}],n=0;n<t.length;n++){var o=t[n];r=i(r,o.left,o.right,o.display||!1)}return r}(e,t.delimiters),n=document.createDocumentFragment(),a=0;a<r.length;a++)if("text"===r[a].type)n.appendChild(document.createTextNode(r[a].data));else{var l=document.createElement("span"),d=r[a].data;t.displayMode=r[a].display;try{t.preProcess&&(d=t.preProcess(d)),o.a.render(d,l,t)}catch(e){if(!(e instanceof o.a.ParseError))throw e;t.errorCallback("KaTeX auto-render: Failed to parse `"+r[a].data+"` with ",e),n.appendChild(document.createTextNode(r[a].rawData));continue}n.appendChild(l)}return n};t.default=function(e,t){if(!e)throw new Error("No element provided to render");var r={};for(var n in t)t.hasOwnProperty(n)&&(r[n]=t[n]);r.delimiters=r.delimiters||[{left:"$$",right:"$$",display:!0},{left:"\\(",right:"\\)",display:!1},{left:"\\[",right:"\\]",display:!0}],r.ignoredTags=r.ignoredTags||["script","noscript","style","textarea","pre","code"],r.ignoredClasses=r.ignoredClasses||[],r.errorCallback=r.errorCallback||console.error,r.macros=r.macros||{},function e(t,r){for(var n=0;n<t.childNodes.length;n++){var o=t.childNodes[n];if(3===o.nodeType){var a=l(o.textContent,r);n+=a.childNodes.length-1,t.replaceChild(a,o)}else 1===o.nodeType&&function(){var t=" "+o.className+" ";-1===r.ignoredTags.indexOf(o.nodeName.toLowerCase())&&r.ignoredClasses.every(function(e){return-1===t.indexOf(" "+e+" ")})&&e(o,r)}()}}(e,r)}}]).default});
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import katex from '../katex.mjs';
|
|
2
|
+
|
|
3
|
+
/* eslint no-constant-condition:0 */
|
|
4
|
+
const findEndOfMath = function findEndOfMath(delimiter, text, startIndex) {
|
|
5
|
+
// Adapted from
|
|
6
|
+
// https://github.com/Khan/perseus/blob/master/src/perseus-markdown.jsx
|
|
7
|
+
let index = startIndex;
|
|
8
|
+
let braceLevel = 0;
|
|
9
|
+
const delimLength = delimiter.length;
|
|
10
|
+
|
|
11
|
+
while (index < text.length) {
|
|
12
|
+
const character = text[index];
|
|
13
|
+
|
|
14
|
+
if (braceLevel <= 0 && text.slice(index, index + delimLength) === delimiter) {
|
|
15
|
+
return index;
|
|
16
|
+
} else if (character === "\\") {
|
|
17
|
+
index++;
|
|
18
|
+
} else if (character === "{") {
|
|
19
|
+
braceLevel++;
|
|
20
|
+
} else if (character === "}") {
|
|
21
|
+
braceLevel--;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
index++;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return -1;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const splitAtDelimiters = function splitAtDelimiters(startData, leftDelim, rightDelim, display) {
|
|
31
|
+
const finalData = [];
|
|
32
|
+
|
|
33
|
+
for (let i = 0; i < startData.length; i++) {
|
|
34
|
+
if (startData[i].type === "text") {
|
|
35
|
+
const text = startData[i].data;
|
|
36
|
+
let lookingForLeft = true;
|
|
37
|
+
let currIndex = 0;
|
|
38
|
+
let nextIndex;
|
|
39
|
+
nextIndex = text.indexOf(leftDelim);
|
|
40
|
+
|
|
41
|
+
if (nextIndex !== -1) {
|
|
42
|
+
currIndex = nextIndex;
|
|
43
|
+
finalData.push({
|
|
44
|
+
type: "text",
|
|
45
|
+
data: text.slice(0, currIndex)
|
|
46
|
+
});
|
|
47
|
+
lookingForLeft = false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
while (true) {
|
|
51
|
+
if (lookingForLeft) {
|
|
52
|
+
nextIndex = text.indexOf(leftDelim, currIndex);
|
|
53
|
+
|
|
54
|
+
if (nextIndex === -1) {
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
finalData.push({
|
|
59
|
+
type: "text",
|
|
60
|
+
data: text.slice(currIndex, nextIndex)
|
|
61
|
+
});
|
|
62
|
+
currIndex = nextIndex;
|
|
63
|
+
} else {
|
|
64
|
+
nextIndex = findEndOfMath(rightDelim, text, currIndex + leftDelim.length);
|
|
65
|
+
|
|
66
|
+
if (nextIndex === -1) {
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
finalData.push({
|
|
71
|
+
type: "math",
|
|
72
|
+
data: text.slice(currIndex + leftDelim.length, nextIndex),
|
|
73
|
+
rawData: text.slice(currIndex, nextIndex + rightDelim.length),
|
|
74
|
+
display: display
|
|
75
|
+
});
|
|
76
|
+
currIndex = nextIndex + rightDelim.length;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
lookingForLeft = !lookingForLeft;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
finalData.push({
|
|
83
|
+
type: "text",
|
|
84
|
+
data: text.slice(currIndex)
|
|
85
|
+
});
|
|
86
|
+
} else {
|
|
87
|
+
finalData.push(startData[i]);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return finalData;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/* eslint no-console:0 */
|
|
95
|
+
|
|
96
|
+
const splitWithDelimiters = function splitWithDelimiters(text, delimiters) {
|
|
97
|
+
let data = [{
|
|
98
|
+
type: "text",
|
|
99
|
+
data: text
|
|
100
|
+
}];
|
|
101
|
+
|
|
102
|
+
for (let i = 0; i < delimiters.length; i++) {
|
|
103
|
+
const delimiter = delimiters[i];
|
|
104
|
+
data = splitAtDelimiters(data, delimiter.left, delimiter.right, delimiter.display || false);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return data;
|
|
108
|
+
};
|
|
109
|
+
/* Note: optionsCopy is mutated by this method. If it is ever exposed in the
|
|
110
|
+
* API, we should copy it before mutating.
|
|
111
|
+
*/
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
const renderMathInText = function renderMathInText(text, optionsCopy) {
|
|
115
|
+
const data = splitWithDelimiters(text, optionsCopy.delimiters);
|
|
116
|
+
const fragment = document.createDocumentFragment();
|
|
117
|
+
|
|
118
|
+
for (let i = 0; i < data.length; i++) {
|
|
119
|
+
if (data[i].type === "text") {
|
|
120
|
+
fragment.appendChild(document.createTextNode(data[i].data));
|
|
121
|
+
} else {
|
|
122
|
+
const span = document.createElement("span");
|
|
123
|
+
let math = data[i].data; // Override any display mode defined in the settings with that
|
|
124
|
+
// defined by the text itself
|
|
125
|
+
|
|
126
|
+
optionsCopy.displayMode = data[i].display;
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
if (optionsCopy.preProcess) {
|
|
130
|
+
math = optionsCopy.preProcess(math);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
katex.render(math, span, optionsCopy);
|
|
134
|
+
} catch (e) {
|
|
135
|
+
if (!(e instanceof katex.ParseError)) {
|
|
136
|
+
throw e;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
optionsCopy.errorCallback("KaTeX auto-render: Failed to parse `" + data[i].data + "` with ", e);
|
|
140
|
+
fragment.appendChild(document.createTextNode(data[i].rawData));
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
fragment.appendChild(span);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return fragment;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const renderElem = function renderElem(elem, optionsCopy) {
|
|
152
|
+
for (let i = 0; i < elem.childNodes.length; i++) {
|
|
153
|
+
const childNode = elem.childNodes[i];
|
|
154
|
+
|
|
155
|
+
if (childNode.nodeType === 3) {
|
|
156
|
+
// Text node
|
|
157
|
+
const frag = renderMathInText(childNode.textContent, optionsCopy);
|
|
158
|
+
i += frag.childNodes.length - 1;
|
|
159
|
+
elem.replaceChild(frag, childNode);
|
|
160
|
+
} else if (childNode.nodeType === 1) {
|
|
161
|
+
// Element node
|
|
162
|
+
const className = ' ' + childNode.className + ' ';
|
|
163
|
+
const shouldRender = optionsCopy.ignoredTags.indexOf(childNode.nodeName.toLowerCase()) === -1 && optionsCopy.ignoredClasses.every(x => className.indexOf(' ' + x + ' ') === -1);
|
|
164
|
+
|
|
165
|
+
if (shouldRender) {
|
|
166
|
+
renderElem(childNode, optionsCopy);
|
|
167
|
+
}
|
|
168
|
+
} // Otherwise, it's something else, and ignore it.
|
|
169
|
+
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const renderMathInElement = function renderMathInElement(elem, options) {
|
|
174
|
+
if (!elem) {
|
|
175
|
+
throw new Error("No element provided to render");
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const optionsCopy = {}; // Object.assign(optionsCopy, option)
|
|
179
|
+
|
|
180
|
+
for (const option in options) {
|
|
181
|
+
if (options.hasOwnProperty(option)) {
|
|
182
|
+
optionsCopy[option] = options[option];
|
|
183
|
+
}
|
|
184
|
+
} // default options
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
optionsCopy.delimiters = optionsCopy.delimiters || [{
|
|
188
|
+
left: "$$",
|
|
189
|
+
right: "$$",
|
|
190
|
+
display: true
|
|
191
|
+
}, {
|
|
192
|
+
left: "\\(",
|
|
193
|
+
right: "\\)",
|
|
194
|
+
display: false
|
|
195
|
+
}, // LaTeX uses $…$, but it ruins the display of normal `$` in text:
|
|
196
|
+
// {left: "$", right: "$", display: false},
|
|
197
|
+
// \[…\] must come last in this array. Otherwise, renderMathInElement
|
|
198
|
+
// will search for \[ before it searches for $$ or \(
|
|
199
|
+
// That makes it susceptible to finding a \\[0.3em] row delimiter and
|
|
200
|
+
// treating it as if it were the start of a KaTeX math zone.
|
|
201
|
+
{
|
|
202
|
+
left: "\\[",
|
|
203
|
+
right: "\\]",
|
|
204
|
+
display: true
|
|
205
|
+
}];
|
|
206
|
+
optionsCopy.ignoredTags = optionsCopy.ignoredTags || ["script", "noscript", "style", "textarea", "pre", "code"];
|
|
207
|
+
optionsCopy.ignoredClasses = optionsCopy.ignoredClasses || [];
|
|
208
|
+
optionsCopy.errorCallback = optionsCopy.errorCallback || console.error; // Enable sharing of global macros defined via `\gdef` between different
|
|
209
|
+
// math elements within a single call to `renderMathInElement`.
|
|
210
|
+
|
|
211
|
+
optionsCopy.macros = optionsCopy.macros || {};
|
|
212
|
+
renderElem(elem, optionsCopy);
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
export default renderMathInElement;
|