Markdown preview

From rp1-run/rp1 · View interactive version

Markdown preview is an AI agent skill from the GitHub repository rp1-run/rp1. Generate browser-viewable HTML previews from markdown, plain text, and Mermaid diagrams. Auto-validates, styles, and opens in browser.

How to use this skill

Skill file

Markdown Preview Generator — Browser-Ready HTML from Markdown

Generate self-contained HTML files from markdown content and automatically open them in the user's default browser.

What This Skill Does

When to Use

Use this skill when you need to:

Trigger scenarios: markdown preview, HTML generation, browser preview, document visualization, diagram rendering.

Parameters

| Parameter | Type | Required | Default | Description |

|-----------|------|----------|---------|-------------|

| content | string | Yes | - | Markdown, plain text, or Mermaid content to render |

| title | string | No | "Markdown Preview" | HTML page title |

| theme | string | No | "github" | Template theme: "github", "dark", or "minimal" |

Workflow

1. Parse Input Content

Extract content from agent context or parameters. The skill expects markdown content as a string input.

2. Validate All Mermaid Diagrams (Single Pass)

Efficient Validation Strategy:

Instead of validating each diagram individually, validate the entire markdown document at once:

  1. Write markdown to temp file:

```bash

TEMP_MD=$(mktemp /tmp/markdown-preview.XXXXXX.md)

echo "$CONTENT" > "$TEMP_MD"

```

  1. Validate all diagrams in one pass:

```bash

rp1 agent-tools mmd-validate "$TEMP_MD"

EXIT_CODE=$?

```

  1. Check validation result:
  1. Handle validation failures:

```bash

if [ $EXIT_CODE -ne 0 ]; then

# Extract error message from validation output

# Prepend warning to markdown content:

CONTENT="⚠️ Mermaid Validation Warning: Some diagrams have syntax errors. They may not render correctly in the preview.\n\n$CONTENT"

# Continue with HTML generation (non-blocking)

fi

```

  1. Clean up temp file:

```bash

rm -f "$TEMP_MD"

```

Why This Approach is Better:

Error Handling:

Dependencies:

3. Generate Self-Contained HTML

Template Selection:

Based on theme parameter, select appropriate template from TEMPLATES.md:

Template Loading:

  1. Read selected template from TEMPLATES.md
  2. Identify template section markers
  3. Extract complete HTML template

Template Processing:

  1. Replace {{TITLE}} with actual page title
  2. Replace {{MARKDOWN_CONTENT}} with markdown content
  3. Ensure proper escaping of special characters in content

HTML Features (embedded in templates):

See TEMPLATES.md for complete HTML structure and styling details.

4. Write HTML to Temp Directory

Filename Generation:

TIMESTAMP=$(date +%s%N)
FILENAME="markdown-preview-$TIMESTAMP.html"
TEMP_DIR="/tmp"
FILE_PATH="$TEMP_DIR/$FILENAME"

File Operations:

Platform-specific temp directories:

5. Open in Default Browser

Platform Detection:

# Detect platform via $OSTYPE
if [[ "$OSTYPE" == "darwin"* ]]; then
    PLATFORM="macos"
    OPEN_CMD="open"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
    PLATFORM="linux"
    OPEN_CMD="xdg-open"
elif [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" || "$OSTYPE" == "win32" ]]; then
    PLATFORM="windows"
    OPEN_CMD="start"
else
    PLATFORM="unknown"
    OPEN_CMD=""
fi

Browser Opening:

if [ -n "$OPEN_CMD" ]; then
    $OPEN_CMD "$FILE_PATH" 2>&1
    if [ $? -eq 0 ]; then
        BROWSER_OPENED=true
    else
        BROWSER_OPENED=false
        # Log error but continue (non-blocking)
    fi
else
    BROWSER_OPENED=false
    # Log warning: unknown platform
fi

Error Handling:

6. Return Results

Output Format:

{
  "status": "success" | "error",
  "filePath": "/tmp/markdown-preview-1699464000000.html",
  "message": "Preview generated successfully.",
  "diagramsValidated": true,
  "browserOpened": true,
  "theme": "github"
}

Success Response:

Error Response:

{
  "status": "error",
  "message": "Failed to write HTML file: Permission denied",
  "filePath": null,
  "diagramsValidated": false,
  "browserOpened": false
}

Error Handling

Empty/Whitespace Input:

File Write Errors:

Validation Script Unavailable:

Browser Launch Failures (non-blocking):

Malformed Markdown (best-effort):

Invalid Theme:

Invalid Mermaid Diagrams (non-blocking):

Integration Example

From PR Visualizer Agent:

## Generate HTML Preview

After creating the markdown file, use the markdown-preview skill to generate the HTML preview.

Invoke the `rp1-base:markdown-preview` skill.

Load the generated markdown file and pass content:
- content: Read from .rp1/work/pr-reviews/<pr-id>-visual.md
- title: "PR Visualization for PR #{pr-number}"
- theme: "github"

The skill will:
1. Write markdown to temp file
2. Validate ALL Mermaid diagrams in one pass using rp1 CLI tool
3. Generate self-contained HTML with professional styling
4. Save to temp directory
5. Auto-open in browser
6. Return file path for logging

Log the file path and report to user:
"✓ Preview generated: {filePath}"

From Documentation Agent:

Invoke `rp1-base:markdown-preview`.

Pass documentation content:
- content: Generated markdown documentation
- title: "Project Documentation"
- theme: "github"

Skill validates all diagrams once and generates HTML.
Browser opens automatically.

Performance Expectations

Optimized Performance (<5 seconds total):

Typical Content:

Performance Improvement:

Dependencies

Internal:

External (embedded via CDN):

System Requirements:

Business Rules

  1. Single-Pass Validation: All Mermaid diagrams validated together in one script call
  2. Non-Blocking Errors: Invalid diagrams don't prevent HTML generation
  3. Browser-Side Error Display: Mermaid.js shows syntax errors in preview
  4. Browser Launch is Non-Blocking: File creation always succeeds; browser opening is best-effort
  5. Self-Contained Output: HTML file contains all resources (CSS inline, JS via CDN)
  6. Best-Effort Rendering: Never fail due to malformed markdown; render as best as possible
  7. Theme Fallback: Invalid theme parameter defaults to "github"
  8. Validation Tool Optional: If rp1 CLI validation unavailable, continue without pre-validation

References

{% include_shared "anti-loop.md" %}

No user interaction required during execution. Complete the entire workflow autonomously.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.