27

I use mermaid for basic diagram rendering within my markdown documentation aside my code. I found this online editor useful to edit this while having a preview. This proposes to change theme (default and forest works).

How can I set theme when I copy paste this into my markdown document ?

RandomCoder
  • 6,606
  • 6
  • 22
  • 28

7 Answers7

25

I was looking for the same as you, so after taking a look and digging in Mermaid source code could find these code snippet:

for (const themeName of ['default', 'forest', 'dark', 'neutral']) {
  themes[themeName] = require(`./themes/${themeName}/index.scss`)
}

So, after testing in their editor, these themes are working fine:

  • default
  • forest
  • dark
  • neutral

You can find their themes here in case you want to build your custom themes: https://github.com/knsv/mermaid/tree/master/src/themes

If you go to Mermaid online editor, you can change the theme to those mentioned above:

enter image description here

Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
24

It is possible to change the theme in the Markdown document with a directive for each figure.

Here is an example (tested with Material for Mkdocs):

This graph uses the forest theme:

``` mermaid
%%{init: {'theme': 'forest', "flowchart" : { "curve" : "basis" } } }%%
graph LR
A[Foo] --> B[Bar]
B --> A
```

That one uses the neutral theme:

``` mermaid
%%{init: {'theme': 'neutral' } }%%
graph LR
A[Foo] --> B[Bar]
B --> C[Baz]
```

The result will be:

Resulting diagrams

Edouard Thiel
  • 5,878
  • 25
  • 33
5

The theme is embedded when rendering SVG and it seems currently there is no support for custom theme setting when using markdown.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
terry0201
  • 51
  • 1
  • 3
  • 3
    Sadly this seems to be the case, the default Theme is the ugliest of all available themes. – phifi Jun 19 '19 at 13:54
  • 1
    @phifi: See my comment under https://stackoverflow.com/a/57274389/6419007 I use the last mermaid version with Markdown and can choose the theme ('neutral' fits well with the rest of our MkDocs material doc) – Eric Duminil Jul 14 '20 at 12:22
5

As far as I know, your only chance to set the theme is on initialization:

 mermaid.initialize({
      theme: 'forest',
      logLevel: 1,
      flowchart: { curve: 'linear' },
    });
3

Don't know what you use to produce output from your Markdown -- I use MkDocs with Material, and added Mermaid support like explained here.

After some trial-and-error configurations I found out that using Cloudflare's CDN, you can include an older version of MermaidJS with another CSS. This way, I was able to render the diagram using the neutral style:

markdown_extensions:
  - pymdownx.superfences:
      custom_fences:
        - name: mermaid
          class: mermaid
          format: !!python/name:pymdownx.superfences.fence_div_format

extra_css:
  - https://cdnjs.cloudflare.com/ajax/libs/mermaid/7.0.9/mermaid.neutral.css
extra_javascript:
  - https://cdnjs.cloudflare.com/ajax/libs/mermaid/7.0.9/mermaid.min.js
Bernd
  • 31
  • 1
  • 1
    I ended up downloading the last version (https://unpkg.com/mermaid@8.4.3/dist/mermaid.min.js), looked for `theme:"default"`, replaced it with `theme:"forest"` and saved the js locally, e.g. to `js/mermaid.min.js`. It worked fine and I can use the last version. – Eric Duminil Dec 06 '19 at 23:20
0

If you copy the stylesheet detailed in the documentation, modify it and add !important after every CSS property you change, it will take precedence over the inline styles Mermaid copies into the SVG. Far from ideal, but neither is the copying of styles Mermaid does, so this is a "fighting fire with fire" solution.

Asbjørn Ulsberg
  • 8,721
  • 3
  • 45
  • 61
0

You didn't say what your tool chain or targeted output format is.

That being said, you can use Pandoc with mermaid-filter to export a Markdown file containing a Mermaid chart to a couple of formats.

Plus, with mermaid-filter you can select the theme using the fenced code block's key-value attributes options

E.g. let /tmp/example.md contain the following

```{.mermaid theme=forest}
graph TD
A[Christmas] -->|Get money| B(Go shopping)
B --> C{Let me think}
C -->|One| D[Laptop]
C -->|Two| E[iPhone]
C -->|Three| F[fa:fa-car Car]
```

then

pandoc --filter mermaid-filter --from markdown --to html /tmp/example.md > /tmp/example.html

will produce the an HTML page /tmp/example.html that looks as follows

example mermaid chart

FK82
  • 4,907
  • 4
  • 29
  • 42