Metadata-Version: 2.5
Name: slack-as-markdown
Version: 0.1.0
Summary: Read a Slack message as Markdown, from its rich_text and other blocks, its attachments, or its mrkdwn text
Project-URL: Repository, https://github.com/iwamot/slack-as-markdown
Project-URL: Issues, https://github.com/iwamot/slack-as-markdown/issues
Author: Takashi Iwamoto
License-Expression: MIT
License-File: LICENSE
Keywords: block-kit,llm,markdown,mrkdwn,slack
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Communications :: Chat
Classifier: Topic :: Text Processing :: Markup :: Markdown
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown

# slack-as-markdown

[![pypi](https://img.shields.io/pypi/v/slack-as-markdown.svg)](https://pypi.org/project/slack-as-markdown/)
[![python](https://img.shields.io/pypi/pyversions/slack-as-markdown.svg)](https://pypi.org/project/slack-as-markdown/)

Read a Slack message as Markdown: its rich text and other blocks, the attachments hung off it, or its mrkdwn `text` when it has nothing else. The output is meant for a reader such as an LLM, so it keeps what the message shows and leaves out how Slack stores it.

```python
from slack_as_markdown import message_as_markdown

message = {
    "type": "message",
    "text": "Release notes\n*Fixed* the login loop :tada:",
    "blocks": [
        {
            "type": "rich_text",
            "elements": [
                {
                    "type": "rich_text_header",
                    "level": 2,
                    "elements": [{"type": "text", "text": "Release notes"}],
                },
                {
                    "type": "rich_text_list",
                    "style": "bullet",
                    "indent": 0,
                    "elements": [
                        {
                            "type": "rich_text_section",
                            "elements": [
                                {"type": "text", "text": "Fixed", "style": {"bold": True}},
                                {"type": "text", "text": " the login loop "},
                                {"type": "emoji", "name": "tada", "unicode": "1f389"},
                            ],
                        }
                    ],
                },
            ],
        }
    ],
}

print(message_as_markdown(message))
```

```
## Release notes
- **Fixed** the login loop 🎉
```

## Why

A message's `text` is a summary Slack writes beside its blocks, and it loses things the blocks keep: a heading becomes a plain line, a code fence loses its language, and characters a person pasted read the same as formatting they typed. Slack's composer also turns what people type and paste into rich text blocks, so reading the blocks is reading what the message shows.

## Install

```sh
pip install slack-as-markdown
```

The package uses the standard library only.

## Usage

Pass a message as `conversations.history` or `conversations.replies` returns it:

```python
from slack_as_markdown import message_as_markdown

markdown = message_as_markdown(message)
```

`message_as_markdown` reads the blocks first, and the `text` as mrkdwn where the blocks carry nothing. The attachments come after, where Slack draws them.

The parts are available on their own too:

| Function | Reads |
|---|---|
| `message_as_markdown(message)` | A whole message |
| `blocks_as_markdown(blocks)` | A message's `blocks` |
| `attachments_as_markdown(attachments)` | A message's `attachments` |
| `mrkdwn_as_markdown(text)` | A string in Slack's mrkdwn |

Every function takes the value as Slack returns it and returns a string, empty when there is nothing to read.

## What is read

- **Rich text**: paragraphs, headings at their level, bullet and numbered lists with their nesting and numbering, quotes, code blocks with their language, and rules. Bold, italic, strike and code are written as Markdown; underline, which Markdown has no syntax for, is left as text.
- **Rich text elements of a kind not listed here**: read through the `fallback` Slack attaches to them, or as the words they hold. The same goes for an inline element, such as a color, which is written as its value.
- **Other blocks**: `header`, `section`, `context`, `markdown`, `table`, `image`, `video`, `divider`, `plan`, `actions` and `input`, and the blocks inside `container`, `card` and `carousel`. Buttons, menus and input fields are named in brackets, such as `[buttons: Approve | Deny]`. A block of a kind not listed here is read for the text it holds.
- **Attachments**: an app's attachment, read as its pretext, author, title, text, fields, image, footer and buttons. A message shared into another is read from the original's blocks, under its author, with a link back to it.
- **Mentions**: written in Slack's own form, such as `<@U0123456>`, `<#C0123456>` and `<!here>`, in `text` with the name after the `|` left as written. Slack sends IDs only, and an agent replying can write the same form back.
- **Emoji**: an emoji in rich text is written as the character, skin tone included; an emoji a workspace added comes with no character and keeps its name, such as `:partyparrot:`. A shortcode in mrkdwn `text`, such as `:rocket:`, is left as written.

## Out of scope

- **Files**: a message's `files` are not read. Their names and contents are for the caller to fetch and place.
- **Names**: user and channel names are not looked up. Doing so needs API calls, and every function here is a pure function of its input.
- **Exact rendering**: the output is Markdown a reader understands, not a guarantee that a Markdown renderer draws it the way Slack does. Characters a person typed as text, such as `2*3*4`, are not escaped.

## How the shapes are known

Slack documents the structure of blocks. The tests are also checked against messages posted to Slack and read back, kept with their IDs replaced under [`tests/fixtures/observed`](tests/fixtures/observed).

## License

MIT
