Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OOTB Mermaid code block #1258

Closed
amimas opened this issue Mar 4, 2019 · 30 comments · Fixed by #7490
Closed

OOTB Mermaid code block #1258

amimas opened this issue Mar 4, 2019 · 30 comments · Fixed by #7490
Labels
feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. help wanted Asking for outside help and/or contributions to this particular issue or PR. status: accepting pr This issue has been accepted, and we are looking for community contributors to implement this

Comments

@amimas
Copy link

amimas commented Mar 4, 2019

🚀 Feature

mermaidjs seems to be a pretty neat tool that allows drawing diagrams using markdown. It'd be nice if Docusaurus can be integrated with that.

Have you read the Contributing Guidelines on issues?

yes

Motivation

Most documentation needs to show diagrams. The current method involves using some external tool and then create an image (i.e. png, svg, jpg, etc). You'll need to manage those diagrams yourself separate from the content.

Pitch

Draw diagrams in the markdown along with the actual content where they are going to be used. This will make it a lot easier to maintain document (contents + diagrams) all in one place.

@endiliey endiliey added the feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. label Mar 5, 2019
@leonardfactory
Copy link

Non sure if the plugin is still updated but there is remarkable-mermaid. I’m interested in this too but I haven’t found the time to test it

@endiliey

This comment has been minimized.

@endiliey endiliey reopened this Mar 14, 2019
@hassanfarid
Copy link
Contributor

This should be really good addition to support product technical documentation in Docusaurus.

@endiliey
Copy link
Contributor

After much consideration, this is indeed best left to markdown plugin

https://github.com/temando/remark-mermaid on v2
and remarkable-mermaid on v1

@ntsd
Copy link

ntsd commented Sep 5, 2019

@endiliey

Hey did anyone did it on version 2 yet?

Maybe I can help contribute?

@eugenenelou
Copy link

Hey did anyone did it on version 1?
remarkable-mermaid is not even on npm and I cannot make it work as is.

@ericfourrier
Copy link

any update on this ?

@18dew
Copy link

18dew commented Dec 15, 2019

Nope i dont think so any one has writen any code over this yet ...

@chmelevskij
Copy link

chmelevskij commented Dec 31, 2019

With the current version it's pretty straightforward to integrate:

// siteConfig.js
    scripts: [
        'https://cdnjs.cloudflare.com/ajax/libs/mermaid/8.4.4/mermaid.min.js',
        '/init.js',
    ],
    markdownPlugins: [ (md) => {
        md.renderer.rules.fence_custom.mermaid = (tokens, idx, options, env, instance) => {
            return `<div class="mermaid">${tokens[idx].content}</div>`;
        };
    }],

Then in init simply

/* eslint-disable */
window.addEventListener('load', function() {
    mermaid.initialize({startOnLoad:true});
});

Usage:

```mermaid
graph TB
    a-->b'

facebook-github-bot pushed a commit to magma/magma that referenced this issue Feb 3, 2020
Summary:
Pull Request resolved: https://github.com/facebookincubator/symphony/pull/263

- Followed this thread in order to add mermaid (graphs on docuzauros) support - facebook/docusaurus#1258
- https://mermaid-js.github.io/mermaid/#/

Reviewed By: vdorfman

Differential Revision: D19688738

fbshipit-source-id: 35a27fe17cdad5456054edd7b6865f82dad09ccf
@bodiam
Copy link

bodiam commented Feb 28, 2020

Hi @chmelevskij , thanks for sharing. Is there any way to get this to work on Docusaurus 2?

@ghost
Copy link

ghost commented Mar 4, 2020

Hi @chmelevskij , thanks for sharing. Is there any way to get this to work on Docusaurus 2?

in v2, you can make your own Component an import it into your markdown.

  1. install mermaid yarn add mermaid
  2. create file in your src/theme/Mermaid.js
  3. Mermaid.js
import React, { useEffect } from "react";
import mermaid from "mermaid";

mermaid.initialize({
	startOnLoad: true
});

const Mermaid = ({ chart }) => {
	useEffect(() => {
		mermaid.contentLoaded();
	}, []);
	return <div className="mermaid">{chart}</div>;
};

export default Mermaid;
  1. doc1.md
import Mermaid from '@theme/Mermaid';
...
<Mermaid chart={`
	graph LR;
		A-->B;
		B-->C;
		B-->D[plop lanflz eknlzeknfz];
`}/>
...

@bodiam
Copy link

bodiam commented Mar 8, 2020

image

Thank you so much!! That's easier than I thought!

@mbykovskyy
Copy link

After much consideration, this is indeed best left to markdown plugin

https://github.com/temando/remark-mermaid on v2
and remarkable-mermaid on v1

@endiliey, is there away to specify a data.destinationDir on a currently processed MD vFile? Without such configuration remark-mermaid writes SVGs into the same directory as the MD file.

@lucaslz2020
Copy link

lucaslz2020 commented Aug 7, 2021

Hi @chmelevskij , thanks for sharing. Is there any way to get this to work on Docusaurus 2?

in v2, you can make your own Component an import it into your markdown.

  1. install mermaid yarn add mermaid
  2. create file in your src/theme/Mermaid.js
  3. Mermaid.js
import React, { useEffect } from "react";
import mermaid from "mermaid";

mermaid.initialize({
	startOnLoad: true
});

const Mermaid = ({ chart }) => {
	useEffect(() => {
		mermaid.contentLoaded();
	}, []);
	return <div className="mermaid">{chart}</div>;
};

export default Mermaid;
  1. doc1.md
import Mermaid from '@theme/Mermaid';
...
<Mermaid chart={`
	graph LR;
		A-->B;
		B-->C;
		B-->D[plop lanflz eknlzeknfz];
`}/>
...

This way is still more troublesome.

expect:

graph
A --> B

@sjwall
Copy link
Contributor

sjwall commented Aug 7, 2021

I have taken the Mermaid component example given above and bundled it in a package mdx-mermaid

The package also includes a remark plugin so that it can be used with code blocks

There are setup instructions and examples here

If you run into problems, please raise an issue on the github repo

@slorber
Copy link
Collaborator

slorber commented Aug 10, 2021

Thanks for creating a package @sjwall ! btw you can add it there: https://docusaurus.io/community/resources

@emerxom
Copy link

emerxom commented Nov 4, 2021

I have taken the Mermaid component example given above and bundled it in a package mdx-mermaid

The package also includes a remark plugin so that it can be used with code blocks

There are setup instructions and examples here

If you run into problems, please raise an issue on the github repo

@sjwall
Is it possible to set the theme dark when it is used in docusaurus? In a way it would update when the theme is switched on the interface

@slorber
Copy link
Collaborator

slorber commented Jan 12, 2022

Thanks 👍

Let's wait and see then ;)

@felipecrs
Copy link
Contributor

It's live now:

https://github.blog/2022-02-14-include-diagrams-markdown-files-mermaid/

Native support for Mermaid in Docusaurus would be nice. :)

@Josh-Cena
Copy link
Collaborator

Josh-Cena commented Feb 17, 2022

Has anyone experimented with remark-mermaid and checked if it works with Docusaurus? Does it have to be built-in? I'm not against making it OOTB, but better see if there has been community work being done already.

@antonk52
Copy link
Contributor

@Josh-Cena I could not get it working out of the box. However, I was able to get mermaid work in a GFM compatible way. It works by introducing a theme component Mermaid(like the one shown above) that is also in MDXComponents. And adding a custom remark plugin. It does not handle all cases but works as a proof of concept.

custom remark plugin
function remarkMermaid() {
  return function transformer(root: any) {
    root.children = root.children.map(x => {
        if (x.type === 'code' && x.lang === 'mermaid') {
          const loc = {
            start: {line: x.position.start.line, column: x.position.start.column},
            end: {line: x.position.end.line, column: x.position.end.column},
          };
          const start = x.position.start.offset;
          const end = x.position.end.offset;
          const range = [start, end];
          const mermaidNode = {
            type: 'mdxJsxFlowElement',
            name: 'Mermaid',
            data: {_xdmExplicitJsx: true},
            children: [],
            attributes: [{
                type: 'mdxJsxAttribute',
                name: 'chart',
                value: {
                  type: 'mdxJsxAttributeValueExpression',
                  value: ['`\n', x.value, '`'].join('\n'),
                  data: {
                    estree: {
                      body: [{
                        type: "ExpressionStatement",
                        start,
                        range,
                        loc,
                        expression: {
                          loc,
                          range,
                          start,
                          end,
                          type: "Literal",
                          value: x.value,
                          raw: ['`\n', x.value, '`'].join('\n'),
                        },
                      }],
                      comments: [],
                      end,
                      loc,
                      range,
                      sourceType: "module",
                      start,
                      type: "Program",
                    },
                  },
                },
                position: x.position,
              }],
          };
          return mermaidNode;
        }

        return x;
    });
  };
};

@Josh-Cena
Copy link
Collaborator

Going to re-open. I think it would be very helpful to have this out-of-the-box.

@Josh-Cena Josh-Cena reopened this Mar 30, 2022
@Josh-Cena Josh-Cena changed the title Integrate with mermaidjs OOTB Mermaid code block Mar 30, 2022
@Josh-Cena Josh-Cena added help wanted Asking for outside help and/or contributions to this particular issue or PR. status: accepting pr This issue has been accepted, and we are looking for community contributors to implement this labels May 5, 2022
@Josh-Cena
Copy link
Collaborator

VS Code has also added support for Mermaid: https://twitter.com/mattbierner/status/1522003140777701376

Many Markdown renderers also support it. I see a general trend to make this available out-of-the-box. If anyone wants to try their hands at a remark plugin, PRs are welcome.

@garyng
Copy link

garyng commented May 5, 2022

I've been using mdx-mermaid mentioned in #1258 (comment) - it works fine for me!

@sjwall
Copy link
Contributor

sjwall commented May 24, 2022

@Josh-Cena I am looking at merging in mdx-mermaid, should hopefully have a PR together this week

@jpadams
Copy link

jpadams commented Jun 15, 2022

Super excited to see this land! Thanks for all of the hard work!

nweldev pushed a commit to blindnet-io/blindnet.dev that referenced this issue Aug 18, 2022
use a custom component to render mermaid diagrams
mdx-mermaid didn't work, but docusaurus should soon integrate this
feature natively

see facebook/docusaurus#1258
nweldev pushed a commit to blindnet-io/blindnet.dev that referenced this issue Aug 18, 2022
use a custom component to render mermaid diagrams
mdx-mermaid didn't work, but docusaurus should soon integrate this
feature natively

see facebook/docusaurus#1258
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. help wanted Asking for outside help and/or contributions to this particular issue or PR. status: accepting pr This issue has been accepted, and we are looking for community contributors to implement this
Projects
None yet
Development

Successfully merging a pull request may close this issue.