Created
May 28, 2026 11:30
-
-
Save ShubhamRasal/ba0bed7a31021e002c54a18d3263ef52 to your computer and use it in GitHub Desktop.
slackready — convert markdown on stdin to Slack mrkdwn on stdout. Usage: pbpaste | slackready | pbcopy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """Convert markdown on stdin to Slack mrkdwn on stdout. | |
| Slack flavor: | |
| *bold* (single asterisk, not double) | |
| _italic_ (underscore) | |
| ~strike~ (single tilde) | |
| <url|label> (links) | |
| `code` (same) | |
| ```code``` (same) | |
| > quote (same) | |
| - bullet (same) | |
| # Heading (not supported -- degrades to *Heading*) | |
| Pipe usage: | |
| pbpaste | slackready | pbcopy | |
| cat notes.md | slackready | |
| """ | |
| import re | |
| import sys | |
| def convert(text: str) -> str: | |
| stash: list[str] = [] | |
| def park(m: re.Match) -> str: | |
| stash.append(m.group(0)) | |
| return f"\x00CODE{len(stash) - 1}\x00" | |
| text = re.sub(r"```[\s\S]*?```", park, text) | |
| text = re.sub(r"`[^`\n]+`", park, text) | |
| text = re.sub(r"(?<!\*)\*([^*\n]+?)\*(?!\*)", r"_\1_", text) | |
| def header(m: re.Match) -> str: | |
| inner = re.sub(r"\*\*([^*]+)\*\*", r"\1", m.group(1).strip()) | |
| inner = re.sub(r"__([^_]+)__", r"\1", inner) | |
| return f"*{inner}*" | |
| text = re.sub(r"^#{1,6}[ \t]+(.+?)[ \t]*#*[ \t]*$", header, text, flags=re.MULTILINE) | |
| text = re.sub(r"\*\*([^*\n]+?)\*\*", r"*\1*", text) | |
| text = re.sub(r"__([^_\n]+?)__", r"*\1*", text) | |
| text = re.sub(r"~~([^~\n]+?)~~", r"~\1~", text) | |
| text = re.sub(r"\[([^\]\n]+)\]\(([^)\s]+)\)", r"<\2|\1>", text) | |
| text = re.sub(r"^[ \t]*(?:-{3,}|\*{3,}|_{3,})[ \t]*$", "", text, flags=re.MULTILINE) | |
| return re.sub(r"\x00CODE(\d+)\x00", lambda m: stash[int(m.group(1))], text) | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 1 and sys.argv[1] in ("-h", "--help"): | |
| print(__doc__) | |
| sys.exit(0) | |
| sys.stdout.write(convert(sys.stdin.read())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment