24

I'm trying to render a flowchart in a markdown file using Mermaid. I have a ReadMe.md file in my GitHub repository, and I'd like to include a basic flowchart to help describe the contents. But I can't figure out how to get it to work. Would someone be able to post some more specific instructions on how to render a simple example?

In this link (https://unpkg.com/mermaid@8.0.0-rc.8/README.md), there's an example code snippet for the Mermaid installation:

    ```
    https://unpkg.com/mermaid@7.1.0/dist/
    ```

I included that code, then tried to make the flowchart in the next code snippet:

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

But all it does is print that text out in the markdown file when I preview it.

It seems like it's possible based on the Mermaid ReadMe: https://github.com/knsv/mermaid/blob/master/README.md. But when I looked at the code it's actually a picture of the flowchart, not a rendering of code. So maybe what I'm asking isn't possible?

Would appreciate any help!

AlexP
  • 577
  • 1
  • 4
  • 15
  • 1
    Just FYI, Webstorm Markdown Settings has a one-click "Install" option for Mermaid and also for PlantUML support (Settings => Markdown). After installing it, the Markdown editor shows the rendered diagram in the preview tab (IDEA Markdown editor has a split view of left: Markdown source, right: rendered view). The bundled Markdown plugin must be enabled of course (by default it is, I think). – Mörre Feb 13 '22 at 11:58

5 Answers5

16

I created a Firefox & Chrome extension that wasn't available at the time of the first answer: Github + Mermaid

To use it you will need to specify mermaid as a language:

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

This works on:

  • PR and issues
  • Comments
  • Gists

(both on preview and save)

PS: I'm not sure whether it's right or not to advertise my own work here, but I feel it answers the questions.

Alex Mercier
  • 161
  • 1
  • 3
  • thanks for providing this! due to github's narrow display column, I also had to add this extension to solve the issue where some large diagrams were squeezed. https://github.com/mdo/github-wide – g2server May 03 '19 at 08:23
13

Unfortunately, github-flavoured markdown doesn't support rendering of mermaid graphs. See this issue for more information and finding comfort in other peoples quest of this feature ;)

If your are using VS Code, you can use this extension to preview your mermaid code blocks inside markdown but note that this does not render once you put it on github. To include the chart on github you will have to render it to a file, though someone suggested somehow using the online mermaid editor to render it and retrieve an URL to a rendered version.

Actually, let me try to insert the example chart from the link on the previousely mentioned page... and, no. We get a Failed to upload image; the format is not supported-error.

So you will need to save it as an image first.

Jannik Buhr
  • 1,788
  • 2
  • 11
  • 11
8

You can use mermaid-cli https://github.com/mermaidjs/mermaid.cli for generating diagrams. you can produce .svg, .png or .pdf file whatever you want.

Run following command after installing mermaid-cli

mmdc -i input.mmd -o output.png

here input.mmd is your mermaid file which contains

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

in your case.

7

Feb. 2022, Markdown pages now officially support Mermaid:
(Note: Aug. 2022, GitHub wiki pages too)

See:

Include diagrams in your Markdown files with Mermaid

Mermaid is a JavaScript based diagramming and charting tool that takes Markdown-inspired text definitions and creates diagrams dynamically in the browser. Maintained by Knut Sveidqvist, it supports a bunch of different common diagram types for software projects, including flowcharts, UML, Git graphs, user journey diagrams, and even the dreaded Gantt chart.

Working with Knut and also the wider community at CommonMark, we’ve rolled out a change that will allow you to create graphs inline using Mermaid syntax:

Example

Gist supports mermaid too.


Jan 2022, while GitHub Markdown does not yet support Mermaid, this is on the roadmap:

"Mermaid diagrams can be displayed within Markdown #372"

Just as language-specific code blocks can be added to Markdown, you'll be able to add a Mermaid diagram using a code block that specifies mermaid as its language identifier. For example:

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

The Markdown code block above uses Mermaid syntax to display this flowchart in the rendered Markdown:

https://user-images.githubusercontent.com/1767415/147986289-f8283c0b-aa5e-4381-bd69-876edeef12d9.png

And:

Another example from the Mermaid website shows how straightforward text can be used to create rich diagrams:

sequenceDiagram
   autonumber
   Alice->>John: Hello John, how are you?
   loop Healthcheck
       John->>John: Fight against hypochondria
   end
   Note right of John: Rational thoughts!
   John-->>Alice: Great!
   John->>Bob: How about you?
   Bob-->>John: Jolly good!

https://user-images.githubusercontent.com/1767415/148230142-63d64ec6-8157-4578-a4a8-a63e386b5cb9.png

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Though the ticket is marked as "Open", "Today, we're excited to add native support for Mermaid wherever Markdown is supported (e.g., issues, repositories, discussions, gists)." had me excited to be able to use this new feature, well, today. Should have been "we're excited to announce we've added this feature to our roadmap." – ksclarke Jan 20 '22 at 17:01
  • @ksclarke True. It is not quite there yet ;) – VonC Jan 20 '22 at 17:14
  • 1
    Arrived https://github.blog/2022-02-14-include-diagrams-markdown-files-mermaid/ – Kariem Feb 15 '22 at 10:24
  • 1
    @Kariem Thank you. I have updated the answer accordingly. – VonC Feb 15 '22 at 13:09
6

Another solution in moving your repository to gitlab. It natively supports mermaid in all markdown files.

Gitlab Markdown Tutorial

pktiuk
  • 235
  • 5
  • 10