comparison katex/contrib/auto-render.js @ 8:4a25b534c81c javascript-experiment

Add v8 engine and include katex
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 17 Jun 2020 21:43:52 +0200
parents
children
comparison
equal deleted inserted replaced
7:a5aa39557726 8:4a25b534c81c
1 (function webpackUniversalModuleDefinition(root, factory) {
2 if(typeof exports === 'object' && typeof module === 'object')
3 module.exports = factory(require("katex"));
4 else if(typeof define === 'function' && define.amd)
5 define(["katex"], factory);
6 else if(typeof exports === 'object')
7 exports["renderMathInElement"] = factory(require("katex"));
8 else
9 root["renderMathInElement"] = factory(root["katex"]);
10 })((typeof self !== 'undefined' ? self : this), function(__WEBPACK_EXTERNAL_MODULE__0__) {
11 return /******/ (function(modules) { // webpackBootstrap
12 /******/ // The module cache
13 /******/ var installedModules = {};
14 /******/
15 /******/ // The require function
16 /******/ function __webpack_require__(moduleId) {
17 /******/
18 /******/ // Check if module is in cache
19 /******/ if(installedModules[moduleId]) {
20 /******/ return installedModules[moduleId].exports;
21 /******/ }
22 /******/ // Create a new module (and put it into the cache)
23 /******/ var module = installedModules[moduleId] = {
24 /******/ i: moduleId,
25 /******/ l: false,
26 /******/ exports: {}
27 /******/ };
28 /******/
29 /******/ // Execute the module function
30 /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
31 /******/
32 /******/ // Flag the module as loaded
33 /******/ module.l = true;
34 /******/
35 /******/ // Return the exports of the module
36 /******/ return module.exports;
37 /******/ }
38 /******/
39 /******/
40 /******/ // expose the modules object (__webpack_modules__)
41 /******/ __webpack_require__.m = modules;
42 /******/
43 /******/ // expose the module cache
44 /******/ __webpack_require__.c = installedModules;
45 /******/
46 /******/ // define getter function for harmony exports
47 /******/ __webpack_require__.d = function(exports, name, getter) {
48 /******/ if(!__webpack_require__.o(exports, name)) {
49 /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
50 /******/ }
51 /******/ };
52 /******/
53 /******/ // define __esModule on exports
54 /******/ __webpack_require__.r = function(exports) {
55 /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
56 /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
57 /******/ }
58 /******/ Object.defineProperty(exports, '__esModule', { value: true });
59 /******/ };
60 /******/
61 /******/ // create a fake namespace object
62 /******/ // mode & 1: value is a module id, require it
63 /******/ // mode & 2: merge all properties of value into the ns
64 /******/ // mode & 4: return value when already ns object
65 /******/ // mode & 8|1: behave like require
66 /******/ __webpack_require__.t = function(value, mode) {
67 /******/ if(mode & 1) value = __webpack_require__(value);
68 /******/ if(mode & 8) return value;
69 /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
70 /******/ var ns = Object.create(null);
71 /******/ __webpack_require__.r(ns);
72 /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
73 /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
74 /******/ return ns;
75 /******/ };
76 /******/
77 /******/ // getDefaultExport function for compatibility with non-harmony modules
78 /******/ __webpack_require__.n = function(module) {
79 /******/ var getter = module && module.__esModule ?
80 /******/ function getDefault() { return module['default']; } :
81 /******/ function getModuleExports() { return module; };
82 /******/ __webpack_require__.d(getter, 'a', getter);
83 /******/ return getter;
84 /******/ };
85 /******/
86 /******/ // Object.prototype.hasOwnProperty.call
87 /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
88 /******/
89 /******/ // __webpack_public_path__
90 /******/ __webpack_require__.p = "";
91 /******/
92 /******/
93 /******/ // Load entry module and return exports
94 /******/ return __webpack_require__(__webpack_require__.s = 1);
95 /******/ })
96 /************************************************************************/
97 /******/ ([
98 /* 0 */
99 /***/ (function(module, exports) {
100
101 module.exports = __WEBPACK_EXTERNAL_MODULE__0__;
102
103 /***/ }),
104 /* 1 */
105 /***/ (function(module, __webpack_exports__, __webpack_require__) {
106
107 "use strict";
108 __webpack_require__.r(__webpack_exports__);
109
110 // EXTERNAL MODULE: external "katex"
111 var external_katex_ = __webpack_require__(0);
112 var external_katex_default = /*#__PURE__*/__webpack_require__.n(external_katex_);
113
114 // CONCATENATED MODULE: ./contrib/auto-render/splitAtDelimiters.js
115 /* eslint no-constant-condition:0 */
116 var findEndOfMath = function findEndOfMath(delimiter, text, startIndex) {
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--;
134 }
135
136 index++;
137 }
138
139 return -1;
140 };
141
142 var splitAtDelimiters = function splitAtDelimiters(startData, leftDelim, rightDelim, display) {
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;
175 } else {
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;
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]);
200 }
201 }
202
203 return finalData;
204 };
205
206 /* harmony default export */ var auto_render_splitAtDelimiters = (splitAtDelimiters);
207 // CONCATENATED MODULE: ./contrib/auto-render/auto-render.js
208 /* eslint no-console:0 */
209
210
211
212 var auto_render_splitWithDelimiters = function splitWithDelimiters(text, delimiters) {
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 }
222
223 return data;
224 };
225 /* Note: optionsCopy is mutated by this method. If it is ever exposed in the
226 * API, we should copy it before mutating.
227 */
228
229
230 var auto_render_renderMathInText = function renderMathInText(text, optionsCopy) {
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 }
248
249 external_katex_default.a.render(math, span, optionsCopy);
250 } catch (e) {
251 if (!(e instanceof external_katex_default.a.ParseError)) {
252 throw e;
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);
261 }
262 }
263
264 return fragment;
265 };
266
267 var renderElem = function renderElem(elem, optionsCopy) {
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);
286 }
287 })();
288 } // Otherwise, it's something else, and ignore it.
289
290 }
291 };
292
293 var renderMathInElement = function renderMathInElement(elem, options) {
294 if (!elem) {
295 throw new Error("No element provided to render");
296 }
297
298 var optionsCopy = {}; // Object.assign(optionsCopy, option)
299
300 for (var option in options) {
301 if (options.hasOwnProperty(option)) {
302 optionsCopy[option] = options[option];
303 }
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);
333 };
334
335 /* harmony default export */ var auto_render = __webpack_exports__["default"] = (renderMathInElement);
336
337 /***/ })
338 /******/ ])["default"];
339 });