Skip to content

mermaid can't render on preview #2

@yasumichi

Description

@yasumichi

I try fix.

(function () {
    console.log("gme.js loaded.");

    var renderKatex = function() {
        var mathElems = document.getElementsByClassName("katex");
        var elems = [];
        for (const i in mathElems) {
            if (mathElems.hasOwnProperty(i)) elems.push(mathElems[i]);
        }

        elems.forEach(elem => {
            katex.render(elem.textContent, elem, { throwOnError: false, displayMode: elem.nodeName !== 'SPAN', });
        });
    };

    var renderMermaid = function() {
        document.querySelectorAll('.mermaid').forEach(async (node, index) => {
            const graphDefinition = node.textContent;
            const graphId = `mermaid-${index}`;
            node.id = graphId;
            const renderResult = await mermaid.mermaidAPI.render(graphId, graphDefinition, node);
            node.innerHTML = renderResult;
        });
    };

    var renderAll = function() {
        renderKatex();
        renderMermaid();
    };

    document.addEventListener("DOMContentLoaded", function(){
        var preview = document.querySelector("#preview");
        if (preview) {
            const config = { attributes: false, childList: true, subtree: false };

            const observer = new MutationObserver((mutations) => {
                mutations.forEach((mutation) => {
                    if (mutation.addedNodes.length == 1) {
                        observer.disconnect();
                        renderAll();
                        observer.observe(preview, config);
                    }
                });
            });

            observer.observe(preview, config);
        }
        renderKatex();
    });
})();

It will be drawn up to halfway.
But error "syntax error in text mermaid" occurs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions