UNPKG

11.4 kBJavaScriptView Raw
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__) {
11return /******/ (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
101module.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"
111var external_katex_ = __webpack_require__(0);
112var external_katex_default = /*#__PURE__*/__webpack_require__.n(external_katex_);
113
114// CONCATENATED MODULE: ./contrib/auto-render/splitAtDelimiters.js
115/* eslint no-constant-condition:0 */
116var 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
142var 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
212var 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
230var auto_render_renderMathInText = function renderMathInText(text, optionsCopy) {
231 var data = auto_render_splitWithDelimiters(text, optionsCopy.delimiters);
232
233 if (data.length === 1 && data[0].type === 'text') {
234 // There is no formula in the text.
235 // Let's return null which means there is no need to replace
236 // the current text node with a new one.
237 return null;
238 }
239
240 var fragment = document.createDocumentFragment();
241
242 for (var i = 0; i < data.length; i++) {
243 if (data[i].type === "text") {
244 fragment.appendChild(document.createTextNode(data[i].data));
245 } else {
246 var span = document.createElement("span");
247 var math = data[i].data; // Override any display mode defined in the settings with that
248 // defined by the text itself
249
250 optionsCopy.displayMode = data[i].display;
251
252 try {
253 if (optionsCopy.preProcess) {
254 math = optionsCopy.preProcess(math);
255 }
256
257 external_katex_default.a.render(math, span, optionsCopy);
258 } catch (e) {
259 if (!(e instanceof external_katex_default.a.ParseError)) {
260 throw e;
261 }
262
263 optionsCopy.errorCallback("KaTeX auto-render: Failed to parse `" + data[i].data + "` with ", e);
264 fragment.appendChild(document.createTextNode(data[i].rawData));
265 continue;
266 }
267
268 fragment.appendChild(span);
269 }
270 }
271
272 return fragment;
273};
274
275var renderElem = function renderElem(elem, optionsCopy) {
276 for (var i = 0; i < elem.childNodes.length; i++) {
277 var childNode = elem.childNodes[i];
278
279 if (childNode.nodeType === 3) {
280 // Text node
281 var frag = auto_render_renderMathInText(childNode.textContent, optionsCopy);
282
283 if (frag) {
284 i += frag.childNodes.length - 1;
285 elem.replaceChild(frag, childNode);
286 }
287 } else if (childNode.nodeType === 1) {
288 (function () {
289 // Element node
290 var className = ' ' + childNode.className + ' ';
291 var shouldRender = optionsCopy.ignoredTags.indexOf(childNode.nodeName.toLowerCase()) === -1 && optionsCopy.ignoredClasses.every(function (x) {
292 return className.indexOf(' ' + x + ' ') === -1;
293 });
294
295 if (shouldRender) {
296 renderElem(childNode, optionsCopy);
297 }
298 })();
299 } // Otherwise, it's something else, and ignore it.
300
301 }
302};
303
304var renderMathInElement = function renderMathInElement(elem, options) {
305 if (!elem) {
306 throw new Error("No element provided to render");
307 }
308
309 var optionsCopy = {}; // Object.assign(optionsCopy, option)
310
311 for (var option in options) {
312 if (options.hasOwnProperty(option)) {
313 optionsCopy[option] = options[option];
314 }
315 } // default options
316
317
318 optionsCopy.delimiters = optionsCopy.delimiters || [{
319 left: "$$",
320 right: "$$",
321 display: true
322 }, {
323 left: "\\(",
324 right: "\\)",
325 display: false
326 }, // LaTeX uses $…$, but it ruins the display of normal `$` in text:
327 // {left: "$", right: "$", display: false},
328 // \[…\] must come last in this array. Otherwise, renderMathInElement
329 // will search for \[ before it searches for $$ or \(
330 // That makes it susceptible to finding a \\[0.3em] row delimiter and
331 // treating it as if it were the start of a KaTeX math zone.
332 {
333 left: "\\[",
334 right: "\\]",
335 display: true
336 }];
337 optionsCopy.ignoredTags = optionsCopy.ignoredTags || ["script", "noscript", "style", "textarea", "pre", "code", "option"];
338 optionsCopy.ignoredClasses = optionsCopy.ignoredClasses || [];
339 optionsCopy.errorCallback = optionsCopy.errorCallback || console.error; // Enable sharing of global macros defined via `\gdef` between different
340 // math elements within a single call to `renderMathInElement`.
341
342 optionsCopy.macros = optionsCopy.macros || {};
343 renderElem(elem, optionsCopy);
344};
345
346/* harmony default export */ var auto_render = __webpack_exports__["default"] = (renderMathInElement);
347
348/***/ })
349/******/ ])["default"];
350});
\No newline at end of file