www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

auto-render.js (10034B)


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