vscode-markdown-mermaid

repository·master·Indexed 21 days ago

https://github.com/mjbvz/vscode-markdown-mermaid

Adds Mermaid diagram and flowchart support to VS Code's built-in Markdown preview and Markdown cells in notebooks. Version 1.32.1 supports Mermaid 11.12.0 and includes features for zooming, panning, and resizing complex diagrams, as well as theme configuration for light and dark modes.

Tokens
4.1K
Snippets
15
Records
24
Agent score
75%

What's inside vscode-markdown-mermaid

  1. Navigate and explore Mermaid diagrams

    master

    Large or complex diagrams can be explored using zooming, panning, and resizing controls. Navigation controls typically appear when you hover over or focus on a diagram.

    Zooming

    • Controls: Use the + and - buttons in the navigation overlay.
    • Mouse/Trackpad: Hold <kbd>alt</kbd> (<kbd>option</kbd> on Mac) and use the scroll wheel, or use a trackpad pinch gesture.
    • Keyboard/Click: Alt+click to zoom in, Alt+Shift+click to zoom out.
    • Reset: Click the reset button to restore the original zoom level and position.

    Panning

    • Mouse: Hold <kbd>alt</kbd> (<kbd>option</kbd> on Mac) and click-and-drag to pan.
    • Pan Mode: Click the pan mode button in the navigation controls to enable click-and-drag panning without needing to hold the <kbd>alt</kbd> key.

    Resizing

    • Vertical Resizing: Drag the bottom edge of a diagram to adjust its height. This is useful when combined with markdown-mermaid.maxHeight.
  2. Use custom CSS in Markdown Preview

    master

    You can use VS Code's built-in markdown.styles setting to inject custom CSS (like Font Awesome) into the Markdown preview, which can then be used within Mermaid diagrams.

    // In settings.json
    "markdown.styles": [
        "https://use.fontawesome.com/releases/v5.7.1/css/all.css"
    ]
    
    // In your Markdown file
    ```mermaid
    graph LR
        fa:fa-check-->fa:fa-coffee
  3. Use Mermaid diagrams in Markdown preview

    master

    You can render Mermaid diagrams in VS Code's built-in Markdown preview and in Markdown cells within notebooks. This extension supports Mermaid version 11.12.0 and allows for the use of Iconify icons (MDI and logos) within diagrams.

    To create a diagram, use either a fenced mermaid code block or a ::: mermaid block.

    ```mermaid
    graph TD;
        A-->B;
        A-->C;
        B-->D;
        C-->D;

    OR

    ::: mermaid graph TD; A-->B; A-->C; B-->D; C-->D; :::

  4. Configure Mermaid themes for Light and Dark modes

    master

    You can specify which Mermaid theme to use depending on your VS Code color theme. Note that these settings are currently not supported in notebooks.

    Supported theme values for both settings: base, forest, dark, default, neutral.

  5. How the Markdown preview Mermaid support initializes

    master

    The Mermaid support in the Markdown preview is initialized automatically when the webview loads or when the content is updated via the vscode.markdown.updateContent event.

    During initialization, the extension:

    1. Aborts any ongoing rendering processes using an AbortController.
    2. Loads extension configuration via loadExtensionConfig().
    3. Configures the Mermaid theme based on the VS Code color theme (detecting vscode-dark or vscode-high-contrast on document.body).
    4. Initializes Mermaid with the loaded configuration.
    5. Registers Mermaid addons.
    6. Scans the document body to render Mermaid blocks using renderMermaidBlocksInElement.
    7. Manages diagram states (like zoom/pan) via a DiagramManager to ensure persistence across updates.
  6. Configure diagram display and resizing

    master

    Control how diagrams are sized and how navigation controls are shown.

    • markdown-mermaid.resizable: (Default: true) Allows dragging the bottom edge of a diagram to adjust height.
    • markdown-mermaid.maxHeight: Sets a maximum height for diagrams. Accepts pixel numbers (e.g., 400) or CSS values (e.g., 80vh).
    • markdown-mermaid.controls.show: Determines when navigation control buttons appear. Supported values: never, onHoverOrFocus (default), always.
  7. Configure mouse navigation behavior

    master

    The markdown-mermaid.mouseNavigation.enabled setting determines how the <kbd>alt</kbd> key interacts with panning and zooming.

    • always: Click and drag always pans without a modifier key.
    • alt: (Default) Click and drag only pans when holding <kbd>alt</kbd> (<kbd>option</kbd> on Mac).
    • never: Disables mouse-based panning (navigation controls and pinch-to-zoom still work).
  8. Use the tidy-tree layout engine in Mermaid diagrams

    master

    You can enable the tidy-tree layout engine for Mermaid diagrams by adding a YAML configuration block at the top of your Mermaid code block. This is particularly useful for mindmap diagrams to control how nodes are laid out.

    To use it, include the following configuration header:

    ---
    config:
      layout: tidy-tree
    ---
    ---
    config:
      layout: tidy-tree
    ---
    mindmap
    root((mindmap is a long thing))
      A
      B
      C
      D
  9. Configure Mermaid extension settings

    master

    The MermaidExtensionConfig interface defines the available configuration options for the extension. These settings control themes, interaction modes, and layout constraints for Mermaid diagrams in the Markdown preview.

    interface MermaidExtensionConfig {
        readonly darkModeTheme: string;
        readonly lightModeTheme: string;
        readonly maxTextSize: number;
        readonly clickDrag: ClickDragMode;
        readonly showControls: ShowControlsMode;
        readonly resizable: boolean;
        readonly maxHeight: string;
    }
  10. Configure markdown-mermaid settings

    master

    The extension uses the markdown-mermaid configuration section in VS Code settings. You can customize how Mermaid diagrams are rendered and how they behave in the Markdown preview.

    Available configuration keys include:

    • darkModeTheme: The Mermaid theme used when VS Code is in dark mode. Valid values: base, forest, dark, default, neutral. Defaults to default if an invalid value is provided.
    • lightModeTheme: The Mermaid theme used when VS Code is in light mode. Valid values: base, forest, dark, default, neutral. Defaults to default if an invalid value is provided.
    • maxTextSize: A number controlling the maximum text size in diagrams.
    • mouseNavigation.enabled: Controls the click-and-drag behavior. Values correspond to ClickDragMode (e.g., Alt is a common default).
    • controls.show: Controls when diagram controls (like zoom/pan) are visible. Values correspond to ShowControlsMode (e.g., OnHoverOrFocus).
    • resizable: A boolean determining if diagrams can be resized. Defaults to true.
    • maxHeight: A string defining the maximum height for diagrams (e.g., 500px).
  11. Reference: markdown-mermaid settings

    master

    List of available configuration settings for the Mermaid extension.

    {
      "markdown-mermaid.lightModeTheme": "base | forest | dark | default | neutral",
      "markdown-mermaid.darkModeTheme": "base | forest | dark | default | neutral",
      "markdown-mermaid.languages": ["mermaid"],
      "markdown-mermaid.mouseNavigation.enabled": "always | alt | never",
      "markdown-mermaid.controls.show": "never | onHoverOrFocus | always",
      "markdown-mermaid.resizable": true,
      "markdown-mermaid.maxHeight": "number | CSS value",
      "markdown-mermaid.maxTextSize": 50000
    }