Skip to content

Instantly share code, notes, and snippets.

@mrts
Last active May 15, 2024 15:52
Show Gist options
  • Save mrts/fbcef81a900226d663b14ae39485c7d3 to your computer and use it in GitHub Desktop.
Save mrts/fbcef81a900226d663b14ae39485c7d3 to your computer and use it in GitHub Desktop.
Markdown to Slack
# Translates Markdown syntax to Slack, replaces:
#
# - hyphened lists with bullet symbols
# - double bold marker asterisks `**` with single asterisk `*`
# - headers `#` with bold marker asterisks `*`
#
# Run with
#
# python markdown-to-slack.py filename.md
#
# Result will be in filename.md.slack.
# Assumes that lists are indented with two spaces and underscore '_'
# is used for italic, which is already compatible with Slack.
import re
import sys
REGEX_REPLACE = (
(re.compile('^- ', flags=re.M), '• '),
(re.compile('^ - ', flags=re.M), ' ◦ '),
(re.compile('^ - ', flags=re.M), ' ⬩ '), # ◆
(re.compile('^ - ', flags=re.M), ' ◽ '),
(re.compile('^#+ (.+)$', flags=re.M), r'*\1*'),
(re.compile('\*\*'), '*'),
)
def main(i, o):
s = i.read()
for regex, replacement in REGEX_REPLACE:
s = regex.sub(replacement, s)
o.write(s)
if __name__ == '__main__':
with open(sys.argv[1], encoding='utf-8') as i, \
open(sys.argv[1] + '.slack', 'w', encoding='utf-8') as o:
main(i, o)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment