Skip to content

Rendering Mermaid graph after navigation #2068

@David-Klemenc

Description

@David-Klemenc

Opening Task.html documentation and then navigating to the Supervisor.html docs via the sidebar does not trigger mermaid and therefore the mermaid graph is not visible - refreshing the page will trigger mermaid and render the graph:

To display the graph when navigating from the sidebar mermaid would have to be triggered. Example code that achieves this:

<script>
  function mermaidLoaded() {
    mermaid.initialize({
      startOnLoad: false,
      theme: document.body.className.includes("dark") ? "dark" : "default"
    });
    const runMermaid = () => {
      let id = 0;
      for (const codeEl of document.querySelectorAll("pre code.mermaid")) {
        const preEl = codeEl.parentElement;
        const graphDefinition = codeEl.textContent;
        const graphEl = document.createElement("div");
        const graphId = "mermaid-graph-" + id++;
        mermaid.render(graphId, graphDefinition).then(({svg, bindFunctions}) => {
          graphEl.innerHTML = svg;
          bindFunctions?.(graphEl);
          preEl.insertAdjacentElement("afterend", graphEl);
          preEl.remove();
        });
      }
    }
    const observer = new MutationObserver(runMermaid);

    // re run mermaid each time we navigate via sidebar
    observer.observe(document.querySelector("#sidebar"), {
      attributes: true,
      subtree: true,
      childList: true,
    });

    runMermaid();
  }
</script>
<script async src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.min.js" onload="mermaidLoaded();"></script>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions