You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
9.8 KiB

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