vitepress-plugin-mermaid

repository·main·Indexed 41 days ago

https://github.com/emersonbottero/vitepress-plugin-mermaid

A VitePress plugin that adds Mermaid diagram support, allowing users to render diagrams, mindmaps, pie charts, and Gantt charts directly from markdown code blocks using the `mermaid` or `mmd` language identifiers. Version 2.0.17 features built-in dark theme support, per-page theme configuration via frontmatter, and the ability to incorporate images and links within diagrams.

Tokens
3.2K
Snippets
14
Records
18
Agent score
51%

What's inside vitepress-plugin-mermaid

  1. Key features of VitePress Plugin Mermaid

    main

    The plugin provides the following capabilities for VitePress users:

    • Mermaid, Diagram and charting tools: Create diagrams and visualizations using text and code.
    • Built-in Dark theme support: Automatically detects theme changes and switches between dark and light modes.
    • Image support: Since it utilizes the Mermaid API, you can include images within your diagrams.
    • Per-page theme configuration: You can set specific themes for individual pages using frontmatter options.
  2. Use images and links in Mermaid diagrams

    main

    Mermaid diagrams can incorporate HTML <img> tags for icons/images and click commands for interactivity.

    Image Requirements

    • Place image files in the public folder of your VitePress project.
    • If you have configured a base path in VitePress, you must prefix your image src paths with it.

    Interactivity

    • Use the click syntax to make nodes clickable.
    • Syntax: click [node_id] "[url]" "[tooltip]" [target] (where target can be _blank).

    Styling

    • You can use classDef to style nodes containing images (e.g., to remove backgrounds or borders) and apply those classes to specific nodes.
    graph LR;
        K([<img src='/vitepress-plugin-mermaid/K.png' width='60' >])-.->G((<img id='git' src='/vitepress-plugin-mermaid/Octocat.png' width='50' >));
        H([<img id='helm' src='/vitepress-plugin-mermaid/helm.png' width='60' >])-.->G
        G-->A;
        A(<img src='/vitepress-plugin-mermaid/argo-cd.png' width='60' >)-->D(<img src='/vitepress-plugin-mermaid/ocp.png' width='60' >);
    classDef img fill:none,color:transparent,stroke:none,borderRadius:50px
    class G,D,A,K,H,B img
    click K "https://kustomize.io/" _blank
    click G "http://www.github.com" "This is a link" _blank
  3. Apply custom styles to Mermaid diagrams using init directives

    main

    You can customize the appearance of Mermaid diagrams (such as themes and colors) by using the %%{init: {...}}%% directive at the beginning of your Mermaid code block. This allows you to pass configuration objects directly to the Mermaid engine within your VitePress documentation.

    Commonly used keys within the init object include:

    • theme: Sets the visual theme (e.g., 'base').
    • themeVariables: An object containing specific style overrides, such as 'primaryColor'.
    %%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#ff0000'}}}%%
    graph TD
      A[Christmas] --> B(Go shopping)
  4. Configure vitepress-plugin-mermaid in Vitepress

    main

    Wrap your existing Vitepress configuration with the withMermaid function from vitepress-plugin-mermaid. You can provide a mermaid configuration object to customize the mermaid rendering.

    Note on Themes: The plugin automatically detects dark themes by looking for the string dark in the body class name. Because dark mode is forced when a dark theme is detected, you should provide your mermaid configuration intended for dark mode inside the mermaid key. The configuration provided here will effectively serve as the theme for dark mode, while light mode uses default settings.

    // .vitepress/config.js
    import { withMermaid } from "vitepress-plugin-mermaid";
    
    export default withMermaid({
      // your existing vitepress config...
      mermaid: {
        // mermaidConfig !theme here works for light mode since dark theme is forced in dark mode
      },
      // ...
    });
  5. Create Mindmaps

    main

    The plugin supports Mermaid mindmap diagrams. You can define a root node and nested branches, including the use of icons via the ::icon() syntax.

    mindmap
      root((mindmap))
        Origins
          Long history
          ::icon(fa fa-book)
          Popularisation
            British popular psychology author Tony Buzan
        Research
          On effectiveness<br/>and features
          On Automatic creation
            Uses
                Creative techniques
                Strategic planning
                Argument mapping
        Tools
          Pen and paper
          Mermaid