Skip to content

Instantly share code, notes, and snippets.

@ryanlewis
Created March 5, 2026 23:10
Show Gist options
  • Select an option

  • Save ryanlewis/84e5b5643fa2261dd7181db286e28891 to your computer and use it in GitHub Desktop.

Select an option

Save ryanlewis/84e5b5643fa2261dd7181db286e28891 to your computer and use it in GitHub Desktop.
mermaid-clip: Render Mermaid diagrams from clipboard to PNG image

mermaid-clip

Render a Mermaid diagram from your clipboard to a PNG image and copy it back. Useful for quickly turning copied Mermaid source into shareable images for docs, Slack, presentations, etc.

macOS only — uses pbpaste and osascript for clipboard access.

Dependencies

brew install mermaid-cli

Installation

Shell script

Download mermaid-clip.sh and put it somewhere on your $PATH:

cp mermaid-clip.sh /usr/local/bin/mermaid-clip
chmod +x /usr/local/bin/mermaid-clip

Works from any shell (bash, zsh, fish, etc.).

Raycast

  1. Download mermaid-clip.raycast.sh into your Raycast Script Commands directory
  2. Make it executable: chmod +x mermaid-clip.raycast.sh
  3. Search "Mermaid to Clipboard" in Raycast

The Raycast script uses the full Homebrew path (/opt/homebrew/bin/mmdc) since Raycast doesn't inherit your shell's $PATH. If you installed Homebrew to a non-default location, update the path accordingly.

Usage

  1. Copy Mermaid text to your clipboard (e.g. graph LR; A-->B)
  2. Run mermaid-clip
  3. Paste the rendered PNG image wherever you need it

Options

-s, --size SIZE     Render size: small (1x), medium (2x, default), large (4x)
-t, --theme THEME   Mermaid theme: default, dark, forest, neutral
-h, --help          Show help

Examples

mermaid-clip                        # medium size, default theme
mermaid-clip -s large               # 4x resolution for print/retina
mermaid-clip -t dark                # dark theme
mermaid-clip -s small -t neutral    # small, neutral theme

The Raycast script exposes size and theme as dropdown arguments.

Notes

  • Outputs PNG with a transparent background, ideal for pasting into slides and docs
  • Default scale is 2x for a good balance of quality and file size
  • Uses a temporary directory for the rendered image, cleaned up automatically on exit
#!/usr/bin/env bash
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Mermaid to Clipboard
# @raycast.mode silent
# @raycast.packageName Developer Utils
# Optional parameters:
# @raycast.icon 🧜‍♀️
# @raycast.description Render Mermaid diagram from clipboard to a PNG image and copy it back
# @raycast.argument1 { "type": "dropdown", "placeholder": "Size", "optional": true, "data": [{"title": "Medium (2x)", "value": "2"}, {"title": "Small (1x)", "value": "1"}, {"title": "Large (4x)", "value": "4"}] }
# @raycast.argument2 { "type": "dropdown", "placeholder": "Theme", "optional": true, "data": [{"title": "Default", "value": "default"}, {"title": "Dark", "value": "dark"}, {"title": "Forest", "value": "forest"}, {"title": "Neutral", "value": "neutral"}] }
# Dependencies:
# brew install mermaid-cli
if [ ! -x /opt/homebrew/bin/mmdc ]; then
echo "mmdc not found. Install with: brew install mermaid-cli"
exit 1
fi
scale="${1:-2}"
theme="${2:-default}"
tmpdir=$(mktemp -d /tmp/mermaid-clip.XXXXXXXX)
tmpfile="$tmpdir/diagram.png"
trap 'rm -rf "$tmpdir"' EXIT
if ! pbpaste | /opt/homebrew/bin/mmdc -i - -o "$tmpfile" -b transparent -s "$scale" -t "$theme" 2>/dev/null; then
echo "Failed to render Mermaid diagram"
exit 1
fi
osascript -e "set the clipboard to (read (POSIX file \"$tmpfile\") as «class PNGf»)"
echo "Mermaid diagram copied to clipboard as image (${theme}, ${scale}x)"
#!/usr/bin/env bash
# mermaid-clip — Render Mermaid diagram from clipboard to a PNG image and copy it back
set -euo pipefail
usage() {
cat >&2 <<EOF
Usage: mermaid-clip [options]
Options:
-s, --size SIZE Render size: small (1x), medium (2x, default), large (4x)
-t, --theme THEME Mermaid theme: default, dark, forest, neutral
-h, --help Show this help message
EOF
}
scale=2
theme="default"
while [[ $# -gt 0 ]]; do
case "$1" in
-s|--size)
case "$2" in
small) scale=1 ;;
medium) scale=2 ;;
large) scale=4 ;;
*) echo "Unknown size: $2 (use small, medium, large)" >&2; exit 1 ;;
esac
shift 2 ;;
-t|--theme)
theme="$2"
shift 2 ;;
-h|--help)
usage; exit 0 ;;
*)
echo "Unknown option: $1" >&2; usage; exit 1 ;;
esac
done
if [ "$(uname)" != "Darwin" ]; then
echo "mermaid-clip requires macOS" >&2
exit 1
fi
if ! which mmdc >/dev/null 2>&1; then
echo "mmdc not found. Install with: brew install mermaid-cli" >&2
exit 1
fi
tmpdir=$(mktemp -d /tmp/mermaid-clip.XXXXXXXX)
tmpfile="$tmpdir/diagram.png"
trap 'rm -rf "$tmpdir"' EXIT
if ! pbpaste | mmdc -i - -o "$tmpfile" -b transparent -s "$scale" -t "$theme" 2>/dev/null; then
echo "Failed to render Mermaid diagram" >&2
exit 1
fi
osascript -e "set the clipboard to (read (POSIX file \"$tmpfile\") as «class PNGf»)"
echo "Mermaid diagram copied to clipboard as image (${theme}, ${scale}x)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment