Skip to main content

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.

Tests PyPI version Python codecov License

๐Ÿ“– 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

Batch convert all markdown files:

mk2html "*" -o ./html/                # All .md files in current directory
mk2html "*" -o ./html/ -r             # Recursively include subdirectories
mk2html "*" -o ./html/ -r --home-link # With home navigation links

๐Ÿ“‹ Command Line Options

mk2html

usage: mk2html [-h] [-o FILE_OR_DIR] [-r] [-t TITLE] [--theme {light,dark}]
               [--no-mermaid] [--no-toc] [--no-breaks] [--no-line-numbers]
               [--no-convert-md-links] [--home-link] [--offline]
               [--clear-cache] [-q] [-v]
               [INPUT]

Convert Markdown to beautiful, interactive HTML with TOC and Mermaid support.

positional arguments:
  INPUT                 Input Markdown file, "-" for stdin, or "*" for batch mode

options:
  -h, --help            Show this help message and exit
  -o FILE_OR_DIR, --output FILE_OR_DIR
                        Output HTML file or directory (for batch mode with "*")
  -r, --recursive       Recursively process subdirectories (only with "*" input)
  -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
  --no-convert-md-links Disable converting local .md links to .html
  --home-link           Add a home link (๐Ÿ  >) before the title
  --offline             Embed JavaScript libraries for offline use
  --clear-cache         Clear cached library files and exit
  -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:

๐Ÿ“ 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


Made with โค๏ธ by Kinshuk

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mk2html-1.4.0.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mk2html-1.4.0-py3-none-any.whl (38.3 kB view details)

Uploaded Python 3

File details

Details for the file mk2html-1.4.0.tar.gz.

File metadata

  • Download URL: mk2html-1.4.0.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mk2html-1.4.0.tar.gz
Algorithm Hash digest
SHA256 927dcdb63407b47af47e48d884d275f274a184174279321a7c3e918ceb256f5c
MD5 71d8dd9bf1cb2a864bbe9d4ab1b3e468
BLAKE2b-256 3e945db3fb4fe925c337857d74217b7b93c130417f8b6638307c709d44d2ea5c

See more details on using hashes here.

File details

Details for the file mk2html-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: mk2html-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 38.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mk2html-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ae929d85c0dfad42796fa0369cb6d1701fb1e2d80821ab986331500602147be1
MD5 79620193a46f3742355878ae91bc61f1
BLAKE2b-256 0abb524308dda3cd0bb22c4a648bd212f9282c414a0d761ebc18c5a0b97c9ba8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page