Writing Slack Messages with Markdown (Multiple Options)

  • infoFull Post Details
    info_outlineClick for Full Post Details
    Date Posted:
    Nov. 29, 2023
    Last Updated:
    Nov. 29, 2023
  • classTags
    classClick for Tags

TLDR

The short answer to “how do I write a Slack message in Markdown” is that there are two options:

  • Option A (not very good): Turn your global setting / preference for message formatting to “Format messages with markup” (documented here)
    • This is not very ideal, because it is not true Markdown, but rather their own markdown-adjacent syntax, and also requires that you globally disable the formatting toolbar / WYSIWYG (what-you-see-is-what-you-get) editing. More on this in my section on the pitfalls of this setting below.
  • Option B (my favorite!): There is a cool trick, where you can author your message in full Markdown, in any editor you chose and then get it into a Slack message. You need to first turn it into an HTML clipboard buffer, and then paste that into Slack. You can jump to that solution here, or read on for the background on how this works and why it is necessary.

Background

I want to be very clear: this post is about turning Markdown into a slack message that you send to someone, not the other way around, or anything to deal with their API.

Put another way: this is a solution for if you want to be able to copy and paste rich markdown into Slack and have it actually format the message correctly.

Why? Well,

  1. I like writing in Markdown
  2. For really long async messages, it makes me feel better to write it in a different editor (like VS Code), so the other person isn’t seeing “Joshua is typing…” for multiple minutes while I write (and sometimes rewrite) a message. I want to be able to paste from this other editor, into Slack.

Wait, I thought Slack Supported Pasting Markdown?

Ah, good question. Yes and no, but mostly no. Slack supports their “flavor” of a markdown like syntax (which they are referring to only as “markup”) which is a small subset of the typical Markdown syntax that most developers (like myself) are used to.

This alone wouldn’t be a deal breaker – many of the normal things you want regular Markdown syntax for (links, italics, etc.) are supported.

However – and here is my personal deal-breaker – in order to get the message editor to accept and parse Markdown (and again, not even full markdown, just their version / “markup”), you have to flip a global setting that then removes the formatting toolbar and WYSIWYG editing across Slack and forces you into only using “markup”, 100% of the time.

I haven’t tested this with every subset of their special syntax, but this is at least true for links.

To me, this is the worst of both worlds; I can get some markdown syntax in my editor, but it is an incomplete version of the Markdown spec, and at the same time, if I turn that on, then I can no longer do things like press CTRL + B to bold a section of text directly within the editor.

This post is the result of me searching for an alternative, and finding a “shortcut” that works surprisingly well!

Side note: One of the problems with Markdown itself is that the core original spec isn’t actually used directly all that much anymore; most of the time when people are talking about Markdown, they really mean a Markdown standard that is built on top of the original one – something like CommonMark, GitHub Flavored Markdown, etc.

The Solution / Shortcut

Here it is – the pro-tip – Slack doesn’t work well with pasting Markdown, but it does work well with pasted HTML!

By pasted HTML, I should also be clear that I don’t mean copying and pasting something like <b>Hello</b> with a standard copy-and-paste operation. Rather, it is pasting out of the special text/html buffer that clipboards can contain.

💡 Side note: If you weren’t aware, most operating system clipboards can contain a plain-text buffer, plus additional buffers with various file types, like RTF (rich text format), HTML, and even raw binary data. This is why you can do things like copy and paste images between applications. This isn’t documented very well – I have a WIP blog post about this that I’d like to publish as some point 😀.

💡 You can inspect your clipboard contents with this handy online tool – Clipboard Inspector

💡 This trick actually works with many different applications that don’t accept Markdown but do accept pasting, so it is a great one to keep in your back pocket. In fact, sometimes this even works with programs that act like they don’t support formatting at all, as a way to get things like bolded text when there isn’t a toolbar or keyboard shortcut to enable it.

Converting Markdown to HTML Clipboard Content

For how to convert your Markdown message to HTML and put it in your clipboard, there are a few options.

Personally, I often have VS Code’s Markdown Live Preview tab open as I author Markdown, so I can just copy and paste directly out of the preview window to get HTML to paste into Slack.

For a more automated solution, you can use scripting to A) Convert the markdown to HTML with something like marked, markdown-it, or pandoc, and B) Put the HTML in your clipboard with pbpaste (MacOS), clip (Window), or something like xclip (Linux).

Here is a shell script to do this on MacOS:

markdown_to_html_clip() {
    # Get markdown as arg, or fallback to clipboard contents
    md="$1"
    if [[ -z "$md" ]]; then
        md=$(pbpaste)
    fi
    html=$(npx marked --gfm -s "$md")
    html_hex=$(echo -n "$html" | hexdump -ve '1/1 "%.2x"')
    osascript <<- EOF
        set the clipboard to {«class HTML»:«data HTML${html_hex}», string:"${md}"}
    EOF
}

🔗 Functions for markdown conversion and clipboard manipulation, such as the above, are now in my dotfiles

Credit / Acknowledgments / Wrap Up

I’m definitely not the first person to realize this trick with Slack. Also, in particular the information about how to get HTML back into MacOS’s clipboard was hard to find, and I have this StackOverflow comment and blog post to thank for helping with that.

Also, I realize that my “shortcut” might not be the panacea that readers might be hoping for – obviously the most ideal solution would be for Slack to implement native Markdown support without requiring that it be an all-or-nothing setting; here’s hoping that they are actively working on that.

Leave a Reply

Your email address will not be published.