question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When creating markdown pages I want to add mermaid-syntax diagrams so that I can display nicely rendered diagrams

https://mermaid-js.github.io/mermaid/#/

Acceptance

```mermaid
graph TD

a --> b
\```

Tasks

  • research options e.g. existing remark plugins
  • Select one to try
  • Try it
  • Deploy
  • Document

Notes

We want a solution that renders client side (we do not want to render svg and insert)

Options

  • https://github.com/temando/remark-mermaid 2022-07-26 this looks pretty promising for what we want 2022-08-02 summary: adding as a plugin in remark just throws error as seen in notes below
  • https://github.com/sjwall/mdx-mermaid - Plug and play Mermaid in MDX 2022-08-02 summary:
    • adding as a plugin in remark just outputs the component as a string.
    • Adding the component in MDX components client side ie. Pre.js is complicated as rehype-prism-plus converts the graph to span elements and <Mermaid char={...} /> needs the graph as a string.
  • https://github.com/remcohaszing/remark-mermaidjs - A remark plugin to render mermaid diagrams using puppeteer 2022-08-02 summary: not considering as puppeteer is a large dependency even if it works without much configuration needed

remark-mermaid

  • throwing errors: Expected usable value, not 'undefined' Looks like it’s trying to parse wrong values.

Screen Shot 2022-07-26 at 6 00 07 PM

mdx-mermaid with remark-code-extra (UPDATE)

Using this remark plugin - https://github.com/s0/remark-code-extra - to replace pre tag elements created by rehype-prism-plus, I have managed to make mdx-mermaid work nicely with it. See screenshots below for result and modified code in contentlayer.config.js

Brief summary of modified code:

Transforming the nodes for <Pre> elements and removing the ones created by rehype-prism-plus …

  • Added the remark plugin in contentlayer with following configuration within it:
    • create a new pre tag element with just the string value as child and a new className .code-mermaid
    • remove the previous pre tag element created by rehype-prism-plus
  • Modified the Pre.js component to check for the new className (code-mermaid) from props and if so, use Mermaid component ie. <Mermaid chart={children} />

Screen Shot 2022-08-02 at 6 07 07 PM

The remark-code-extra plugin in contentlayer.config.js looks like this:

Screen Shot 2022-08-02 at 6 18 49 PM

mdx-mermaid

  • adding mdx-mermaid as a remark plugin just outputs a string as seen below. Probably happening since import statements in md are not working/resolved.

Screen Shot 2022-07-27 at 5 18 24 AM

using mermaid component from mdx-mermaid in Pre.js component works after removing rehype-prism-plus plugin Screen Shot 2022-07-27 at 5 24 20 AM

Why rehype-prism-plus conflicts:

rehype prism plus modifies all the code blocks including mermaid. for example below markdown

graph LR
     Start --> Stop

would generate individual span elements for each line

<span>graph LR</span>
<span>Start --> Stop</span>

Since mdx-mermaid component takes only a string <Mermaid chart={'graph LR ....'} />, we have to map each span element and join them as strings. This gets complex when there are elements within.

A better approach would be to somehow make rehype-prism-plus to discard modifying the language-mermaid code block.

remark-mermaidjs

https://github.com/remcohaszing/remark-mermaidjs

  • works and probably should go with this …
  • needs some style fixes Screen Shot 2022-07-26 at 6 12 21 PM

Mermaidjs

https://mermaid-js.github.io/mermaid/#/

  • If using this option we have to:
    • configure Pre.js component used in MDXComponents to make this work
    • Also have to do more nested stuff since rehype-prism-plus converts code blocks to components
      if (/^language-mermaid/.test(children.props.className.toLocaleLowerCase())) {
        useEffect(() => mermaid.initialize({ startOnLoad: true }), [])
        return <div className="mermaid my-12">{children.props.children.map(el => el.props.children).join().replace(/,/g, "")} </div>
      }
      
    • Additional configuration for color code support required (not implemented)

Screen Shot 2022-07-26 at 6 21 59 PM

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
rufuspollockcommented, Aug 8, 2022

@khalilcodes if this is working and relatively clean let’s go with this, perhaps documenting clearly here the approach and what improvements we would like in future (with perhaps some comments in code e.g. in contentlayer segment - that can also ref here - explaining that this is for mermaid support). That way 6m or a year from now when we look at the code we can understand why we did what we did and how we would improve or replace it if a new remark plugin comes out.

My other question is whether we want a standalone remark-mermaid plugin like we had with remark-wiki-link-plus?

1reaction
rufuspollockcommented, Aug 2, 2022

@khalilcodes thanks. I just refactored notes section to be a bit clearer. Could you update the bulleted list at the top of the notes with a summary of current evaluation and recommendation including effort to make it work and reliability (detail can be left out there and can go below in dedicated section for each option)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Include diagrams in your Markdown files with Mermaid
Maintained by Knut Sveidqvist, it supports a bunch of different common diagram types for software projects, including flowcharts, UML, Git ...
Read more >
Mermaid | Diagramming and charting tool
JavaScript based diagramming and charting tool that renders Markdown-inspired text definitions to create and modify diagrams dynamically. Get Started · View on ...
Read more >
Markdown Preview Mermaid Support
Extension for Visual Studio Code - Adds Mermaid diagram and flowchart support to VS Code's builtin markdown preview.
Read more >
Markdown Preview Mermaid Support - GitHub
Markdown Preview Mermaid Support. Adds Mermaid diagram and flowchart support to VS Code's builtin Markdown preview and to Markdown cells in notebooks.
Read more >
Mermaid.js support in Markdown files - GoLand Guide
Mermaid.js support in Markdown files ... How to use: Turn on the Mermaid option in Settings/Preferences | Languages & Frameworks | Markdown under ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found