Skip to main content

Render markdown with syntax highlighting in the terminal

Project description

mdsyntax

PyPI version Python versions CI License: MIT

Render markdown with syntax highlighting in the terminal.

Installation

pip install mdsyntax

Usage

Python API

from mdsyntax import md_print, md_render

# Print directly to terminal
md_print("""
# Hello World

This is **bold** and *italic* text.

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

""")

Get ANSI string for further processing

output = md_render("Some inline code here")


### Command Line

```bash
# Render a file
mdsyntax README.md

# Pipe from stdin
echo "# Hello" | mdsyntax

# Use a different syntax theme
mdsyntax --style dracula document.md

# List available themes
mdsyntax --list-styles

Styling API

Style arbitrary text without markdown:

from mdsyntax import Style, style

# Static methods
print(Style.bold_text("important"))
print(Style.italic_text("emphasis"))
print(Style.color("warning", fg="red"))
print(Style.color("highlight", fg="white", bg="blue"))

# Chainable API
print(style("hello").bold().italic())
print(style("fancy").fg("cyan").underline())
print(style("rgb").fg((255, 100, 50)))  # RGB colors

Available styles:

  • bold() / Style.bold_text()
  • dim() / Style.dim_text()
  • italic() / Style.italic_text()
  • underline() / Style.underline_text()
  • strikethrough() / Style.strike_text()
  • fg(color) / bg(color) - named colors or RGB tuples

Named colors: black, red, green, yellow, blue, magenta, cyan, white

Features

  • Headers (h1-h6) with color coding
  • Bold, italic, bold italic
  • Strikethrough
  • Inline code
  • Fenced code blocks with syntax highlighting
  • Links
  • Unordered and ordered lists
  • Task lists
  • Blockquotes
  • Horizontal rules

Configuration

Code Styles

Any Pygments style is supported. Popular options:

  • monokai (default)
  • dracula
  • one-dark
  • gruvbox-dark
  • nord
  • github-dark

True Color

By default, mdsyntax auto-detects 24-bit color support via the COLORTERM environment variable. You can override this:

# Force 256-color mode
md_print(text, true_color=False)

# Force true color
md_print(text, true_color=True)

API Reference

md_print(text, *, code_style="monokai", code_width=None, true_color=None)

Print markdown to terminal.

  • text: Markdown string to render
  • code_style: Pygments style name for code blocks
  • code_width: Fixed width for code blocks (default: terminal width)
  • true_color: Use 24-bit color (default: auto-detect)

md_render(...) -> str

Same arguments as md_print, but returns the ANSI-formatted string instead of printing.

MarkdownRenderer

Dataclass for more control:

from mdsyntax import MarkdownRenderer

renderer = MarkdownRenderer(
    code_style="dracula",
    code_width=80,
    true_color=True,
)
output = renderer.render(markdown_text)

SyntaxHighlighter

Standalone code highlighter:

from mdsyntax import SyntaxHighlighter

hl = SyntaxHighlighter(style="monokai")
print(hl.highlight("print('hello')", "python"))
print(SyntaxHighlighter.available_styles())

Style / style()

Style text without markdown parsing:

from mdsyntax import Style, style

# Static (returns string directly)
Style.bold_text("text")
Style.color("text", fg="red", bg="white")

# Chainable (call str() or print directly)
style("text").bold().fg("blue")

Ansi

Direct access to ANSI escape codes:

from mdsyntax import Ansi

print(f"{Ansi.BOLD}Bold{Ansi.BOLD_OFF}")
print(f"{Ansi.FG_RED}Red{Ansi.FG_DEFAULT}")
print(f"{Ansi.rgb_fg(255, 100, 50)}RGB{Ansi.FG_DEFAULT}")

License

MIT

Contributing

Contributions are welcome! See CONTRIBUTING.md for guidelines.

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

mdsyntax-0.2.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

mdsyntax-0.2.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file mdsyntax-0.2.0.tar.gz.

File metadata

  • Download URL: mdsyntax-0.2.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mdsyntax-0.2.0.tar.gz
Algorithm Hash digest
SHA256 bfdf10e1da007453cc9925c1f1c81a0690d2f21a2c3304e3f58c206634b1ab29
MD5 b2535f477a05eca40d7bb9746b5117fd
BLAKE2b-256 b390c57956f3182c0898bacab340c8194dd5eeaf79fee8231adf613dff3935be

See more details on using hashes here.

Provenance

The following attestation bundles were made for mdsyntax-0.2.0.tar.gz:

Publisher: release.yml on Azaias/mdsyntax

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mdsyntax-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: mdsyntax-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mdsyntax-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c77ccccc741b6eb97317aa83201b1f3450528ab2b5e10a7e11a9baeb170fa54a
MD5 e024395237ce3fd6dd216762aa2aa949
BLAKE2b-256 02e5c4ecaaa0dc4833f414f430e8763ceb12eef5dae4cbdf78d9110d0f28634b

See more details on using hashes here.

Provenance

The following attestation bundles were made for mdsyntax-0.2.0-py3-none-any.whl:

Publisher: release.yml on Azaias/mdsyntax

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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