www

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

mathtex-script-type.mjs (751B)


      1 import katex from '../katex.mjs';
      2 
      3 var scripts = document.body.getElementsByTagName("script");
      4 scripts = Array.prototype.slice.call(scripts);
      5 scripts.forEach(function (script) {
      6   if (!script.type || !script.type.match(/math\/tex/i)) {
      7     return -1;
      8   }
      9 
     10   var display = script.type.match(/mode\s*=\s*display(;|\s|\n|$)/) != null;
     11   var katexElement = document.createElement(display ? "div" : "span");
     12   katexElement.setAttribute("class", display ? "equation" : "inline-equation");
     13 
     14   try {
     15     katex.render(script.text, katexElement, {
     16       displayMode: display
     17     });
     18   } catch (err) {
     19     //console.error(err); linter doesn't like this
     20     katexElement.textContent = script.text;
     21   }
     22 
     23   script.parentNode.replaceChild(katexElement, script);
     24 });