auto-render.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* eslint no-console:0 */
  2. import katex from "katex";
  3. import splitAtDelimiters from "./splitAtDelimiters";
  4. const splitWithDelimiters = function(text, delimiters) {
  5. let data = [{type: "text", data: text}];
  6. for (let i = 0; i < delimiters.length; i++) {
  7. const delimiter = delimiters[i];
  8. data = splitAtDelimiters(
  9. data, delimiter.left, delimiter.right,
  10. delimiter.display || false);
  11. }
  12. return data;
  13. };
  14. /* Note: optionsCopy is mutated by this method. If it is ever exposed in the
  15. * API, we should copy it before mutating.
  16. */
  17. const renderMathInText = function(text, optionsCopy) {
  18. const data = splitWithDelimiters(text, optionsCopy.delimiters);
  19. const fragment = document.createDocumentFragment();
  20. for (let i = 0; i < data.length; i++) {
  21. if (data[i].type === "text") {
  22. fragment.appendChild(document.createTextNode(data[i].data));
  23. } else {
  24. const span = document.createElement("span");
  25. const math = data[i].data;
  26. // Override any display mode defined in the settings with that
  27. // defined by the text itself
  28. optionsCopy.displayMode = data[i].display;
  29. try {
  30. katex.render(math, span, optionsCopy);
  31. } catch (e) {
  32. if (!(e instanceof katex.ParseError)) {
  33. throw e;
  34. }
  35. optionsCopy.errorCallback(
  36. "KaTeX auto-render: Failed to parse `" + data[i].data +
  37. "` with ",
  38. e
  39. );
  40. fragment.appendChild(document.createTextNode(data[i].rawData));
  41. continue;
  42. }
  43. fragment.appendChild(span);
  44. }
  45. }
  46. return fragment;
  47. };
  48. const renderElem = function(elem, optionsCopy) {
  49. for (let i = 0; i < elem.childNodes.length; i++) {
  50. const childNode = elem.childNodes[i];
  51. if (childNode.nodeType === 3) {
  52. // Text node
  53. const frag = renderMathInText(childNode.textContent, optionsCopy);
  54. i += frag.childNodes.length - 1;
  55. elem.replaceChild(frag, childNode);
  56. } else if (childNode.nodeType === 1) {
  57. // Element node
  58. const className = ' ' + childNode.className + ' ';
  59. const shouldRender = optionsCopy.ignoredTags.indexOf(
  60. childNode.nodeName.toLowerCase()) === -1 &&
  61. optionsCopy.ignoredClasses.every(
  62. x => className.indexOf(' ' + x + ' ') === -1);
  63. if (shouldRender) {
  64. renderElem(childNode, optionsCopy);
  65. }
  66. }
  67. // Otherwise, it's something else, and ignore it.
  68. }
  69. };
  70. const renderMathInElement = function(elem, options) {
  71. if (!elem) {
  72. throw new Error("No element provided to render");
  73. }
  74. const optionsCopy = {};
  75. // Object.assign(optionsCopy, option)
  76. for (const option in options) {
  77. if (options.hasOwnProperty(option)) {
  78. optionsCopy[option] = options[option];
  79. }
  80. }
  81. // default options
  82. optionsCopy.delimiters = optionsCopy.delimiters || [
  83. {left: "$$", right: "$$", display: true},
  84. {left: "\\(", right: "\\)", display: false},
  85. // LaTeX uses $…$, but it ruins the display of normal `$` in text:
  86. // {left: "$", right: "$", display: false},
  87. // \[…\] must come last in this array. Otherwise, renderMathInElement
  88. // will search for \[ before it searches for $$ or \(
  89. // That makes it susceptible to finding a \\[0.3em] row delimiter and
  90. // treating it as if it were the start of a KaTeX math zone.
  91. {left: "\\[", right: "\\]", display: true},
  92. ];
  93. optionsCopy.ignoredTags = optionsCopy.ignoredTags || [
  94. "script", "noscript", "style", "textarea", "pre", "code",
  95. ];
  96. optionsCopy.ignoredClasses = optionsCopy.ignoredClasses || [];
  97. optionsCopy.errorCallback = optionsCopy.errorCallback || console.error;
  98. // Enable sharing of global macros defined via `\gdef` between different
  99. // math elements within a single call to `renderMathInElement`.
  100. optionsCopy.macros = optionsCopy.macros || {};
  101. renderElem(elem, optionsCopy);
  102. };
  103. export default renderMathInElement;