1
0
mirror of https://github.com/kalmarek/SmallHyperbolic synced 2024-07-27 21:10:31 +02:00
SmallHyperbolic/docs/math_render.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-02-11 18:12:24 +01:00
function prepareTextForKatex(string) {
return string.replace(/ /g, "")
.replace(/\*/g, "")
.replace(/\^-1/g, "^{-1}")
.replace(/inf/g, "\\infty");
}
function createMathSpan(content) {
let item = document.createElement("span");
item.className = "math";
let math_text = document.createElement("span");
let math_tex = document.createElement("span");
math_text.className = "math-text";
math_text.innerText = content.toString().replace(/\*/g, "").replace(/ /g, "")
math_tex.className = "math-tex";
katex.render(prepareTextForKatex(math_text.innerText), math_tex);
item.appendChild(math_tex);
item.appendChild(math_text);
2022-02-11 18:12:24 +01:00
return item;
}
function toggleKaTeX(elt, toggle) {
let display_text = toggle ? "none" : "revert";
let display_tex = toggle ? "revert" : "none";
for (let child of elt.childNodes) {
switch (child.className) {
case "math-text":
child.style.display = display_text;
break;
case "math-tex":
child.style.display = display_tex;
break;
default:
// nothing
}
}
}