Skip to main content

markdown_to_mrkdwn

PyPI Downloads GitHub Stars
MIT License GitHub release (latest by date) PyPI Version Documentation
codecov CI Tests

A lightweight, efficient library for converting standard Markdown to Slack's mrkdwn format. This library helps you maintain consistent formatting when sending messages to Slack from your applications.

Features

  • Fast and lightweight conversion from Markdown to Slack's mrkdwn format
  • No external dependencies
  • Comprehensive support for Markdown elements:
    • Headings (H1, H2, H3, H4, H5, H6)
    • Text formatting (bold, italic, strikethrough)
    • Lists (ordered and unordered, with nesting)
    • Ordered lists (numbered lists with proper indentation)
    • Task lists (checked and unchecked items)
    • Tables (with header formatting)
    • Links and image references
    • Code blocks (with language specification preserved)
    • Blockquotes
    • Horizontal rules
  • Preserves code blocks without converting their contents
  • Handles special characters and edge cases

Installation

Install from PyPI using pip:

pip install markdown_to_mrkdwn

Requirements:

  • Python 3.8 or higher

Usage

Basic Usage

from markdown_to_mrkdwn import SlackMarkdownConverter

# Create a converter instance
converter = SlackMarkdownConverter()

# Convert markdown to mrkdwn
markdown_text = """
# Heading 1
**Bold text**
- List item
[Link](https://example.com)
~~Strikethrough text~~
"""
mrkdwn_text = converter.convert(markdown_text)
print(mrkdwn_text)

Output

*Heading 1*
*Bold text*
• List item
<https://example.com|Link>
~Strikethrough text~

Supported Conversions

Markdown Slack mrkdwn
# Heading *Heading*
## Heading *Heading*
### Heading *Heading*
#### Heading *Heading*
##### Heading *Heading*
###### Heading *Heading*
**Bold** *Bold*
__Bold__ *Bold*
*Italic* _Italic_
~~Strikethrough~~ ~Strikethrough~
[Link](https://example.com) <https://example.com|Link>
![Image](https://example.com/img.png) <https://example.com/img.png>
- List item • List item
* List item • List item
1. Ordered item 1. Ordered item
- [ ] Task • ☐ Task
- [x] Task • ☑ Task
> Quote > Quote
`Code` `Code`
```python ```python
--- ──────────
Tables Simple text tables with bold headers

Testing in Slack

You can test the output in Slack Block Kit Builder: Slack Block Kit Builder

Advanced Usage

Custom Encoding

You can specify a custom encoding when initializing the converter:

converter = SlackMarkdownConverter()

Plugin System

You can extend the converter with your own plugins.

Function Plugin Example

from markdown_to_mrkdwn.converter import SlackMarkdownConverter

def to_upper(line):
    return line.upper()

converter = SlackMarkdownConverter()
converter.register_plugin(
    name="to_upper",
    converter_func=to_upper,
    priority=10,
    scope="line",
    timing="after"
)
print(converter.convert("hello"))  # Output: HELLO

Regex Plugin Example

# Add comma to thousands
converter.register_regex_plugin(
    name="add_comma_to_thousands",
    pattern=r"(?<=\\d)(?=(\\d{3})+(?!\\d))",
    replacement=",",
    priority=10,
    timing="after"
)
print(converter.convert("1234567"))  # Output: 1,234,567

# Mask email addresses
converter.register_regex_plugin(
    name="mask_email",
    pattern=r"[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+",
    replacement="[EMAIL]",
    priority=20,
    timing="after"
)
print(converter.convert("Contact: test.user@example.com"))  # Output: Contact: [EMAIL]
  • priority controls execution order (lower runs first)
  • timing can be "before" or "after" (default: "after")
  • scope is always "line" for regex plugins

Error Handling

The converter will return the original markdown text if an error occurs during conversion:

try:
    mrkdwn_text = converter.convert(markdown_text)
except Exception as e:
    print(f"Conversion error: {e}")

Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-feature-name
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin feature/your-feature-name
  5. Submit a pull request

Please make sure to update tests as appropriate.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for markdown-to-mrkdwn 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for markdown-to-mrkdwn 0.4.0
File Size Uploaded Provenance Actions
markdown_to_mrkdwn-0.4.0.tar.gz 16.4 kB Without provenance Details for markdown_to_mrkdwn-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for markdown-to-mrkdwn 0.4.0
File Interpreter ABI Platform Actions
markdown_to_mrkdwn-0.4.0-py3-none-any.whl Python 3 none any Details for markdown_to_mrkdwn-0.4.0-py3-none-any.whl

Total release size: 31.7 kB

Release files / markdown_to_mrkdwn-0.4.0.tar.gz

Download URL markdown_to_mrkdwn-0.4.0.tar.gz
Size 16.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c15fc1cb18a9d1128daada0b9a344fc259645d92cf3a15dc39a5b405d7d81e83
BLAKE2b-256 checksum
How to use checksums
2ef40a50f89d8ef260a7ebe9759e8474202624db436cb2d0ebba15fcefd9639f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / markdown_to_mrkdwn-0.4.0-py3-none-any.whl

Download URL markdown_to_mrkdwn-0.4.0-py3-none-any.whl
Size 15.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6de02f83aeed53579232a4e7858122ee9bc9b7a9d79b42643a8dc6883289d67e
BLAKE2b-256 checksum
How to use checksums
82afa6e14b36169ee979b79bc4d920da5316f5d7a9a6843e639ac6f8da953758
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Security for markdown-to-mrkdwn 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page