User:Wee Jimmy/common.js

// no-semi, print-width=88, trailing-comma=all $(" ").appendTo("head").attr({ type: "text/css",  rel: "stylesheet",  href: "https://cdn.jsdelivr.net/npm/katex/dist/katex.min.css", }) var css = String.raw $(" ").appendTo("head").text(css` .katex-display {    margin: 0.5em 0;  }  .katex-display > .base {    margin: 0.25em 0;  }  .katex-display > .katex {    white-space: normal;  } `) await $.getScript("https://cdn.jsdelivr.net/npm/katex/dist/katex.min.js") await $.getScript("https://cdn.jsdelivr.net/npm/katex/dist/contrib/mhchem.min.js")

// for text mode function fallback(el) { // strip away the $ return el.innerText.substring(1, el.innerText.length - 1) }

// for "mathml with fallback" mode function mml(el) { return el.querySelector('semantics > annotation[encoding="application/x-tex"]') .textContent }

function render_tex(el, how, displayMode) { let span = document.createElement("span") try { let source = how(el) if (source.startsWith("{")) source = source.substring(1, source.length - 1) katex.render(source, span, { displayMode, fleqn: true }) el.replaceWith(span) } catch (e) { console.log(el) console.warn(e.message) } }

document .querySelectorAll("span.tex.mwe-math-fallback-source-inline") .forEach((el) => render_tex(el, fallback, false)) document .querySelectorAll("span.tex.mwe-math-fallback-source-display") .forEach((el) => render_tex(el, fallback, true)) document .querySelectorAll("span.mwe-math-element") .forEach((el) => render_tex(el, mml, false)) document .querySelectorAll("div.mwe-math-element") .forEach((el) => render_tex(el, mml, true))