Authoring Markdown externally and pasting the 'pretty' output into Slack (on Linux)

As someone with someone with ADHD, I find that introducing context switching for my "muscle memory" to be a little jarring, and is why I've found in the past that using something "with Vim keybindings" to be not-quite-the-same as actual Vim/Neovim.

I'll also find that when writing longer docs or messages, I'll lean on using Neovim (for the superior editing process) compared to something like Slack's editor.

In this case, I'll write them in Neovim, with Slack's Markdown-ish syntax, and then paste them into Slack when ready.

Unfortunately, Slack doesn't always suggest to "apply formatting" to the message, so I'll end up having raw Markdown links, or *Action Required* instead of Action Required.

Over the last couple of days I've been looking into how to better work this to make it more of a reasonable workflow, and done a bit of a deep-dive into the way that X11/Xorg clipboards work.

When copying text to the clipboard in Linux, there are a few "targets" that can get populated, based on the MIME type of the data, if the application that's interacting with the clipboard marks it as such:

# to list all known targets
$ xclip -o -t TARGETS -sel clipboard
TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
text/html
text/_moz_htmlcontext
text/_moz_htmlinfo
UTF8_STRING
COMPOUND_TEXT
TEXT
STRING
text/plain;charset=utf-8
text/plain
text/x-moz-url-priv

# for the HTML version of this
$ xclip -o -t text/html -sel clipboard
<meta http-equiv="content-type" content="text/html; charset=utf-8">Hi, I'm <a class="p-name u-uid u-url" rel="me" href="https://www.jvt.me"><span class="p-given-name">Jamie</span>

# for the plain-text (but may include some HTML)
$ xclip -o -t text/plain -sel clipboard
<span class="p-family-name">Tanna</span></a> Hi, I'm Jamie Tanna

# for a pure plain-text version of the string
$ xclip -o -t STRING -sel clipboard
Hi, I'm Jamie Tanna

With this in mind, I thought "oh great, all I need to do is copy text into the text/html target, and that should work"? Unfortunately this didn't work:

# this involves writing Markdown in a /real/ Markdown way, so we get bold/italics correctly, which then will be parsed by Pandoc, and converted to HTML, which should then be parsed correctly by Slack
pandoc /path/to/file.md -t html | xclip -sel clipboard -t text/html

You'll have to take my word for it, but Slack didn't seem to allow pasting after I did this, even though it looked alright.

Doing some continued digging, I found this issue on the xclip repo and a PR to make it possible to register multiple targets.

When building the - currently unreleased - changes locally (via the AUR package), we can now run:

# NOTE that we could wrap this to have the plain-text from `pandoc`, but right now let's leave it as "Markdown"
pandoc /path/to/file.md -t html | xclip -sel clipboard -t text/html -alt-text Markdown

From here, this now allows us to paste into Slack πŸš€

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #slack #markdown #linux.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.