Posting syntax-highlighted code snippets in your Slack messages (without 'snippets')

As I've written before, I quite like nicely rendered Slack mesages, which include full syntax highlighting of code snippets.

There was a discussion on reddit last night which brought this up again, which made me think about splitting this specific knowledge into a separate post, so it's more discoverable.

As I was preparing this, I found that Slack already supports posting syntax highlighted code snippets, but it's not super obvious πŸ‘€

To be clear, I'm not talking about using a "snippet", but the result of this gives you the syntax highlighted code snippet as a fully inline part of your message.

For instance, we want this sort of output:

A screenshot of a Slack message in the Slack app. It shows the text "Via https://learnxinyminutes.com/typescript" above a code snippet, which is syntax highlighted, showing it's TypeScript code, and has a "copy" button. Below, we see "And some plain code blocks:" followed by some un-highlighted code

I'd previously found that you needed to use the Slack Block Kit to get this rich rendering, but as I was looking at it this morning, I found that actually you don't even need that - Slack (at least as of client (4.51.x) supports it natively!

To get the above text, you can take this input:

Via https://learnxinyminutes.com/typescript:

```typescript
// Functions can accept more than one type
function f6(i: string | number): void {
  console.log("The value was " + i);
}

// Interfaces are structural, anything that has the properties is compliant with
// the interface
interface Person {
  name: string;
  // Optional properties, marked with a "?"
  age?: number;
  // And of course functions
  move(): void;
}
```

And some plain code blocks:

```
This isn't syntax highlighted
```

And paste it into the message box:

The Slack message editor in the Slack app, showing the Markdown snippet from above pasted into the "new message" box. However, it's detected that the TypeScript code snippet is exactly that, whereas the plain code block is not. Not pictured is the fact that you can move the snippet around in the editor with drag-and-drop, and hovering over it will show you a preview of the code.

Now, the important thing to note is that this requires you to paste this Markdown into the editor, you can't actually write:

```typescript

To get a TypeScript-highlighted code snippet.

Knowing that it's possible to do this more straightforwardly means that maybe I don't need my md2slack tool as much, at least until I want to be pasting tables into Slack.

I've tested this with Slack 4.51.x.

Written by Jamie Tanna's profile image Jamie Tanna 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.

This blog post is 100% human-written, and has nothing derived from LLMs (be it code, editorial support or prose).

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.