slack-blocks-markdown


Nameslack-blocks-markdown JSON
Version 0.1.7 PyPI version JSON
download
home_pageNone
SummaryConvert Markdown to Slack Block Kit blocks using mistletoe
upload_time2025-10-28 13:58:55
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseMIT License Copyright (c) 2025 atacan Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords slack markdown blocks mistletoe converter block-kit
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Slack Blocks Markdown

[![PyPI version](https://badge.fury.io/py/slack-blocks-markdown.svg)](https://badge.fury.io/py/slack-blocks-markdown)
[![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Tests](https://github.com/atacan/slack-blocks-markdown/workflows/tests/badge.svg)](https://github.com/atacan/slack-blocks-markdown/actions)

Convert Markdown to Slack Block Kit blocks using Python. This library provides a clean, efficient way to transform your Markdown content into Slack's interactive Block Kit format.

## Features

- ๐Ÿš€ **Complete Markdown Support**: Headers, paragraphs, lists, code blocks, quotes, tables, links, and inline formatting
- ๐Ÿ“ฑ **Slack Block Kit Compatible**: All blocks follow official Slack specifications with proper constraints
- ๐ŸŽฏ **Custom Table Support**: Full implementation of Slack's table blocks with validation
- ๐Ÿ”ง **Easy to Use**: Simple API with both direct renderer and convenience function
- โšก **High Performance**: Efficient processing of large documents
- ๐Ÿงช **Well Tested**: Comprehensive test suite with 88% coverage
- ๐Ÿ“ **Type Safe**: Full type hints for better development experience

## Installation

```bash
pip install slack-blocks-markdown
```

## Quick Start

### Simple Usage

```python
from slack_blocks_markdown import markdown_to_blocks

# Convert markdown to Slack blocks
markdown = """# Project Update

This is a **bold** announcement about our new features:

- โœ… User authentication
- โœ… Real-time updates
- ๐Ÿšง Mobile app (coming soon)

> **Note**: This is still in beta, so please report any issues!
"""

blocks = markdown_to_blocks(markdown)
print(f"Generated {len(blocks)} blocks")

# Use blocks with Slack SDK
from slack_sdk import WebClient

client = WebClient(token="your-token")
client.chat_postMessage(
    channel="#general",
    blocks=blocks
)
```

### Advanced Usage

```python
from mistletoe import Document
from slack_blocks_markdown import SlackBlocksRenderer

# For more control over the rendering process
with SlackBlocksRenderer() as renderer:
    document = Document(markdown)
    blocks = renderer.render(document)

# Convert to dictionaries for JSON serialization
blocks_json = [block.to_dict() for block in blocks]
```

## Supported Markdown Elements

| Markdown Element | Slack Block Type | Notes |
|-----------------|------------------|-------|
| `# Headers` | HeaderBlock | Truncated to 150 chars |
| Paragraphs | SectionBlock | With mrkdwn formatting |
| `**Bold**` โ†’ `*Bold*` | Inline formatting | Slack markdown style |
| `_Italic_` โ†’ `_Italic_` | Inline formatting | Slack markdown style |
| `` `Code` `` โ†’ `` `Code` `` | Inline formatting | Preserved |
| `~~Strike~~` โ†’ `~Strike~` | Inline formatting | Slack markdown style |
| `[Link](url)` โ†’ `<url\|Link>` | Inline formatting | Slack link format |
| Code blocks | SectionBlock | With triple backticks |
| > Blockquotes | SectionBlock | With > prefix |
| Lists | SectionBlock | Bullet (โ€ข) or numbered |
| Tables | TableBlock | Custom implementation |
| `---` | DividerBlock | Horizontal rules |

## Examples

### Basic Formatting

```python
markdown = "This is **bold** and _italic_ with `code` and [links](https://example.com)"
blocks = markdown_to_blocks(markdown)
# Result: [{"type": "section", "text": {"type": "mrkdwn", "text": "This is *bold* and _italic_ with `code` and <https://example.com|links>"}}]
```

### Lists and Code

```python
markdown = """
## Features

- Easy to use
- Fast processing
- Great documentation

```python
def hello():
    return "world"
```
"""
blocks = markdown_to_blocks(markdown)
# Generates HeaderBlock, SectionBlock (list), and SectionBlock (code)
```

### Tables

```python
markdown = """
| Feature | Status |
|---------|--------|
| Auth | โœ… Done |
| API | ๐Ÿšง Progress |
"""
blocks = markdown_to_blocks(markdown)
# Generates custom TableBlock with proper cell structure
```

## API Reference

### `markdown_to_blocks(markdown_text: str) -> list`

Convenience function to convert markdown to Slack blocks.

**Parameters:**
- `markdown_text`: Markdown formatted string

**Returns:**
- List of Slack block dictionaries ready for API use

### `SlackBlocksRenderer`

Main renderer class inheriting from mistletoe's BaseRenderer.

**Methods:**
- `render(document)`: Convert mistletoe Document to list of Block objects
- Context manager support for proper resource handling

### `TableBlock`

Custom table block implementation for Slack Block Kit.

**Parameters:**
- `rows`: List of rows (each row is list of cell objects)
- `block_id`: Optional unique identifier (max 255 chars)
- `column_settings`: Optional column configuration

## Slack Block Kit Constraints

This library automatically handles Slack's Block Kit constraints:

- **Headers**: Maximum 150 characters (truncated with "..." if longer)
- **Text blocks**: Maximum 3000 characters (truncated with "..." if longer)
- **Tables**: Maximum 100 rows, 20 columns per row
- **Block IDs**: Maximum 255 characters

## Development

```bash
# Clone repository
git clone https://github.com/atacan/slack-blocks-markdown.git
cd slack-blocks-markdown

# Install with development dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run linting
ruff check src/ tests/
black src/ tests/

# Run type checking
mypy src/
```

## Testing

The library includes comprehensive tests covering:

- All markdown element types
- Slack constraint validation
- Edge cases and error handling
- Integration with Slack API format
- Performance with large documents

```bash
# Run with coverage
pytest --cov=slack_blocks_markdown --cov-report=html
```

## Contributing

Contributions welcome! Please:

1. Fork the repository
2. Create a feature branch
3. Add tests for new functionality
4. Ensure all tests pass
5. Submit a pull request

## License

MIT License - see [LICENSE](LICENSE) file for details.

## Changelog

See [CHANGELOG.md](CHANGELOG.md) for version history.

## Related Projects

- [mistletoe](https://github.com/miyuchina/mistletoe) - The markdown parser we use
- [slack-sdk](https://github.com/slackapi/python-slack-sdk) - Official Slack SDK for Python
- [Slack Block Kit Builder](https://app.slack.com/block-kit-builder) - Visual tool for building Slack blocks

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "slack-blocks-markdown",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": "atacan <info@actondon.com>",
    "keywords": "slack, markdown, blocks, mistletoe, converter, block-kit",
    "author": null,
    "author_email": "atacan <info@actondon.com>",
    "download_url": "https://files.pythonhosted.org/packages/b2/ae/cdb00b559b2e59e928fb263c7984d198688431c52108ffe0d6d9775b2955/slack_blocks_markdown-0.1.7.tar.gz",
    "platform": null,
    "description": "# Slack Blocks Markdown\n\n[![PyPI version](https://badge.fury.io/py/slack-blocks-markdown.svg)](https://badge.fury.io/py/slack-blocks-markdown)\n[![Python 3.11+](https://img.shields.io/badge/python-3.11+-blue.svg)](https://www.python.org/downloads/)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Tests](https://github.com/atacan/slack-blocks-markdown/workflows/tests/badge.svg)](https://github.com/atacan/slack-blocks-markdown/actions)\n\nConvert Markdown to Slack Block Kit blocks using Python. This library provides a clean, efficient way to transform your Markdown content into Slack's interactive Block Kit format.\n\n## Features\n\n- \ud83d\ude80 **Complete Markdown Support**: Headers, paragraphs, lists, code blocks, quotes, tables, links, and inline formatting\n- \ud83d\udcf1 **Slack Block Kit Compatible**: All blocks follow official Slack specifications with proper constraints\n- \ud83c\udfaf **Custom Table Support**: Full implementation of Slack's table blocks with validation\n- \ud83d\udd27 **Easy to Use**: Simple API with both direct renderer and convenience function\n- \u26a1 **High Performance**: Efficient processing of large documents\n- \ud83e\uddea **Well Tested**: Comprehensive test suite with 88% coverage\n- \ud83d\udcdd **Type Safe**: Full type hints for better development experience\n\n## Installation\n\n```bash\npip install slack-blocks-markdown\n```\n\n## Quick Start\n\n### Simple Usage\n\n```python\nfrom slack_blocks_markdown import markdown_to_blocks\n\n# Convert markdown to Slack blocks\nmarkdown = \"\"\"# Project Update\n\nThis is a **bold** announcement about our new features:\n\n- \u2705 User authentication\n- \u2705 Real-time updates\n- \ud83d\udea7 Mobile app (coming soon)\n\n> **Note**: This is still in beta, so please report any issues!\n\"\"\"\n\nblocks = markdown_to_blocks(markdown)\nprint(f\"Generated {len(blocks)} blocks\")\n\n# Use blocks with Slack SDK\nfrom slack_sdk import WebClient\n\nclient = WebClient(token=\"your-token\")\nclient.chat_postMessage(\n    channel=\"#general\",\n    blocks=blocks\n)\n```\n\n### Advanced Usage\n\n```python\nfrom mistletoe import Document\nfrom slack_blocks_markdown import SlackBlocksRenderer\n\n# For more control over the rendering process\nwith SlackBlocksRenderer() as renderer:\n    document = Document(markdown)\n    blocks = renderer.render(document)\n\n# Convert to dictionaries for JSON serialization\nblocks_json = [block.to_dict() for block in blocks]\n```\n\n## Supported Markdown Elements\n\n| Markdown Element | Slack Block Type | Notes |\n|-----------------|------------------|-------|\n| `# Headers` | HeaderBlock | Truncated to 150 chars |\n| Paragraphs | SectionBlock | With mrkdwn formatting |\n| `**Bold**` \u2192 `*Bold*` | Inline formatting | Slack markdown style |\n| `_Italic_` \u2192 `_Italic_` | Inline formatting | Slack markdown style |\n| `` `Code` `` \u2192 `` `Code` `` | Inline formatting | Preserved |\n| `~~Strike~~` \u2192 `~Strike~` | Inline formatting | Slack markdown style |\n| `[Link](url)` \u2192 `<url\\|Link>` | Inline formatting | Slack link format |\n| Code blocks | SectionBlock | With triple backticks |\n| > Blockquotes | SectionBlock | With > prefix |\n| Lists | SectionBlock | Bullet (\u2022) or numbered |\n| Tables | TableBlock | Custom implementation |\n| `---` | DividerBlock | Horizontal rules |\n\n## Examples\n\n### Basic Formatting\n\n```python\nmarkdown = \"This is **bold** and _italic_ with `code` and [links](https://example.com)\"\nblocks = markdown_to_blocks(markdown)\n# Result: [{\"type\": \"section\", \"text\": {\"type\": \"mrkdwn\", \"text\": \"This is *bold* and _italic_ with `code` and <https://example.com|links>\"}}]\n```\n\n### Lists and Code\n\n```python\nmarkdown = \"\"\"\n## Features\n\n- Easy to use\n- Fast processing\n- Great documentation\n\n```python\ndef hello():\n    return \"world\"\n```\n\"\"\"\nblocks = markdown_to_blocks(markdown)\n# Generates HeaderBlock, SectionBlock (list), and SectionBlock (code)\n```\n\n### Tables\n\n```python\nmarkdown = \"\"\"\n| Feature | Status |\n|---------|--------|\n| Auth | \u2705 Done |\n| API | \ud83d\udea7 Progress |\n\"\"\"\nblocks = markdown_to_blocks(markdown)\n# Generates custom TableBlock with proper cell structure\n```\n\n## API Reference\n\n### `markdown_to_blocks(markdown_text: str) -> list`\n\nConvenience function to convert markdown to Slack blocks.\n\n**Parameters:**\n- `markdown_text`: Markdown formatted string\n\n**Returns:**\n- List of Slack block dictionaries ready for API use\n\n### `SlackBlocksRenderer`\n\nMain renderer class inheriting from mistletoe's BaseRenderer.\n\n**Methods:**\n- `render(document)`: Convert mistletoe Document to list of Block objects\n- Context manager support for proper resource handling\n\n### `TableBlock`\n\nCustom table block implementation for Slack Block Kit.\n\n**Parameters:**\n- `rows`: List of rows (each row is list of cell objects)\n- `block_id`: Optional unique identifier (max 255 chars)\n- `column_settings`: Optional column configuration\n\n## Slack Block Kit Constraints\n\nThis library automatically handles Slack's Block Kit constraints:\n\n- **Headers**: Maximum 150 characters (truncated with \"...\" if longer)\n- **Text blocks**: Maximum 3000 characters (truncated with \"...\" if longer)\n- **Tables**: Maximum 100 rows, 20 columns per row\n- **Block IDs**: Maximum 255 characters\n\n## Development\n\n```bash\n# Clone repository\ngit clone https://github.com/atacan/slack-blocks-markdown.git\ncd slack-blocks-markdown\n\n# Install with development dependencies\npip install -e \".[dev]\"\n\n# Run tests\npytest\n\n# Run linting\nruff check src/ tests/\nblack src/ tests/\n\n# Run type checking\nmypy src/\n```\n\n## Testing\n\nThe library includes comprehensive tests covering:\n\n- All markdown element types\n- Slack constraint validation\n- Edge cases and error handling\n- Integration with Slack API format\n- Performance with large documents\n\n```bash\n# Run with coverage\npytest --cov=slack_blocks_markdown --cov-report=html\n```\n\n## Contributing\n\nContributions welcome! Please:\n\n1. Fork the repository\n2. Create a feature branch\n3. Add tests for new functionality\n4. Ensure all tests pass\n5. Submit a pull request\n\n## License\n\nMIT License - see [LICENSE](LICENSE) file for details.\n\n## Changelog\n\nSee [CHANGELOG.md](CHANGELOG.md) for version history.\n\n## Related Projects\n\n- [mistletoe](https://github.com/miyuchina/mistletoe) - The markdown parser we use\n- [slack-sdk](https://github.com/slackapi/python-slack-sdk) - Official Slack SDK for Python\n- [Slack Block Kit Builder](https://app.slack.com/block-kit-builder) - Visual tool for building Slack blocks\n",
    "bugtrack_url": null,
    "license": "MIT License\n        \n        Copyright (c) 2025 atacan\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy\n        of this software and associated documentation files (the \"Software\"), to deal\n        in the Software without restriction, including without limitation the rights\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n        copies of the Software, and to permit persons to whom the Software is\n        furnished to do so, subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n        SOFTWARE.\n        ",
    "summary": "Convert Markdown to Slack Block Kit blocks using mistletoe",
    "version": "0.1.7",
    "project_urls": {
        "Documentation": "https://github.com/atacan/slack-blocks-markdown/blob/main/README.md",
        "Homepage": "https://github.com/atacan/slack-blocks-markdown",
        "Issues": "https://github.com/atacan/slack-blocks-markdown/issues",
        "Repository": "https://github.com/atacan/slack-blocks-markdown"
    },
    "split_keywords": [
        "slack",
        " markdown",
        " blocks",
        " mistletoe",
        " converter",
        " block-kit"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "0ce0968c84fe32c416d4048c95b13398cd786574851321c938bc2d2467f2cc17",
                "md5": "250785a292d73a949bb4872f2d57bab0",
                "sha256": "e3b6992cc3bae0f1c3b38ec1eb7c030372eb01ed18e87e4783850e2c9a5c7bdd"
            },
            "downloads": -1,
            "filename": "slack_blocks_markdown-0.1.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "250785a292d73a949bb4872f2d57bab0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 10731,
            "upload_time": "2025-10-28T13:58:54",
            "upload_time_iso_8601": "2025-10-28T13:58:54.619868Z",
            "url": "https://files.pythonhosted.org/packages/0c/e0/968c84fe32c416d4048c95b13398cd786574851321c938bc2d2467f2cc17/slack_blocks_markdown-0.1.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "b2aecdb00b559b2e59e928fb263c7984d198688431c52108ffe0d6d9775b2955",
                "md5": "d98bad3461d3ec76e219a7f4829a088f",
                "sha256": "d5d7b0e14c92b782c51c84acf1727d30f58777c0aa4f7592685aa26d8a821ba3"
            },
            "downloads": -1,
            "filename": "slack_blocks_markdown-0.1.7.tar.gz",
            "has_sig": false,
            "md5_digest": "d98bad3461d3ec76e219a7f4829a088f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 17225,
            "upload_time": "2025-10-28T13:58:55",
            "upload_time_iso_8601": "2025-10-28T13:58:55.881380Z",
            "url": "https://files.pythonhosted.org/packages/b2/ae/cdb00b559b2e59e928fb263c7984d198688431c52108ffe0d6d9775b2955/slack_blocks_markdown-0.1.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-28 13:58:55",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "atacan",
    "github_project": "slack-blocks-markdown",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "slack-blocks-markdown"
}
        
Elapsed time: 2.25376s