devela 0.26.0

A development layer of coherence.
Documentation
<!-- devela::src::_doc::header.html -->
<script>
  document.addEventListener("DOMContentLoaded", async function() {
    // Ensure this script runs only once per page load
    if (window.scriptLoaderInitialized) return;
    window.scriptLoaderInitialized = true;

    // console.log("Loaded: src/_doc/header.html:"); // DEBUG

    /* settings */

    const crateConfigs = {
      "devela": {
        katex: ["/zall", "/zall_", "/media/color", "/num", "/phys"],
        katexChem: ["/zall/","/zall_", "/phys/"]
      },
      "devela_base_core": {
        katex: ["/zall", "zall_", "/num", "/phys"],
        katexChem: []
      },
    };

    // Find which crate this page belongs to
    let currentCrate = null;
    let relativePath = null;
    for (const crateName of Object.keys(crateConfigs)) {
      // Find the LAST occurrence of the crate name in the path
      const lastCrateIndex = window.location.pathname.lastIndexOf(`/${crateName}/`);
      if (lastCrateIndex !== -1) {
        currentCrate = crateName;
        // Extract everything after the last occurence of the crate name
        relativePath = window.location.pathname.slice(lastCrateIndex + crateName.length + 1);
        break;
      }
    }
    if (!currentCrate) return; // Not a known crate
    const config = crateConfigs[currentCrate];

    /* load resources */

    try {
      if (config.katex && config.katex.some(prefix => relativePath.startsWith(prefix))) {
        await loadKatex().catch(handleMathError);
      // } else {
      //   console.log("Not loading KaTeX for:", relativePath); // DEBUG
      }
      if (config.katexChem && config.katexChem.some(prefix => relativePath.startsWith(prefix))) {
        await loadKatexChem().catch(handleMathError);
      }
    } catch (error) {
      handleMathError(error);
    }

    /* resource loading functions */

    // INFO: https://katex.org/docs/browser.html#starter-template
    // INFO: https://github.com/KaTeX/KaTeX/tree/main/contrib/copy-tex
    async function loadKatex() {
      console.log("Loading KaTeX for:", relativePath);

      // Load CSS, and scripts sequentially
      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" // ==
      });

      // Initialize KaTeX
      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"
      });
      // Add custom styles: https://katex.org/docs/issues.html#css-customization
      const katexStyle = document.createElement("style");
      katexStyle.textContent = `
        .katex-display { overflow: hidden; }
        .docblock p { overflow-y: hidden; }
      `;
      document.head.appendChild(katexStyle);
    }

    // INFO: https://github.com/KaTeX/KaTeX/blob/main/contrib/mhchem/README.md
    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" // ==
      });
    }

    /* helper functions */

    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>