A command-line tool written in Go that converts Markdown files to HTML and automatically previews them in your default web browser.
- Convert Markdown files to clean, sanitized HTML
- Automatic preview in your default web browser
- Cross-platform support (Linux, Windows, macOS)
- Safe HTML generation using bluemonday sanitizer
- Auto-preview mode with file watching capability
- Command-line flags for flexible usage
# Clone the repository
git clone https://github.com/user/program.git
# Navigate to the project directory
cd program
# Build the binary
go build -o mdpThe program uses the following external packages:
github.com/microcosm-cc/bluemonday- For HTML sanitizationgithub.com/russross/blackfriday/v2- For Markdown to HTML conversion
To run the tests:
go test -v# Preview a markdown file
./mdp -file=your_markdown_file.md
# Preview without opening browser (just generate HTML)
./mdp -file=your_markdown_file.md -s-file: Specify the input Markdown file (required)-s: Skip auto-preview in browser-t: Specify an alternate HTML template file
The tool supports custom HTML templates for rendering the markdown content. Two template files are provided:
template.html.tmpl: Basic HTML templatetemplate-fmt.html.tmpl: Template with custom styling (blue headers)
To use a custom template:
./mdp -file=your_markdown_file.md -t=template-fmt.html.tmplTemplate variables available:
{{ .Title }}: The title of the document{{ .Body }}: The converted HTML content
The tool includes an auto-preview feature that watches for changes in your Markdown file and automatically updates the preview. To use this feature:
-
Make the auto-preview script executable:
chmod +x autopreview.sh
-
Run the auto-preview script with your markdown file:
./autopreview.sh your_markdown_file.md
The script will check for changes every 5 seconds and update the preview automatically.
- The program reads the input Markdown file
- Converts the Markdown to HTML using blackfriday
- Sanitizes the HTML output using bluemonday for security
- Creates a temporary HTML file with proper HTML structure
- Opens the generated HTML in your default web browser (unless -s flag is used)
- Cleans up the temporary file after preview
The generated HTML includes proper DOCTYPE and HTML structure for optimal rendering.
You can also use Python's built-in HTTP server to view the generated HTML files:
-
Generate the HTML file:
./mdp -file=your_markdown_file.md -s
-
Start Python's HTTP server:
python3 -m http.server 8000
-
Open your browser and navigate to:
http://localhost:8000/mdpPreview.html
This is particularly useful when you want to serve the preview over a network or need to refresh the page manually.