Marked ( Markdown ) + Mermaid ( Flow Chart & Diagrams )

一个人想着一个人 提交于 2020-01-14 20:17:31

问题


Struggling to get Mermaid - https://github.com/knsv/mermaid to work with Marked - https://github.com/chjj/marked

From what I gather I should be able to write the following in markdown

```
  graph TD;A-->B;A-->C;B-->D;C-->D;
```

and have it render

<div class="mermaid">
   FLOWCHART / DIAGRAM IS DRAWN HERE
</div>

What am I missing?

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>Document</title>
      <script src="libs/jquery.min.js"></script>
      <script src="libs/marked.min.js"></script>
      <script src="libs/mermaid.full.js"></script>
    </head>
    <body>

    <div id="content"></div>

    <script>

      var renderer = new marked.Renderer();
      renderer.code = function (code, language) {
        if(code.match(/^sequenceDiagram/)||code.match(/^graph/)){
           return '<div class="mermaid">'+code+'</div>';
        }
      };

        $(document).ready(function(){

        $.get( "test.md", function( data ) {
          // console.log(data);
          $('#content').html(marked(data));
        });

      });

    console.log(marked('```graph TD;A-->B;A-->C;B-->D;C-->D;```', { renderer: renderer }));

    </script>
    </body>
    </html>

回答1:


I tested your code as far as to get the console.log writing the mermaid div.

There is nothing wrong with your marked instantiation and nothing wrong with your renderer. However... the markdown in the console log was not ok.

By adding new lines before and after the graph definition the expeced div was printed to the console.:

\ngraph TD;A-->B;A-->C;B-->D;C-->D;\n

I hope this helps.

/Knut




回答2:


I had that issue as well. Try this string format, it worked for me:

'graph TB\n'+'A --> B\n'+'A --> C\n'+'B --> D\n'+'C --> D\n'



来源:https://stackoverflow.com/questions/28626329/marked-markdown-mermaid-flow-chart-diagrams

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!