Skip to main content

A library to convert Markdown to Slack's mrkdwn format

Project description

markdown_to_mrkdwn

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

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
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()

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.

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

markdown_to_mrkdwn-0.1.7.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

markdown_to_mrkdwn-0.1.7-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file markdown_to_mrkdwn-0.1.7.tar.gz.

File metadata

  • Download URL: markdown_to_mrkdwn-0.1.7.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for markdown_to_mrkdwn-0.1.7.tar.gz
Algorithm Hash digest
SHA256 4e3ef2f368dc18a2bf63d36c582df859d711c8d8bf4855910e9dab145d01677f
MD5 6203825bde778538db442f8bcc821ee7
BLAKE2b-256 73b3deafdee521530994c57687454a11cc10aa1694d2769f8bc71e2c777652b6

See more details on using hashes here.

File details

Details for the file markdown_to_mrkdwn-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for markdown_to_mrkdwn-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a4359ffcd89805bc2ed128aa0d9adb4031cfb72a22b976386e3857f40826426c
MD5 5648a258cfacf31c0b26776ae14f0625
BLAKE2b-256 1c1322cab58638ab1728809576165aa057fde66d5006983342892cd8f3234b11

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