Mercurial > repos > public > wdown
diff katex/contrib/mathtex-script-type.mjs @ 8:4a25b534c81c javascript-experiment
Add v8 engine and include katex
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Wed, 17 Jun 2020 21:43:52 +0200 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/katex/contrib/mathtex-script-type.mjs Wed Jun 17 21:43:52 2020 +0200 @@ -0,0 +1,24 @@ +import katex from '../katex.mjs'; + +let scripts = document.body.getElementsByTagName("script"); +scripts = Array.prototype.slice.call(scripts); +scripts.forEach(function (script) { + if (!script.type || !script.type.match(/math\/tex/i)) { + return -1; + } + + const display = script.type.match(/mode\s*=\s*display(;|\s|\n|$)/) != null; + const katexElement = document.createElement(display ? "div" : "span"); + katexElement.setAttribute("class", display ? "equation" : "inline-equation"); + + try { + katex.render(script.text, katexElement, { + displayMode: display + }); + } catch (err) { + //console.error(err); linter doesn't like this + katexElement.textContent = script.text; + } + + script.parentNode.replaceChild(katexElement, script); +});