auto-render.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. // Concatenate all sibling text nodes.
  202. // Webkit browsers split very large text nodes into smaller ones,
  203. // so the delimiters may be split across different nodes.
  204. var textContentConcat = childNode.textContent;
  205. var sibling = childNode.nextSibling;
  206. var nSiblings = 0;
  207. while (sibling && sibling.nodeType === Node.TEXT_NODE) {
  208. textContentConcat += sibling.textContent;
  209. sibling = sibling.nextSibling;
  210. nSiblings++;
  211. }
  212. var frag = renderMathInText(textContentConcat, optionsCopy);
  213. if (frag) {
  214. // Remove extra text nodes
  215. for (var j = 0; j < nSiblings; j++) {
  216. childNode.nextSibling.remove();
  217. }
  218. i += frag.childNodes.length - 1;
  219. elem.replaceChild(frag, childNode);
  220. } else {
  221. // If the concatenated text does not contain math
  222. // the siblings will not either
  223. i += nSiblings;
  224. }
  225. } else if (childNode.nodeType === 1) {
  226. (function () {
  227. // Element node
  228. var className = ' ' + childNode.className + ' ';
  229. var shouldRender = optionsCopy.ignoredTags.indexOf(childNode.nodeName.toLowerCase()) === -1 && optionsCopy.ignoredClasses.every(function (x) {
  230. return className.indexOf(' ' + x + ' ') === -1;
  231. });
  232. if (shouldRender) {
  233. renderElem(childNode, optionsCopy);
  234. }
  235. })();
  236. } // Otherwise, it's something else, and ignore it.
  237. }
  238. };
  239. var renderMathInElement = function renderMathInElement(elem, options) {
  240. if (!elem) {
  241. throw new Error("No element provided to render");
  242. }
  243. var optionsCopy = {}; // Object.assign(optionsCopy, option)
  244. for (var option in options) {
  245. if (options.hasOwnProperty(option)) {
  246. optionsCopy[option] = options[option];
  247. }
  248. } // default options
  249. optionsCopy.delimiters = optionsCopy.delimiters || [{
  250. left: "$$",
  251. right: "$$",
  252. display: true
  253. }, {
  254. left: "\\(",
  255. right: "\\)",
  256. display: false
  257. }, // LaTeX uses $…$, but it ruins the display of normal `$` in text:
  258. // {left: "$", right: "$", display: false},
  259. // $ must come after $$
  260. // Render AMS environments even if outside $$…$$ delimiters.
  261. {
  262. left: "\\begin{equation}",
  263. right: "\\end{equation}",
  264. display: true
  265. }, {
  266. left: "\\begin{align}",
  267. right: "\\end{align}",
  268. display: true
  269. }, {
  270. left: "\\begin{alignat}",
  271. right: "\\end{alignat}",
  272. display: true
  273. }, {
  274. left: "\\begin{gather}",
  275. right: "\\end{gather}",
  276. display: true
  277. }, {
  278. left: "\\begin{CD}",
  279. right: "\\end{CD}",
  280. display: true
  281. }, {
  282. left: "\\[",
  283. right: "\\]",
  284. display: true
  285. }];
  286. optionsCopy.ignoredTags = optionsCopy.ignoredTags || ["script", "noscript", "style", "textarea", "pre", "code", "option"];
  287. optionsCopy.ignoredClasses = optionsCopy.ignoredClasses || [];
  288. optionsCopy.errorCallback = optionsCopy.errorCallback || console.error; // Enable sharing of global macros defined via `\gdef` between different
  289. // math elements within a single call to `renderMathInElement`.
  290. optionsCopy.macros = optionsCopy.macros || {};
  291. renderElem(elem, optionsCopy);
  292. };
  293. /* harmony default export */ var auto_render = (renderMathInElement);
  294. }();
  295. __webpack_exports__ = __webpack_exports__["default"];
  296. /******/ return __webpack_exports__;
  297. /******/ })()
  298. ;
  299. });