readmeio / markdown

ReadMe's flavored Markdown parser and React-based rendering engine.
https://rdmd.readme.io
ISC License
34 stars 9 forks source link

Mermaid.js Support #700

Open garrett-wade opened 1 year ago

garrett-wade commented 1 year ago

Our engineers are looking to create docs with diagrams and would like to use mermaid js (https://mermaid.js.org/). Is there any plans to add mermaid support to readme's markdorn engine or an alternative?

kellyjosephprice commented 1 year ago

That would be really awesome. I think our current plan for adding more custom components, will first be adding MDX support. Presumably at that point, you'll be able to import a Mermaid component.

## Potential Example

<Mermaid>
  graph
  ...
</Mermaid>
kellyjosephprice commented 1 year ago

For a workaround now, you could write a custom transformer:

import * as rdmd from '@readme/markdown'
import mermaid from 'mermaid'

const doc = `
~~~mermaid
graph
  ...
~~~
`

const traverse = (node, fn, parent = null, index = null) => {
  fn(node, parent, index)

  if ('children' in node) {
    node.children.forEach((child, idx => traverse(child, fn, node, idx))
  }
}

const mdast = rdmd.mdast(doc)

traverse(mdast, async (node, parent, index) => {
  if (node.type === 'code' && node.lang === 'mermaid') {
    const { svg } = await mermaid.render('graphDiv', node.value);

    parent.children[index] = {
      type: 'html',
      value: svg,
    }
  }
})

console.log(rdmd.md(mdast))
SethAngell commented 1 year ago

Hey! Is this something we could potentially implement as a workaround within our developer hub hosted on readme? Super excited about the potential for mdx support, but wanted to start using mermaid.js in our docs as soon as we can. Thanks!