20

Trying to figure out a way to upload a Markdown file to be a formatted post in a channel. Is there an API call to do this?

The files.upload seems to only support Markdown raw upload.

d219
  • 2,707
  • 5
  • 31
  • 36
kickingvegas
  • 409
  • 1
  • 3
  • 6

3 Answers3

7

Not 100% sure what you mean by "Markdown raw upload" vs "Markdown file" but files.upload works with a .md file. You seem to have gotten this to work:

curl -F filetype=post -F content="# [like this](https://someurl)" -F channels=C1.....7L -F token=xoxp-... https://slack.com/api/files.upload

... now swap content="..." for file=@post.md

curl -F filetype=post -F file=@post.md -F channels=C1.....7L -F token=xoxp-... https://slack.com/api/files.upload

What this does is convert a standard MD file (e.g. from github) into a Slack Post document. It will try to keep all formatting, like headlines, code, etc.

However, keep in mind that Slack only supports a subset of MD, so e.g. tables will not be displayed correctly.

Reuben
  • 143
  • 1
  • 6
5

I'm using the following Python script to translate the most useful subset of Markdown to Slack format. It replaces:

  • hyphened lists with bullet symbols
  • double bold marker asterisks ** with single asterisk *
  • headers # with bold marker asterisks *

The script assumes that lists are indented with two spaces and that single underscores _ are used for italic in Markdown, so it 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)

The result might be good enough for most purposes.

Run the script with

python markdown-to-slack.py filename.md

Result will be in filename.md.slack.

mrts
  • 16,697
  • 8
  • 89
  • 72
3

Looks like no.

From the Slack help, the correct way to format lists is using Shift+Enter, and pasting the actual bullet point character (•). It won't convert your asterisks for you (for example).

Seems weirdly basic, considering how Slack are using some markdown functionality, and how widespread the use of Slack is! I guess I should make a feature request?

Ian Grainger
  • 5,148
  • 3
  • 46
  • 72
  • In case of use, although the OP is talking about the API (see from comment on one of the other posts that you've realised this), if you did want to use the Markdown which Slack does use you can through changing preferences - see https://www.howtogeek.com/450030/how-to-enable-classic-markdown-text-formatting-in-slack/ – d219 Jan 21 '21 at 21:31