A library to convert Markdown to Slack's mrkdwn format
Project description
markdown_to_mrkdwn
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> |
 |
<https://example.com/img.png> |
- 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]
prioritycontrols execution order (lower runs first)timingcan be "before" or "after" (default: "after")scopeis 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:
- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature-name - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin feature/your-feature-name - 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file markdown_to_mrkdwn-0.2.0.tar.gz.
File metadata
- Download URL: markdown_to_mrkdwn-0.2.0.tar.gz
- Upload date:
- Size: 13.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2476dc8481f70d7e2ad7018afa82db964d2aaebd9870cd15bfa6125a24aee57b
|
|
| MD5 |
6600b71a6cacf51d0fe921743766f54a
|
|
| BLAKE2b-256 |
c0e2a044ebf4b477782fc308e0f52bd9afc461df97bd89fe7c39b651b07a66de
|
File details
Details for the file markdown_to_mrkdwn-0.2.0-py3-none-any.whl.
File metadata
- Download URL: markdown_to_mrkdwn-0.2.0-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14116baab3f7219c7c56553e03922e556328a4b8c693ea6c3fbfa5d3ce0912de
|
|
| MD5 |
7e087e1db43864879144b0a0484d16f2
|
|
| BLAKE2b-256 |
0500c9413a9d616cb329900fd52a54e6d3072c89655a3704ee6cf3da2440b0f9
|