Andrew Vink
Andrew Vink

Reputation: 331

How to integrate Mermaid in Marked?

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>

Upvotes: 4

Views: 9456

Answers (3)

user2070364
user2070364

Reputation: 88

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

Upvotes: 0

Knut Sveidqvist
Knut Sveidqvist

Reputation: 231

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

Upvotes: 2

Carl
Carl

Reputation: 1019

For me, Mermaid diagrams render fine in Marked 2 (version: 2.6.4).

Example markdown source:

<script src="https://unpkg.com/[email protected]/dist/mermaid.min.js"></script>
```mermaid
graph TD;A-->B;A-->C;B-->D;C-->D;

```

Caveat: it seems Mermaid needs to be invoked twice after each edit. So after an edit, once Marked 2 has updated its streaming preview, I need to hit "Refresh".

Marked 2 settings:

  • "Generating typographically correct quotes and punctuation" unchecked
  • Syntax highlighting: unchecked
  • Processor: either MultiMarkdown or Discount

Related posts on the Marked 2 forum:

Upvotes: 0

Related Questions