auto-render.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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__757__) {
  11. return /******/ (function() { // webpackBootstrap
  12. /******/ "use strict";
  13. /******/ var __webpack_modules__ = ({
  14. /***/ 757:
  15. /***/ (function(module) {
  16. module.exports = __WEBPACK_EXTERNAL_MODULE__757__;
  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. // EXPORTS
  77. __webpack_require__.d(__webpack_exports__, {
  78. "default": function() { return /* binding */ auto_render; }
  79. });
  80. // EXTERNAL MODULE: external "katex"
  81. var external_katex_ = __webpack_require__(757);
  82. var external_katex_default = /*#__PURE__*/__webpack_require__.n(external_katex_);
  83. ;// ./contrib/auto-render/splitAtDelimiters.ts
  84. /* eslint no-constant-condition:0 */
  85. const findEndOfMath = function (delimiter, text, startIndex) {
  86. // Adapted from
  87. // https://github.com/Khan/perseus/blob/master/src/perseus-markdown.jsx
  88. let index = startIndex;
  89. let braceLevel = 0;
  90. const delimLength = delimiter.length;
  91. while (index < text.length) {
  92. const character = text[index];
  93. if (braceLevel <= 0 && text.slice(index, index + delimLength) === delimiter) {
  94. return index;
  95. } else if (character === "\\") {
  96. index++;
  97. } else if (character === "{") {
  98. braceLevel++;
  99. } else if (character === "}") {
  100. braceLevel--;
  101. }
  102. index++;
  103. }
  104. return -1;
  105. };
  106. const escapeRegex = function (string) {
  107. return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
  108. };
  109. const amsRegex = /^\\begin{/;
  110. const splitAtDelimiters = function (text, delimiters) {
  111. let index;
  112. const data = [];
  113. const regexLeft = new RegExp("(" + delimiters.map(x => escapeRegex(x.left)).join("|") + ")");
  114. while (true) {
  115. index = text.search(regexLeft);
  116. if (index === -1) {
  117. break;
  118. }
  119. if (index > 0) {
  120. data.push({
  121. type: "text",
  122. data: text.slice(0, index)
  123. });
  124. text = text.slice(index); // now text starts with delimiter
  125. }
  126. // ... so this always succeeds:
  127. const i = delimiters.findIndex(delim => text.startsWith(delim.left));
  128. index = findEndOfMath(delimiters[i].right, text, delimiters[i].left.length);
  129. if (index === -1) {
  130. break;
  131. }
  132. const rawData = text.slice(0, index + delimiters[i].right.length);
  133. const math = amsRegex.test(rawData) ? rawData : text.slice(delimiters[i].left.length, index);
  134. data.push({
  135. type: "math",
  136. data: math,
  137. rawData,
  138. display: delimiters[i].display
  139. });
  140. text = text.slice(index + delimiters[i].right.length);
  141. }
  142. if (text !== "") {
  143. data.push({
  144. type: "text",
  145. data: text
  146. });
  147. }
  148. return data;
  149. };
  150. /* harmony default export */ var auto_render_splitAtDelimiters = (splitAtDelimiters);
  151. ;// ./contrib/auto-render/auto-render.ts
  152. /* eslint no-console:0 */
  153. /* Note: optionsCopy is mutated by this method. If it is ever exposed in the
  154. * API, we should copy it before mutating.
  155. */
  156. const renderMathInText = function (text, optionsCopy) {
  157. const data = auto_render_splitAtDelimiters(text, optionsCopy.delimiters);
  158. if (data.length === 1 && data[0].type === 'text') {
  159. // There is no formula in the text.
  160. // Let's return null which means there is no need to replace
  161. // the current text node with a new one.
  162. return null;
  163. }
  164. const fragment = document.createDocumentFragment();
  165. for (let i = 0; i < data.length; i++) {
  166. if (data[i].type === "text") {
  167. fragment.appendChild(document.createTextNode(data[i].data));
  168. } else {
  169. const span = document.createElement("span");
  170. let math = data[i].data;
  171. // Override any display mode defined in the settings with that
  172. // defined by the text itself
  173. optionsCopy.displayMode = data[i].display;
  174. try {
  175. if (optionsCopy.preProcess) {
  176. math = optionsCopy.preProcess(math);
  177. }
  178. external_katex_default().render(math, span, optionsCopy);
  179. } catch (e) {
  180. if (!(e instanceof (external_katex_default()).ParseError)) {
  181. throw e;
  182. }
  183. optionsCopy.errorCallback("KaTeX auto-render: Failed to parse `" + data[i].data + "` with ", e);
  184. fragment.appendChild(document.createTextNode(data[i].rawData));
  185. continue;
  186. }
  187. fragment.appendChild(span);
  188. }
  189. }
  190. return fragment;
  191. };
  192. const renderElem = function (elem, optionsCopy) {
  193. for (let i = 0; i < elem.childNodes.length; i++) {
  194. const childNode = elem.childNodes[i];
  195. if (childNode.nodeType === 3) {
  196. var _childNode$textConten;
  197. // Text node
  198. // Concatenate all sibling text nodes.
  199. // Webkit browsers split very large text nodes into smaller ones,
  200. // so the delimiters may be split across different nodes.
  201. let textContentConcat = (_childNode$textConten = childNode.textContent) != null ? _childNode$textConten : "";
  202. let sibling = childNode.nextSibling;
  203. let nSiblings = 0;
  204. while (sibling && sibling.nodeType === Node.TEXT_NODE) {
  205. var _sibling$textContent;
  206. textContentConcat += (_sibling$textContent = sibling.textContent) != null ? _sibling$textContent : "";
  207. sibling = sibling.nextSibling;
  208. nSiblings++;
  209. }
  210. const frag = renderMathInText(textContentConcat, optionsCopy);
  211. if (frag) {
  212. // Remove extra text nodes
  213. for (let j = 0; j < nSiblings; j++) {
  214. childNode.nextSibling.remove();
  215. }
  216. i += frag.childNodes.length - 1;
  217. elem.replaceChild(frag, childNode);
  218. } else {
  219. // If the concatenated text does not contain math
  220. // the siblings will not either
  221. i += nSiblings;
  222. }
  223. } else if (childNode.nodeType === 1) {
  224. // Element node
  225. const className = ' ' + childNode.className + ' ';
  226. const shouldRender = !optionsCopy.ignoredTags.has(childNode.nodeName.toLowerCase()) && optionsCopy.ignoredClasses.every(x => !className.includes(' ' + x + ' '));
  227. if (shouldRender) {
  228. renderElem(childNode, optionsCopy);
  229. }
  230. }
  231. // Otherwise, it's something else, and ignore it.
  232. }
  233. };
  234. const renderMathInElement = function (elem, options) {
  235. if (!elem) {
  236. throw new Error("No element provided to render");
  237. }
  238. const optionsCopy = {};
  239. Object.assign(optionsCopy, options);
  240. // default options
  241. optionsCopy.delimiters = optionsCopy.delimiters || [{
  242. left: "$$",
  243. right: "$$",
  244. display: true
  245. }, {
  246. left: "\\(",
  247. right: "\\)",
  248. display: false
  249. },
  250. // LaTeX uses $…$, but it ruins the display of normal `$` in text:
  251. // {left: "$", right: "$", display: false},
  252. // $ must come after $$
  253. // Render AMS environments even if outside $$…$$ delimiters.
  254. {
  255. left: "\\begin{equation}",
  256. right: "\\end{equation}",
  257. display: true
  258. }, {
  259. left: "\\begin{align}",
  260. right: "\\end{align}",
  261. display: true
  262. }, {
  263. left: "\\begin{alignat}",
  264. right: "\\end{alignat}",
  265. display: true
  266. }, {
  267. left: "\\begin{gather}",
  268. right: "\\end{gather}",
  269. display: true
  270. }, {
  271. left: "\\begin{CD}",
  272. right: "\\end{CD}",
  273. display: true
  274. }, {
  275. left: "\\[",
  276. right: "\\]",
  277. display: true
  278. }];
  279. optionsCopy.ignoredTags = new Set((options == null ? void 0 : options.ignoredTags) || ["script", "noscript", "style", "textarea", "pre", "code", "option"]);
  280. optionsCopy.ignoredClasses = optionsCopy.ignoredClasses || [];
  281. optionsCopy.errorCallback = optionsCopy.errorCallback || console.error;
  282. // Enable sharing of global macros defined via `\gdef` between different
  283. // math elements within a single call to `renderMathInElement`.
  284. optionsCopy.macros = optionsCopy.macros || {};
  285. renderElem(elem, optionsCopy);
  286. };
  287. /* harmony default export */ var auto_render = (renderMathInElement);
  288. __webpack_exports__ = __webpack_exports__["default"];
  289. /******/ return __webpack_exports__;
  290. /******/ })()
  291. ;
  292. });