arnaucube
arnaucube/arnaucube.comPublic Self-hosted repository
arnaucube.com/blogo-input/js/katex/contrib/auto-render.js
blogo-input/js/katex/contrib/auto-render.jsRaw
  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__771__) {
 11return /******/ (function() { // webpackBootstrap
 12/******/ 	"use strict";
 13/******/ 	var __webpack_modules__ = ({
 14
 15/***/ 771:
 16/***/ (function(module) {
 17
 18module.exports = __WEBPACK_EXTERNAL_MODULE__771__;
 19
 20/***/ })
 21
 22/******/ 	});
 23/************************************************************************/
 24/******/ 	// The module cache
 25/******/ 	var __webpack_module_cache__ = {};
 26/******/ 	
 27/******/ 	// The require function
 28/******/ 	function __webpack_require__(moduleId) {
 29/******/ 		// Check if module is in cache
 30/******/ 		var cachedModule = __webpack_module_cache__[moduleId];
 31/******/ 		if (cachedModule !== undefined) {
 32/******/ 			return cachedModule.exports;
 33/******/ 		}
 34/******/ 		// Create a new module (and put it into the cache)
 35/******/ 		var module = __webpack_module_cache__[moduleId] = {
 36/******/ 			// no module.id needed
 37/******/ 			// no module.loaded needed
 38/******/ 			exports: {}
 39/******/ 		};
 40/******/ 	
 41/******/ 		// Execute the module function
 42/******/ 		__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
 43/******/ 	
 44/******/ 		// Return the exports of the module
 45/******/ 		return module.exports;
 46/******/ 	}
 47/******/ 	
 48/************************************************************************/
 49/******/ 	/* webpack/runtime/compat get default export */
 50/******/ 	!function() {
 51/******/ 		// getDefaultExport function for compatibility with non-harmony modules
 52/******/ 		__webpack_require__.n = function(module) {
 53/******/ 			var getter = module && module.__esModule ?
 54/******/ 				function() { return module['default']; } :
 55/******/ 				function() { return module; };
 56/******/ 			__webpack_require__.d(getter, { a: getter });
 57/******/ 			return getter;
 58/******/ 		};
 59/******/ 	}();
 60/******/ 	
 61/******/ 	/* webpack/runtime/define property getters */
 62/******/ 	!function() {
 63/******/ 		// define getter functions for harmony exports
 64/******/ 		__webpack_require__.d = function(exports, definition) {
 65/******/ 			for(var key in definition) {
 66/******/ 				if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
 67/******/ 					Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
 68/******/ 				}
 69/******/ 			}
 70/******/ 		};
 71/******/ 	}();
 72/******/ 	
 73/******/ 	/* webpack/runtime/hasOwnProperty shorthand */
 74/******/ 	!function() {
 75/******/ 		__webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
 76/******/ 	}();
 77/******/ 	
 78/************************************************************************/
 79var __webpack_exports__ = {};
 80// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
 81!function() {
 82
 83// EXPORTS
 84__webpack_require__.d(__webpack_exports__, {
 85  "default": function() { return /* binding */ auto_render; }
 86});
 87
 88// EXTERNAL MODULE: external "katex"
 89var external_katex_ = __webpack_require__(771);
 90var external_katex_default = /*#__PURE__*/__webpack_require__.n(external_katex_);
 91;// CONCATENATED MODULE: ./contrib/auto-render/splitAtDelimiters.js
 92/* eslint no-constant-condition:0 */
 93var findEndOfMath = function findEndOfMath(delimiter, text, startIndex) {
 94  // Adapted from
 95  // https://github.com/Khan/perseus/blob/master/src/perseus-markdown.jsx
 96  var index = startIndex;
 97  var braceLevel = 0;
 98  var delimLength = delimiter.length;
 99
100  while (index < text.length) {
101    var character = text[index];
102
103    if (braceLevel <= 0 && text.slice(index, index + delimLength) === delimiter) {
104      return index;
105    } else if (character === "\\") {
106      index++;
107    } else if (character === "{") {
108      braceLevel++;
109    } else if (character === "}") {
110      braceLevel--;
111    }
112
113    index++;
114  }
115
116  return -1;
117};
118
119var escapeRegex = function escapeRegex(string) {
120  return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
121};
122
123var amsRegex = /^\\begin{/;
124
125var splitAtDelimiters = function splitAtDelimiters(text, delimiters) {
126  var index;
127  var data = [];
128  var regexLeft = new RegExp("(" + delimiters.map(function (x) {
129    return escapeRegex(x.left);
130  }).join("|") + ")");
131
132  while (true) {
133    index = text.search(regexLeft);
134
135    if (index === -1) {
136      break;
137    }
138
139    if (index > 0) {
140      data.push({
141        type: "text",
142        data: text.slice(0, index)
143      });
144      text = text.slice(index); // now text starts with delimiter
145    } // ... so this always succeeds:
146
147
148    var i = delimiters.findIndex(function (delim) {
149      return text.startsWith(delim.left);
150    });
151    index = findEndOfMath(delimiters[i].right, text, delimiters[i].left.length);
152
153    if (index === -1) {
154      break;
155    }
156
157    var rawData = text.slice(0, index + delimiters[i].right.length);
158    var math = amsRegex.test(rawData) ? rawData : text.slice(delimiters[i].left.length, index);
159    data.push({
160      type: "math",
161      data: math,
162      rawData: rawData,
163      display: delimiters[i].display
164    });
165    text = text.slice(index + delimiters[i].right.length);
166  }
167
168  if (text !== "") {
169    data.push({
170      type: "text",
171      data: text
172    });
173  }
174
175  return data;
176};
177
178/* harmony default export */ var auto_render_splitAtDelimiters = (splitAtDelimiters);
179;// CONCATENATED MODULE: ./contrib/auto-render/auto-render.js
180/* eslint no-console:0 */
181
182
183/* Note: optionsCopy is mutated by this method. If it is ever exposed in the
184 * API, we should copy it before mutating.
185 */
186
187var renderMathInText = function renderMathInText(text, optionsCopy) {
188  var data = auto_render_splitAtDelimiters(text, optionsCopy.delimiters);
189
190  if (data.length === 1 && data[0].type === 'text') {
191    // There is no formula in the text.
192    // Let's return null which means there is no need to replace
193    // the current text node with a new one.
194    return null;
195  }
196
197  var fragment = document.createDocumentFragment();
198
199  for (var i = 0; i < data.length; i++) {
200    if (data[i].type === "text") {
201      fragment.appendChild(document.createTextNode(data[i].data));
202    } else {
203      var span = document.createElement("span");
204      var math = data[i].data; // Override any display mode defined in the settings with that
205      // defined by the text itself
206
207      optionsCopy.displayMode = data[i].display;
208
209      try {
210        if (optionsCopy.preProcess) {
211          math = optionsCopy.preProcess(math);
212        }
213
214        external_katex_default().render(math, span, optionsCopy);
215      } catch (e) {
216        if (!(e instanceof (external_katex_default()).ParseError)) {
217          throw e;
218        }
219
220        optionsCopy.errorCallback("KaTeX auto-render: Failed to parse `" + data[i].data + "` with ", e);
221        fragment.appendChild(document.createTextNode(data[i].rawData));
222        continue;
223      }
224
225      fragment.appendChild(span);
226    }
227  }
228
229  return fragment;
230};
231
232var renderElem = function renderElem(elem, optionsCopy) {
233  for (var i = 0; i < elem.childNodes.length; i++) {
234    var childNode = elem.childNodes[i];
235
236    if (childNode.nodeType === 3) {
237      // Text node
238      var frag = renderMathInText(childNode.textContent, optionsCopy);
239
240      if (frag) {
241        i += frag.childNodes.length - 1;
242        elem.replaceChild(frag, childNode);
243      }
244    } else if (childNode.nodeType === 1) {
245      (function () {
246        // Element node
247        var className = ' ' + childNode.className + ' ';
248        var shouldRender = optionsCopy.ignoredTags.indexOf(childNode.nodeName.toLowerCase()) === -1 && optionsCopy.ignoredClasses.every(function (x) {
249          return className.indexOf(' ' + x + ' ') === -1;
250        });
251
252        if (shouldRender) {
253          renderElem(childNode, optionsCopy);
254        }
255      })();
256    } // Otherwise, it's something else, and ignore it.
257
258  }
259};
260
261var renderMathInElement = function renderMathInElement(elem, options) {
262  if (!elem) {
263    throw new Error("No element provided to render");
264  }
265
266  var optionsCopy = {}; // Object.assign(optionsCopy, option)
267
268  for (var option in options) {
269    if (options.hasOwnProperty(option)) {
270      optionsCopy[option] = options[option];
271    }
272  } // default options
273
274
275  optionsCopy.delimiters = optionsCopy.delimiters || [{
276    left: "$$",
277    right: "$$",
278    display: true
279  }, {
280    left: "\\(",
281    right: "\\)",
282    display: false
283  }, // LaTeX uses $…$, but it ruins the display of normal `$` in text:
284  // {left: "$", right: "$", display: false},
285  // $ must come after $$
286  // Render AMS environments even if outside $$…$$ delimiters.
287  {
288    left: "\\begin{equation}",
289    right: "\\end{equation}",
290    display: true
291  }, {
292    left: "\\begin{align}",
293    right: "\\end{align}",
294    display: true
295  }, {
296    left: "\\begin{alignat}",
297    right: "\\end{alignat}",
298    display: true
299  }, {
300    left: "\\begin{gather}",
301    right: "\\end{gather}",
302    display: true
303  }, {
304    left: "\\begin{CD}",
305    right: "\\end{CD}",
306    display: true
307  }, {
308    left: "\\[",
309    right: "\\]",
310    display: true
311  }];
312  optionsCopy.ignoredTags = optionsCopy.ignoredTags || ["script", "noscript", "style", "textarea", "pre", "code", "option"];
313  optionsCopy.ignoredClasses = optionsCopy.ignoredClasses || [];
314  optionsCopy.errorCallback = optionsCopy.errorCallback || console.error; // Enable sharing of global macros defined via `\gdef` between different
315  // math elements within a single call to `renderMathInElement`.
316
317  optionsCopy.macros = optionsCopy.macros || {};
318  renderElem(elem, optionsCopy);
319};
320
321/* harmony default export */ var auto_render = (renderMathInElement);
322}();
323__webpack_exports__ = __webpack_exports__["default"];
324/******/ 	return __webpack_exports__;
325/******/ })()
326;
327});