mermaid-cli

repository·master·Indexed 26 days ago

https://github.com/mermaid-js/mermaid-cli

A command-line interface for Mermaid.js that converts Mermaid diagram definitions into SVG, PNG, or PDF files. It features the `mmdc` command for automating diagram generation in CI/CD pipelines, supports processing Markdown files to replace code blocks with images, and provides a Node.js API via the `run` and `renderMermaid` functions. It can be installed via npm or run using Docker and Podman.

Tokens
3.5K
Snippets
21
Records
28
Agent score
83%

What's inside @mermaid-js/mermaid-cli

  1. Install mermaid-cli

    master

    You can install mermaid-cli globally via npm to use the mmdc command anywhere in your terminal.

    Alternatively, if you encounter issues with global installation, you can install it locally in your project directory and call the binary from ./node_modules/.bin/mmdc.

    npm install -g @mermaid-js/mermaid-cli
  2. Run mermaid-cli via Docker or Podman

    master

    If you prefer not to install the package locally, you can use Docker or Podman. The container expects input files to be located in the /data directory inside the container.

    Docker usage:

    docker run --rm -u `id -u`:`id -g` -v /path/to/diagrams:/data minlag/mermaid-cli -i diagram.mmd

    Podman usage: Podman requires --userns keep-id to maintain user permissions and the :z flag on volume mounts for SELinux relabeling.

    podman run --userns keep-id --user ${UID} --rm -v /path/to/diagrams:/data:z ghcr.io/mermaid-js/mermaid-cli/mermaid-cli -i diagram.mmd
  3. Use an existing browser installation via --puppeteerConfigFile

    master

    If you want to use a pre-installed browser (like Chrome or Firefox) instead of the bundled Chromium, you can provide a JSON configuration file using the --puppeteerConfigFile flag.

    Note that Puppeteer is only guaranteed to work with the bundled Chromium; using an external executable is done at your own risk. You can check compatible Chrome versions via the Puppeteer repository if needed.

    Supported Puppeteer launch options via this config include:

    • executablePath: Path to your browser executable.
    • product: Specify if you want to use firefox instead of chrome.
    • timeout: Adjust the timeout before Puppeteer throws an error.
  4. Run Percy Visual Inspection tests from a fork PR

    master

    Because uploading to Percy requires a GitHub Secret token that forks cannot access, you must bring the PR branch into the main repository to trigger Percy visual inspection tests.

    Security Warning: Before proceeding, ensure the code contains no security exploits (e.g., commands like echo "$TOKEN" that might leak secrets).

    Follow these steps to run the tests:

    1. Create a new local branch for the PR work.
    2. Pull the code from the fork's repository.
    3. Verify that the commit hash matches the specific commit hash of the PR you reviewed using git log --max-count=1.
    4. Push the branch to the main mermaid-cli repository.

    Once pushed, CI will re-run, and Percy results will appear on the PR if both branches share the same commit hash.

    git switch -c "<NAME_OF_YOUR_BRANCH>"
    git pull "https://github.com/<NAME_OF_YOUR_USERNAME>/mermaid-cli.git" "<NAME_OF_YOUR_BRANCH>"
    # Make sure the commit hash matches the commit hash of the PR you reviewed
    git log --max-count=1
    git push git@github.com:mermaid-js/mermaid-cli.git "<NAME_OF_YOUR_BRANCH>"
  5. Animate SVG files with custom CSS

    master

    You can inline custom CSS into an SVG using the --cssFile option. This is useful for adding animations to your diagrams.

    Warning: If you want to override mermaid's themeCSS, it is recommended to add {"themeCSS": "..."} to your mermaid --configFile instead. You may also need to use !important to ensure your styles override mermaid's defaults.

    Warning: Inline CSS files may be blocked by the browser's Content-Security-Policy (CSP) depending on where the SVG is hosted.

    mmdc --input test-positive/flowchart1.mmd --cssFile test-positive/flowchart1.css -o docs/animated-flowchart.svg
  6. Fix 'Failed to launch chrome!' error on Linux by disabling the sandbox

    master

    If you encounter the error Failed to launch chrome! or Running as root without --no-sandbox is not supported when using mmdc on Linux, you can bypass the sandbox requirement by providing a Puppeteer configuration file.

    Note: The recommended long-term solutions are to avoid running as the root user and to ensure your Linux kernel is updated to the latest version. Use the --no-sandbox workaround only if you cannot follow those recommendations.

    {
        "args": ["--no-sandbox"]
    }
  7. Fix EACCES permission denied error in Docker or Podman

    master

    When running the mermaid-cli Docker image, you may encounter an EACCES: permission denied error when attempting to write output files (e.g., .svg). This typically happens because the container process does not have permission to write to the mounted volume.

    To resolve this, use the -u flag to run the container with your current user's UID (User ID).

    docker run -u $UID -it --rm -v "$(pwd)":data minlag/mermaid-cli -i /data/diagram.mmd
  8. Transform Markdown files containing Mermaid diagrams

    master

    The CLI can process a Markdown file, find all mermaid code blocks, convert them into SVG files, and replace the code blocks with image references in a new Markdown file.

    mmdc -i readme.template.md -o readme.md