This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
npm install# Single file (auto-reload enabled by default)
./viewmd.js <markdown-file>
# or
node viewmd.js <markdown-file>
# Multiple files (with page breaks, auto-reload enabled)
./viewmd.js <file1.md> <file2.md> <file3.md>
# or
node viewmd.js <file1.md> <file2.md> <file3.md>
# Snapshot mode (render once without watching)
./viewmd.js --snapshot <markdown-file>
# or
./viewmd.js -s <file1.md> <file2.md>npm install -g .
# Then use from anywhere:
viewmd <markdown-file>
# or for multiple files:
viewmd <file1.md> <file2.md> <file3.md>
# or for snapshot mode:
viewmd --snapshot <file1.md> <file2.md> <file3.md>-p, --port <number>: Use specific port instead of random--no-open: Don't automatically open browser-k, --keep-alive: Keep server running after browser closes-s, --snapshot: Render once without watching for changes (disables auto-reload)
This is a CLI tool that renders one or more markdown files in a browser with GitHub-flavored markdown and Mermaid diagram support. When multiple files are provided, they are combined into a single HTML document with CSS page breaks for clean PDF generation.
- viewmd.js - CLI entry point that handles command-line arguments and file validation for one or more files
- index.js - Core logic that:
- Reads all markdown files
- Converts each markdown file to HTML using marked with GFM plugins
- Creates an Express server that serves a single HTML page
- For multiple files: generates table of contents and combines content with page breaks
- Embeds the rendered markdown in a GitHub-styled HTML template
- Includes Mermaid.js for diagram rendering
- Auto-opens browser and manages server lifecycle
- The
openpackage requires special handling:require('open').default || require('open') - Mermaid diagrams are detected by
language-mermaidcode blocks and converted client-side - Server auto-shuts down when browser tab closes via
beforeunloadevent - Uses GitHub markdown CSS and Highlight.js for authentic GitHub rendering
- Multi-file support: Accepts multiple file arguments and combines them with CSS page breaks
- Table of Contents: Auto-generated for multiple files with anchor links
- Print optimization: CSS
@media printrules ensure clean page breaks when generating PDFs - Auto-reload: Enabled by default (disable with
--snapshot):- Uses
chokidarfor cross-platform file watching with debouncing (300ms) - Server-Sent Events (SSE) for one-way server-to-client communication
- Client-side EventSource with automatic reconnection and exponential backoff
- Brief notification flashes when page reloads due to file changes
- Full page reload on file changes to ensure consistency
- Uses
- marked: Markdown parser with GFM support configured
- express: Serves the HTML page
- open: Opens browser automatically
- commander: CLI argument parsing
- marked-gfm-heading-id: Adds GitHub-style heading anchors
- marked-highlight + highlight.js: Syntax highlighting for code blocks
- chokidar: Cross-platform file watching for auto-reload functionality