Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to upload a Markdown file as a Slack post?

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.

like image 709
kickingvegas Avatar asked Dec 21 '16 20:12

kickingvegas


People also ask

Can you use Markdown in Slack?

Slack is a popular team messaging and collaboration application that supports a subset of the Markdown syntax. Different parts of the interface provide different levels of Markdown support.

How do I render a Markdown in Slack?

To do so, open Slack's preferences menu by clicking the menu at the top-left corner of the Slack window and selecting “Preferences.” Click “Advanced” in the left pane and enable “Format messages with markup” under Input Options. The change will take effect immediately.


2 Answers

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 [email protected]

curl -F filetype=post -F [email protected] -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.

like image 191
Reuben Avatar answered Sep 30 '22 19:09

Reuben


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.

like image 31
mrts Avatar answered Sep 30 '22 17:09

mrts