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 graphs on multiple pages only render on first synchronous load #504

Closed
jfmou opened this issue May 24, 2018 · 3 comments
Closed

Comments

@jfmou
Copy link

jfmou commented May 24, 2018

Hi all,

Thanks a lot for the awesome work done on docsify, love it !
I'm having troubles with 2 mermaid graphs on two separate pages.
When I access each of these pages synchronously, each graph renders correctly.
But when I try to navigate from one page to the other, the second one asynchronously loaded doesn't have any graph rendered.
On click, it does go through the following snippet :

<script>
  mermaid.initialize({ startOnLoad: false });
  window.$docsify = {
    name: 'COP Front',
    coverpage: true,
    loadSidebar: true,
    subMaxLevel: 3,
    auto2top: true,
    themeColor: '#4e93a5',
    markdown: {
      renderer: {
        code: function(code, lang) {
          if (lang === "mermaid") {
            return (
              '<div class="mermaid">' + mermaid.render(lang, code) + "</div>"
            );
          }
          return this.origin.code.apply(this, arguments);
        }
      }
    }
  }
</script>

Mermaid seems to generate the proper svg graph based on raw text.
But when the destination page is rendered it looks like it didn't replace raw text by svg graph in place resulting in the page rendered without any graph.
I guess i'm missing something, thanks for your help.

@sprungknoedl
Copy link

Hi,

This seems to be caused by the variable lang being used as an ID for mermaid. If you generate a random ID for the first parameter of mermaid.render it works:

<script>
    function makeid(len) {
        var text = new Array(len);
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        for (var i = 0; i < len; i++) text[i] = possible.charAt(Math.floor(Math.random() * possible.length));
        return text.join("");
    }

    mermaid.initialize({ startOnLoad: false });
    window.$docsify = {
    markdown: {
        renderer: {
            code: function(code, lang) {
                if (lang === "mermaid") {
                var id = makeid(10);
                var svg = mermaid.render(id, code)
                return (
                    '<div class="mermaid">' + svg + '</div>'
                );
                }
                return this.origin.code.apply(this, arguments);
            }
            }
        },
    }
</script>

@jfmou
Copy link
Author

jfmou commented Nov 5, 2018

Well thank you very much, it did solve my case. I didn't notice mermaid #render method took an id as first param wasn't obvious for me while reading the demo and/or doc.

Thank you very much !

@jfmou jfmou closed this as completed Nov 5, 2018
@upupming
Copy link
Contributor

upupming commented Nov 5, 2018

Thanks a lot, I have encountered this problem too after following docsify's documentation.

This is the problem:

docsify-mermaid-bug-1

I see the element tab on Chrome, there are several graphs with same id mermaid, so this is the root of this problem:

image

But could anyone explain the reason why this happens?

QingWei-Li pushed a commit that referenced this issue Nov 6, 2018
Please makes sure these boxes are checked before submitting your PR, thank you!

* [x] Make sure you are merging your commits to `master` branch.
* [x] Add some descriptions and refer relative issues for you PR.
* [x] DO NOT include files inside `lib` directory.
QingWei-Li pushed a commit that referenced this issue Feb 19, 2019
Please makes sure these boxes are checked before submitting your PR, thank you!

* [x] Make sure you are merging your commits to `master` branch.
* [x] Add some descriptions and refer relative issues for you PR.
* [x] DO NOT include files inside `lib` directory.
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

3 participants