<script>
document.addEventListener("DOMContentLoaded", async function() {
if (window.scriptLoaderInitialized) return;
window.scriptLoaderInitialized = true;
const crateConfigs = {
"devela": {
katex: ["/zall", "/zall_", "/media/color", "/num", "/phys"],
katexChem: ["/zall/","/zall_", "/phys/"]
},
"devela_base_core": {
katex: ["/zall", "zall_", "/num", "/phys"],
katexChem: []
},
};
let currentCrate = null;
let relativePath = null;
for (const crateName of Object.keys(crateConfigs)) {
const lastCrateIndex = window.location.pathname.lastIndexOf(`/${crateName}/`);
if (lastCrateIndex !== -1) {
currentCrate = crateName;
relativePath = window.location.pathname.slice(lastCrateIndex + crateName.length + 1);
break;
}
}
if (!currentCrate) return; const config = crateConfigs[currentCrate];
try {
if (config.katex && config.katex.some(prefix => relativePath.startsWith(prefix))) {
await loadKatex().catch(handleMathError);
}
if (config.katexChem && config.katexChem.some(prefix => relativePath.startsWith(prefix))) {
await loadKatexChem().catch(handleMathError);
}
} catch (error) {
handleMathError(error);
}
async function loadKatex() {
console.log("Loading KaTeX for:", relativePath);
const katexCSS = document.createElement("link");
katexCSS.rel = "stylesheet";
katexCSS.href = "https://cdn.jsdelivr.net/npm/katex@0.16.27/dist/katex.min.css"; katexCSS.integrity = "sha384-Pu5+C18nP5dwykLJOhd2U4Xen7rjScHN/qusop27hdd2drI+lL5KvX7YntvT8yew";
katexCSS.crossOrigin = "anonymous";
document.head.appendChild(katexCSS);
await loadScript({
src: "https://cdn.jsdelivr.net/npm/katex@0.16.27/dist/katex.min.js",
integrity: "sha384-2B8pfmZZ6JlVoScJm/5hQfNS2TI/6hPqDZInzzPc8oHpN5SgeNOf4LzREO6p5YtZ" });
await loadScript({
src: "https://cdn.jsdelivr.net/npm/katex@0.16.27/dist/contrib/auto-render.min.js",
integrity: "sha384-hCXGrW6PitJEwbkoStFjeJxv+fSOOQKOPbJxSfM6G5sWZjAyWhXiTIIAmQqnlLlh" });
await loadScript({
src: "https://cdn.jsdelivr.net/npm/katex@0.16.27/dist/contrib/copy-tex.min.js",
integrity: "sha384-HORx6nWi8j5/mYA+y57/9/CZc5z8HnEw4WUZWy5yOn9ToKBv1l58vJaufFAn9Zzi" });
renderMathInElement(document.body, {
delimiters: [
{left: "$$", right: "$$", display: true},
{left: "$", right: "$", display: false},
{left: "\\(", right: "\\)", display: false},
{left: "\\[", right: "\\]", display: true}
],
trust: (context) => context.command === "\\href"
});
const katexStyle = document.createElement("style");
katexStyle.textContent = `
.katex-display { overflow: hidden; }
.docblock p { overflow-y: hidden; }
`;
document.head.appendChild(katexStyle);
}
async function loadKatexChem() {
console.log("Loading KaTeX MhChem plugin for:", relativePath);
await loadScript({
src: "https://cdn.jsdelivr.net/npm/katex@0.16.27/dist/contrib/mhchem.min.js",
integrity: "sha384-F2ptQFZqNJuqfGGl28mIXyQ5kXH48spn7rcoS0Y9psqIKAcZPLd1NzwFlm/bl1mH" });
}
function loadScript(config) {
return new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src = config.src;
if (config.integrity) {
script.integrity = config.integrity;
script.crossOrigin = "anonymous";
}
script.onload = resolve;
script.onerror = () => reject(new Error(`Failed to load script: ${config.src}`));
document.head.appendChild(script);
});
}
function handleMathError(error) {
console.error("Math rendering error:", error);
}
});
</script>