17

MermaidJS was my favourite cool thing in 2018!

But I can't find a way to draw MermaidJS graph or sequenceDiagram on Stack Overflow.

I've seen many Markdown tools such as StackEdit, Typora, Markdown Plus and Markdown Monster support MermaidJS, and I feel a little bit restricted not having the luxury of this cool technology on Stack Exchange sites.

Adding <!-- language: mermaid-js --> or triple back-ticks (```mermaid) didn't work so I assumed it is not supported.

I think this would be very useful in asking design-related questions, or in explaining complex scenarios.

So will it ever be supported? if not, what are the alternatives?

1
  • 4
    When you're making a feature request, you need to have more than vague "I think" statements. Why would it be useful exactly? Where? Do you have examples of questions or answers that could make use of it, and across SE sites? I also don't think SE supports even the full basic markdown. Commented Jan 1, 2019 at 0:20

3 Answers 3

24

It doesn't but if a site has Stack Snippets enabled you can use that functionality to include your diagram.

The mermaid documentation gives an example of using Mermaid in a way that's compatible with Snippets.

The JavaScript snippet section to call the mermaidAPI in a separate script tag may not be needed.

The html snippet section must include:

  • mermaid address using a script tag in the src section
  • mermaid code/ graph definition declared inside a <pre> with class=mermaid

mermaid.initialize({ startOnLoad: true });
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/font-awesome.min.css">
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>

<pre class="mermaid">
  graph LR
    A --- B
    B -->|invalid| C[fa:fa-ban forbidden]
    B -->|valid| D(fa:fa-spinner fa:fa-spin);
</pre>

The only downside is that visitors of your post have to click the Run code snippet button to actually see your diagram.

3
  • @flywire they reshuffled content, I updated the link.
    – rene Mod
    Commented Jun 8, 2022 at 16:37
  • Your version of mermaid did not support flowchart so pasted the bottom block into html using non-version specific <script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script> and did not need the top block in javascript. For the purpose of the question It was easier not to indent the mermaid code. Can I edit this into your answer?
    – flywire
    Commented Jun 9, 2022 at 6:16
  • @flywire sure, go ahead
    – rene Mod
    Commented Jun 9, 2022 at 16:29
16

To indirectly post your mermaid diagram, you can edit online with https://mermaid.live/.

You can then share your chart with a link that you can use in markdown (click on the picture to see the code and edit)

[![Node A connects to Node B, which splits into a "forbidden" node and a node with a spinner](https://mermaid.ink/img/eyJjb2RlIjoiICAgIGdyYXBoIExSXG4gICAgICBBIC0tLSBCXG4gICAgICBCLS0-Q1tmYTpmYS1iYW4gZm9yYmlkZGVuXVxuICAgICAgQi0tPkQoZmE6ZmEtc3Bpbm5lcilcbiAgICAgICIsIm1lcm1haWQiOnsidGhlbWUiOiJkZWZhdWx0In0sInVwZGF0ZUVkaXRvciI6ZmFsc2UsImF1dG9TeW5jIjp0cnVlLCJ1cGRhdGVEaWFncmFtIjpmYWxzZX0)](https://mermaid.live/edit#eyJjb2RlIjoiICAgIGdyYXBoIExSXG4gICAgICBBIC0tLSBCXG4gICAgICBCLS0-Q1tmYTpmYS1iYW4gZm9yYmlkZGVuXVxuICAgICAgQi0tPkQoZmE6ZmEtc3Bpbm5lcilcbiAgICAgICIsIm1lcm1haWQiOiJ7XG4gIFwidGhlbWVcIjogXCJkZWZhdWx0XCJcbn0iLCJ1cGRhdGVFZGl0b3IiOmZhbHNlLCJhdXRvU3luYyI6dHJ1ZSwidXBkYXRlRGlhZ3JhbSI6ZmFsc2V9)

Node A connects to Node B, which splits into a "forbidden" node and a node with a spinner

2
  • The other answer from rene uses the built-in snippet and does not require third-parties. But this method has a live preview and doesn't require an extra click, so I'm marking this as the answer.
    – Bizhan
    Commented Dec 16, 2021 at 12:55
  • 1
    To generate the markdown link go to the Actions link in the left-hand pane of the Mermaid Live Editor and hit the 'Copy Markdown' button.
    – Joman68
    Commented May 30, 2023 at 5:27
9

As far as I know, no. It might be shiny but amusingly the preferred way to do graphs and diagrams on SE is using ASCII and code blocks.

  +--------------------+
  |                    |
  |                    |
  |   Computer         |
  |                    |    <--------------------------+
  |                    |          IF Man TOUCHES Computer
  |                    |          THEN Dog bites Man   |
  +--------+-----------+          ELSE SLEEP           |
           ^                                           |
           |                                           |
           X   No touchie!                             |
           |                                           |
           |                                           +
+----------+--------+
|                   |                            +----------------+
|                   |           Feeds dog        |                |
| Man               | +----------------------->  |                |
|                   |                            |    Dog         |
|                   |                            |                |
+-------------------+                            +----------------+

I use ASCII flow for this but there's other ways to do it

5
  • 6
    "the preferred way to do graphs and diagrams on SE is using ASCII and code blocks": Preferred? Really?! I doubt so: do you have any statistics across the sites? Commented Jan 1, 2019 at 14:02
  • Nope. Or posting an image :p
    – Journeyman Geek Mod
    Commented Jan 1, 2019 at 14:03
  • 4
    This should be avoided, since it creates a very confusing experience for screen readers (and isn't responsive for mobile users). I wrote about what an ASCII table sounds like, so just imagine that but even less rhyme or reason, since there's no way to make sense of this with a screen reader. It would be preferable to use an image with appropriate alt text, which doesn't create a bunch of random chatter in the middle of the post like this does.
    – Laurel
    Commented Jun 8, 2022 at 17:21
  • Wow! That's amazing tool! Commented May 3, 2023 at 21:43
  • Good point, this tool's "Extended ASCII" doestn't render well on mobile, bit Basic ASCII is beink rendered fine. But issue about scrren readers raised by @Laurel is conserning :-( Commented May 3, 2023 at 23:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .