Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mermaid渲染 #180

Open
wlkla opened this issue Sep 2, 2024 · 2 comments
Open

mermaid渲染 #180

wlkla opened this issue Sep 2, 2024 · 2 comments

Comments

@wlkla
Copy link

wlkla commented Sep 2, 2024

我在script中引用了mermaid的js文件,但是网站并没有正确渲染并显示,我该怎么正确调用并添加script呢

@Meekdai
Copy link
Owner

Meekdai commented Sep 3, 2024

你添加的时候,后面没有添加</script>

@wlkla
Copy link
Author

wlkla commented Sep 9, 2024

我后面又修改了,发现还是不行,看了一下网页代码发现应该是显示代码时将mermaid代码拆成了多个部分,然后逐个渲染显示,导致不能正确识别完整代码,我就重新编写了一个渲染脚本:

// 等待DOM完全加载
document.addEventListener('DOMContentLoaded', function() {
    // 查找所有包含Mermaid代码的元素
    var mermaidDivs = document.querySelectorAll('.highlight-source-mermaid');

    mermaidDivs.forEach(function(div) {
        // 提取Mermaid代码
        var codeElement = div.querySelector('code');
        if (codeElement) {
            var mermaidCode = codeElement.textContent;

            // 创建新的div来放置渲染后的图表
            var renderDiv = document.createElement('div');
            renderDiv.className = 'mermaid';
            renderDiv.textContent = mermaidCode;

            // 替换原来的pre元素
            var preElement = div.querySelector('pre');
            if (preElement) {
                preElement.parentNode.replaceChild(renderDiv, preElement);
            }
        }
    });

    // 初始化Mermaid
    mermaid.initialize({ startOnLoad: true });
});

然后就可以正确显示了,但是有一个小问题就是此时复制按钮没法复制完整代码,仅复制渲染后图片中文字,我暂时没有修复这个小bug,希望哪位大佬有空帮忙完善一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants