Convert Markdown to beautiful, interactive HTML and PDF with TOC, dark/light mode, Mermaid diagrams, and syntax highlighting
Project description
mk2html
A powerful Python CLI tool that converts Markdown files to beautiful, interactive HTML documents with Table of Contents, Dark/Light mode, Mermaid diagram support, and syntax highlighting.
๐ Documentation | ๐ PyPI | ๐ Changelog
โจ Features
- ๐ Dark/Light Mode - Toggle between themes with persistence via localStorage
- ๐ Auto Table of Contents - Generated from headings with active section highlighting
- ๐ Mermaid Diagrams - Flowcharts, sequence, class, gantt, pie, state diagrams
- ๐จ Syntax Highlighting - Language-aware code highlighting with Highlight.js
- ๐ข Line Numbers - Code blocks include line numbers
- ๐ Progress Bar - Shows reading progress as you scroll
- ๐ Back to Top - Floating button for quick navigation
- ๐ฑ Responsive Design - Mobile-friendly with collapsible sidebar
- ๐จ๏ธ Print Styles - Clean output when printing
- ๐ Click-to-Copy - Click any heading to copy its link
๐ Getting Started
Prerequisites
- Python 3.9 or higher
- pip or uv (Python package managers)
Installation
Install from PyPI:
# HTML converter only
pip install mk2html
# With PDF support
pip install mk2html[pdf]
Install as global CLI tool with uv:
uv tool install mk2html
Install from source:
git clone https://github.com/km1790/mk2html.git
cd mk2html
pip install -e .
โก Quick Start
Convert a Markdown file to HTML:
mk2html README.md
This creates README.html in the same directory.
Convert with custom output:
mk2html docs.md -o documentation.html
Convert to PDF (requires mk2html[pdf]):
mk2pdf README.md -o README.pdf
Convert from stdin:
cat input.md | mk2html - -o output.html
๐ Command Line Options
mk2html
usage: mk2html [-h] [-o FILE] [-t TITLE] [--theme {light,dark}] [--no-mermaid]
[--no-toc] [--no-breaks] [--no-line-numbers] [--css FILE]
[--offline] [--download-libs] [-q] [-v]
INPUT
Convert Markdown to beautiful, interactive HTML with TOC and Mermaid support.
positional arguments:
INPUT Input Markdown file (use "-" for stdin)
options:
-h, --help Show this help message and exit
-o FILE, --output FILE
Output HTML file (default: input filename with .html)
-t TITLE, --title TITLE
Document title (default: filename without extension)
--theme {light,dark} Default theme (default: light)
--no-mermaid Disable Mermaid diagram support
--no-toc Disable table of contents generation
--no-breaks Disable automatic line break conversion (nl2br)
--no-line-numbers Disable line numbers in code blocks
--css FILE Custom CSS file to include
--offline Use local libraries instead of CDN
--download-libs Download libraries for offline use
-q, --quiet Suppress output messages
-v, --version Show program's version number and exit
mk2pdf
usage: mk2pdf [-h] [-o FILE] [-t TITLE] [--theme {light,dark}] [--no-mermaid]
[--no-toc] [--no-breaks] [--no-line-numbers]
[--page-size {a4,letter,legal}] [--margin MARGIN] [--landscape]
[-q] [-v]
INPUT
Convert Markdown to PDF via HTML rendering.
positional arguments:
INPUT Input Markdown file (use "-" for stdin)
options:
-h, --help Show this help message and exit
-o FILE, --output FILE
Output PDF file (default: input filename with .pdf)
-t TITLE, --title TITLE
Document title
--theme {light,dark} Theme for rendering (default: light)
--no-mermaid Disable Mermaid diagram support
--no-toc Disable table of contents generation
--no-breaks Disable automatic line break conversion
--no-line-numbers Disable line numbers in code blocks
--page-size {a4,letter,legal}
PDF page size (default: a4)
--margin MARGIN Page margins in CSS format (default: 20mm)
--landscape Use landscape orientation
-q, --quiet Suppress output messages
-v, --version Show program's version number and exit
๐ Mermaid Diagrams
The tool supports all Mermaid diagram types. Simply use fenced code blocks with mermaid language:
Flowchart
```mermaid
flowchart TD
A[Start] --> B{Is it working?}
B -->|Yes| C[Great!]
B -->|No| D[Debug]
D --> B
```
Sequence Diagram
```mermaid
sequenceDiagram
Alice->>Bob: Hello Bob!
Bob-->>Alice: Hi Alice!
```
Pie Chart
```mermaid
pie title Project Distribution
"Development" : 45
"Testing" : 25
"Documentation" : 15
"Other" : 15
```
Gantt Chart
```mermaid
gantt
title Project Timeline
dateFormat YYYY-MM-DD
section Phase 1
Task 1 :a1, 2024-01-01, 30d
Task 2 :after a1, 20d
```
๐จ Syntax Highlighting
Code blocks are automatically highlighted with language detection:
```python
def hello_world():
print("Hello, World!")
```
```javascript
const greet = (name) => {
console.log(`Hello, ${name}!`);
};
```
Supported languages include: Python, JavaScript, TypeScript, Java, C, C++, Go, Rust, Ruby, PHP, SQL, HTML, CSS, JSON, YAML, Bash, and many more.
๐ Theme Support
The generated HTML includes both light and dark themes:
- Light Theme: Clean white background with blue accents
- Dark Theme: Dark slate background with softer colors
Users can toggle between themes using the switch in the header. The preference is saved in localStorage.
๐ Project Structure
mk2html/
โโโ .github/
โ โโโ workflows/
โ โโโ release.yml # PyPI release & GitHub Pages deployment
โ โโโ tests.yml # CI test workflow
โโโ docs/ # Documentation (deployed to GitHub Pages)
โ โโโ index.md
โ โโโ getting-started.md
โ โโโ mk2html.md
โ โโโ mk2pdf.md
โ โโโ configuration.md
โ โโโ api-reference.md
โ โโโ examples.md
โ โโโ contributing.md
โ โโโ changelog.md
โโโ example/
โ โโโ markdown/ # Example markdown files
โ โโโ sample.md
โ โโโ sample_with_mermaid.md
โ โโโ api_documentation.md
โ โโโ meeting_notes.md
โ โโโ technical_spec.md
โโโ tests/
โ โโโ __init__.py
โ โโโ test_mk2html.py # mk2html unit tests
โ โโโ test_mk2pdf.py # mk2pdf unit tests
โโโ mk2html.py # Main HTML converter CLI
โโโ mk2pdf.py # PDF converter CLI
โโโ pyproject.toml # Package configuration
โโโ requirements.txt # Python dependencies
โโโ CHANGELOG.md # Version history
โโโ CONTRIBUTING.md # Contribution guidelines
โโโ LICENSE # MIT License
โโโ README.md # This file
๐ง Dependencies
| Package | Version | Description |
|---|---|---|
| markdown | โฅ3.4.0 | Markdown parsing |
| Pygments | โฅ2.15.0 | Syntax highlighting (backup) |
External CDN Resources
The generated HTML loads these from CDN:
- Mermaid.js v10 - Diagram rendering
- Highlight.js v11.9 - Syntax highlighting
๐ Example
Convert the sample file:
python3 mk2html.py sample_with_mermaid.md -o demo.html --title "Demo Document"
Then open demo.html in your browser.
๐ค Contributing
Contributions are welcome! Please see our Contributing Guidelines for details on:
- Code of Conduct
- Development setup
- Pull request process
- Coding standards
Quick start for contributors:
git clone https://github.com/km1790/mk2html.git
cd mk2html
pip install -e ".[dev]"
pytest tests/ -v
๐ License
MIT License - feel free to use this tool for any purpose.
๐ Acknowledgments
- Python-Markdown for Markdown parsing
- Mermaid.js for diagram rendering
- Highlight.js for syntax highlighting
- Design inspired by modern documentation sites
Made with โค๏ธ by Kinshuk
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mk2html-1.3.1.tar.gz.
File metadata
- Download URL: mk2html-1.3.1.tar.gz
- Upload date:
- Size: 23.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49250f44dace367a3f1723e452acf40620a4b34118fbf4553e3f0fb0d74bec26
|
|
| MD5 |
bf06898b1abe4a06423884394c9a837c
|
|
| BLAKE2b-256 |
faad85ee3e19bf3b193c08d0eef95251780f17710d588577918271dd51b1efd3
|
File details
Details for the file mk2html-1.3.1-py3-none-any.whl.
File metadata
- Download URL: mk2html-1.3.1-py3-none-any.whl
- Upload date:
- Size: 35.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d5284c1b8c86507f5c7c6c7331526b6417b2c3d1b9abe7ac528ab207bb9b68fa
|
|
| MD5 |
4841953e91aab9ddf8e8390a5c79008c
|
|
| BLAKE2b-256 |
53ba7ed52059dce3d624538231f97be4cf973a37e822e4fa1f98c841a06ff5b7
|