<script>
document.addEventListener("DOMContentLoaded", async function() {
if (window.scriptLoaderInitialized) return;
window.scriptLoaderInitialized = true;
const crateConfigs = {
"devela": {
katex: ["/zall", "/zall_", "/media/visual/color", "/num", "/phys"],
katexChem: ["/zall/","/zall_", "/phys/"]
},
};
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.45/dist/katex.min.css"; katexCSS.integrity = "sha384-UA8juhPf75SzzAMA/4fo3yOU7sBJ0om7SCD2GHq0fZqZco6tr1UCV7nUbk9J90JM";
katexCSS.crossOrigin = "anonymous";
document.head.appendChild(katexCSS);
await loadScript({
src: "https://cdn.jsdelivr.net/npm/katex@0.16.45/dist/katex.min.js",
integrity: "sha384-Tt7wBxLKwSzFVRET4O4U9H6v8MNaQ/CjN2FMP4xFm0ErrFu6aNqoonRVW5W40iGI" });
await loadScript({
src: "https://cdn.jsdelivr.net/npm/katex@0.16.45/dist/contrib/auto-render.min.js",
integrity: "sha384-bjyGPfbij8/NDKJhSGZNP/khQVgtHUE5exjm4Ydllo42FwIgYsdLO2lXGmRBf5Mz" });
await loadScript({
src: "https://cdn.jsdelivr.net/npm/katex@0.16.45/dist/contrib/copy-tex.min.js",
integrity: "sha384-Pe2JWbaShhSRT0SESJ9XLPeAsi4yrNi7r/CiH0Coq7giBjK5a6Ae7XcybE8rNlQP" });
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.45/dist/contrib/mhchem.min.js",
integrity: "sha384-fB8BH//9nBzROkMUsu/Dr35jWHIbnKesUo9rW0hfEgw8mZGnkAyBAjKX9F98OVuo" });
}
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>