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.

222 lines
5.9 KiB

  1. import katex from '../katex.mjs';
  2. /* eslint no-constant-condition:0 */
  3. var findEndOfMath = function findEndOfMath(delimiter, text, startIndex) {
  4. // Adapted from
  5. // https://github.com/Khan/perseus/blob/master/src/perseus-markdown.jsx
  6. var index = startIndex;
  7. var braceLevel = 0;
  8. var delimLength = delimiter.length;
  9. while (index < text.length) {
  10. var character = text[index];
  11. if (braceLevel <= 0 && text.slice(index, index + delimLength) === delimiter) {
  12. return index;
  13. } else if (character === "\\") {
  14. index++;
  15. } else if (character === "{") {
  16. braceLevel++;
  17. } else if (character === "}") {
  18. braceLevel--;
  19. }
  20. index++;
  21. }
  22. return -1;
  23. };
  24. var escapeRegex = function escapeRegex(string) {
  25. return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
  26. };
  27. var amsRegex = /^\\begin{/;
  28. var splitAtDelimiters = function splitAtDelimiters(text, delimiters) {
  29. var index;
  30. var data = [];
  31. var regexLeft = new RegExp("(" + delimiters.map(x => escapeRegex(x.left)).join("|") + ")");
  32. while (true) {
  33. index = text.search(regexLeft);
  34. if (index === -1) {
  35. break;
  36. }
  37. if (index > 0) {
  38. data.push({
  39. type: "text",
  40. data: text.slice(0, index)
  41. });
  42. text = text.slice(index); // now text starts with delimiter
  43. } // ... so this always succeeds:
  44. var i = delimiters.findIndex(delim => text.startsWith(delim.left));
  45. index = findEndOfMath(delimiters[i].right, text, delimiters[i].left.length);
  46. if (index === -1) {
  47. break;
  48. }
  49. var rawData = text.slice(0, index + delimiters[i].right.length);
  50. var math = amsRegex.test(rawData) ? rawData : text.slice(delimiters[i].left.length, index);
  51. data.push({
  52. type: "math",
  53. data: math,
  54. rawData,
  55. display: delimiters[i].display
  56. });
  57. text = text.slice(index + delimiters[i].right.length);
  58. }
  59. if (text !== "") {
  60. data.push({
  61. type: "text",
  62. data: text
  63. });
  64. }
  65. return data;
  66. };
  67. /* eslint no-console:0 */
  68. /* Note: optionsCopy is mutated by this method. If it is ever exposed in the
  69. * API, we should copy it before mutating.
  70. */
  71. var renderMathInText = function renderMathInText(text, optionsCopy) {
  72. var data = splitAtDelimiters(text, optionsCopy.delimiters);
  73. if (data.length === 1 && data[0].type === 'text') {
  74. // There is no formula in the text.
  75. // Let's return null which means there is no need to replace
  76. // the current text node with a new one.
  77. return null;
  78. }
  79. var fragment = document.createDocumentFragment();
  80. for (var i = 0; i < data.length; i++) {
  81. if (data[i].type === "text") {
  82. fragment.appendChild(document.createTextNode(data[i].data));
  83. } else {
  84. var span = document.createElement("span");
  85. var math = data[i].data; // Override any display mode defined in the settings with that
  86. // defined by the text itself
  87. optionsCopy.displayMode = data[i].display;
  88. try {
  89. if (optionsCopy.preProcess) {
  90. math = optionsCopy.preProcess(math);
  91. }
  92. katex.render(math, span, optionsCopy);
  93. } catch (e) {
  94. if (!(e instanceof katex.ParseError)) {
  95. throw e;
  96. }
  97. optionsCopy.errorCallback("KaTeX auto-render: Failed to parse `" + data[i].data + "` with ", e);
  98. fragment.appendChild(document.createTextNode(data[i].rawData));
  99. continue;
  100. }
  101. fragment.appendChild(span);
  102. }
  103. }
  104. return fragment;
  105. };
  106. var renderElem = function renderElem(elem, optionsCopy) {
  107. for (var i = 0; i < elem.childNodes.length; i++) {
  108. var childNode = elem.childNodes[i];
  109. if (childNode.nodeType === 3) {
  110. // Text node
  111. var frag = renderMathInText(childNode.textContent, optionsCopy);
  112. if (frag) {
  113. i += frag.childNodes.length - 1;
  114. elem.replaceChild(frag, childNode);
  115. }
  116. } else if (childNode.nodeType === 1) {
  117. (function () {
  118. // Element node
  119. var className = ' ' + childNode.className + ' ';
  120. var shouldRender = optionsCopy.ignoredTags.indexOf(childNode.nodeName.toLowerCase()) === -1 && optionsCopy.ignoredClasses.every(x => className.indexOf(' ' + x + ' ') === -1);
  121. if (shouldRender) {
  122. renderElem(childNode, optionsCopy);
  123. }
  124. })();
  125. } // Otherwise, it's something else, and ignore it.
  126. }
  127. };
  128. var renderMathInElement = function renderMathInElement(elem, options) {
  129. if (!elem) {
  130. throw new Error("No element provided to render");
  131. }
  132. var optionsCopy = {}; // Object.assign(optionsCopy, option)
  133. for (var option in options) {
  134. if (options.hasOwnProperty(option)) {
  135. optionsCopy[option] = options[option];
  136. }
  137. } // default options
  138. optionsCopy.delimiters = optionsCopy.delimiters || [{
  139. left: "$$",
  140. right: "$$",
  141. display: true
  142. }, {
  143. left: "\\(",
  144. right: "\\)",
  145. display: false
  146. }, // LaTeX uses $…$, but it ruins the display of normal `$` in text:
  147. // {left: "$", right: "$", display: false},
  148. // $ must come after $$
  149. // Render AMS environments even if outside $$…$$ delimiters.
  150. {
  151. left: "\\begin{equation}",
  152. right: "\\end{equation}",
  153. display: true
  154. }, {
  155. left: "\\begin{align}",
  156. right: "\\end{align}",
  157. display: true
  158. }, {
  159. left: "\\begin{alignat}",
  160. right: "\\end{alignat}",
  161. display: true
  162. }, {
  163. left: "\\begin{gather}",
  164. right: "\\end{gather}",
  165. display: true
  166. }, {
  167. left: "\\begin{CD}",
  168. right: "\\end{CD}",
  169. display: true
  170. }, {
  171. left: "\\[",
  172. right: "\\]",
  173. display: true
  174. }];
  175. optionsCopy.ignoredTags = optionsCopy.ignoredTags || ["script", "noscript", "style", "textarea", "pre", "code", "option"];
  176. optionsCopy.ignoredClasses = optionsCopy.ignoredClasses || [];
  177. optionsCopy.errorCallback = optionsCopy.errorCallback || console.error; // Enable sharing of global macros defined via `\gdef` between different
  178. // math elements within a single call to `renderMathInElement`.
  179. optionsCopy.macros = optionsCopy.macros || {};
  180. renderElem(elem, optionsCopy);
  181. };
  182. export { renderMathInElement as default };