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.3.tar.gz (463.4 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.3-py3-none-any.whl (517.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: clickmd-1.0.3.tar.gz
  • Upload date:
  • Size: 463.4 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.3.tar.gz
Algorithm Hash digest
SHA256 866479a87b2a358c425959fd66a3287fa3d3c9fe5e1d479a16f95551124cdc64
MD5 3f09315d9645a7d04a3c3e331234e4a1
BLAKE2b-256 78409ba96b898389b947108112fd9c28a3b3123ceb6dec267b948357f670fa13

See more details on using hashes here.

File details

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

File metadata

  • Download URL: clickmd-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 517.2 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 90e6860847ce7555b0493c9f30352ef260274571a058468e8f1dc50ac9ada562
MD5 04ca5c89ee0c6850e2d5d2e1ddc3292d
BLAKE2b-256 9a9de42c256436ef1fee88e0b364a4f62b2c7da08a6d26465c7314ba071aa8b0

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