Skip to main content

Markdown rendering for CLI applications with syntax highlighting

Project description

clickmd

PyPI version Python 3.10+ License: Apache-2.0 Tests codecov

Markdown rendering for CLI applications with syntax highlighting.

clickmd provides beautiful terminal output with:

  • 🎨 Syntax highlighting for code blocks (Python, TypeScript, JSON, YAML, Bash, etc.)
  • 📝 Markdown rendering with headers, bold, links, and more
  • 🔧 Zero dependencies for core functionality
  • 🖱️ Optional Click integration for CLI decorators

Installation

# Core package (no dependencies)
pip install clickmd

# With Click support
pip install clickmd[click]

Quick Start

Basic Usage (No Dependencies)

from clickmd import md, echo

# Render markdown with syntax highlighting
md("""
# Hello World

This is **bold** and this is a [link](https://example.com).

```python
def greet(name: str) -> str:
    return f"Hello, {name}!"

""")

Smart echo - auto-detects markdown

echo("## Status Update") echo("Regular text without markdown")


### With Click Integration

```python
import clickmd as click

@click.group()
def cli():
    """My awesome CLI tool"""
    pass

@cli.command()
@click.option("--name", "-n", default="World")
def hello(name: str):
    """Say hello"""
    click.md(f"""
## 👋 Hello, {name}!

Welcome to **clickmd** - making CLIs beautiful.
    """)

if __name__ == "__main__":
    cli()

Features

Syntax Highlighting

clickmd provides syntax highlighting for multiple languages:

Language Extensions Highlight Features
Python .py Keywords, strings, comments, decorators
TypeScript/JavaScript .ts, .js Keywords, strings, template literals
JSON .json Keys, strings, numbers, booleans
YAML .yaml, .yml Keys, values, comments
Bash/Shell .sh, .bash Commands, comments
Markdown .md Headers, bold, links
Log .log Errors (red), warnings (yellow), success (green)

Markdown Elements

from clickmd import md

md("""
# Heading 1
## Heading 2
### Heading 3

**Bold text** and regular text.

[Links](https://example.com) are supported.

```python
# Code blocks with syntax highlighting
print("Hello, World!")
  • List items
  • Are rendered
  • Correctly """)

### MarkdownRenderer Class

For more control, use the `MarkdownRenderer` class directly:

```python
from clickmd import MarkdownRenderer
import sys

renderer = MarkdownRenderer(use_colors=True, stream=sys.stdout)
renderer.heading(1, "My Title")
renderer.codeblock("python", 'print("Hello!")')

Progress and Status Output

from clickmd import md

# Log-style output with automatic coloring
md("""
```log
🚀 Starting process...
📦 Installing dependencies...
✅ Build successful!
⚠️ Warning: deprecated API
🛑 Error: connection failed

""")


## API Reference

### Core Functions

#### `md(text: str) -> None`
Render markdown text with syntax highlighting.

#### `echo(message, file=None, nl=True, err=False, color=None) -> None`
Smart echo that auto-detects markdown and renders it.

#### `render_markdown(text, text_lang="markdown", stream=None, use_colors=True) -> None`
Low-level markdown rendering function.

#### `get_renderer(stream=None, use_colors=True) -> MarkdownRenderer`
Get a `MarkdownRenderer` instance.

### Click Decorators (requires `click` package)

When `click` is installed, these decorators are available:

- `@clickmd.group()` - Create a command group
- `@clickmd.command()` - Create a command
- `@clickmd.option()` - Add an option
- `@clickmd.argument()` - Add an argument
- `@clickmd.pass_context` - Pass Click context
- `clickmd.Choice` - Choice type
- `clickmd.Path` - Path type

### Constants

- `CLICK_AVAILABLE: bool` - Whether Click is installed

## Examples

See the [examples/](examples/) directory for more usage examples:

- `examples/basic.py` - Basic markdown rendering
- `examples/cli_app.py` - Full CLI application with Click
- `examples/custom_renderer.py` - Custom renderer configuration
- `examples/logging.py` - Log-style colored output

## Development

```bash
# Clone the repository
git clone https://github.com/wronai/clickmd.git
cd clickmd

# Install development dependencies
pip install -e ".[dev]"

# Run tests
make test

# Run linter
make lint

# Format code
make format

License

Apache License 2.0 - see LICENSE for details.

Author

Created by Tom Sapletta - tom@sapletta.com

Contributing

Contributions are welcome! Please read our Contributing Guide first.

Related Projects

  • Click - Python CLI framework
  • Rich - Rich text and beautiful formatting
  • Reclapp - Contract-first development platform

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

clickmd-1.0.2.tar.gz (461.9 kB view details)

Uploaded Source

Built Distribution

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

clickmd-1.0.2-py3-none-any.whl (510.4 kB view details)

Uploaded Python 3

File details

Details for the file clickmd-1.0.2.tar.gz.

File metadata

  • Download URL: clickmd-1.0.2.tar.gz
  • Upload date:
  • Size: 461.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for clickmd-1.0.2.tar.gz
Algorithm Hash digest
SHA256 046a9b3ec7fed97eff1de790b412e0d27a74ea90ef580c90ce4ebe123ca685e6
MD5 de5b4cb4adf7de65e83470066a2cf7a7
BLAKE2b-256 946402490d00221c110b4e7660584de6ec6c9655ba82122e59a06258490ba379

See more details on using hashes here.

File details

Details for the file clickmd-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: clickmd-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 510.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for clickmd-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f02f767f8ee1331382a9d1d67f39a5038997badda42e00acd7cecf433b5dbf9c
MD5 faceab53ba23ca7d298eed1d90261bd3
BLAKE2b-256 257f7c6a117c055b68b719b626f96d21064e888b2cc1c7d7d2ae03abd17f6f79

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