Last active
May 15, 2024 15:52
-
-
Save mrts/fbcef81a900226d663b14ae39485c7d3 to your computer and use it in GitHub Desktop.
Markdown to Slack
This file contains 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
# 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