Best answer

What is the best way to convert Slack markup to HTML?

  • 23 May 2022
  • 7 replies
  • 1521 views

Userlevel 1

Searching over the forums and Google I’ve found more than a few threads of individuals converting Gmail contents and/or generic HTML for Slack markdown, but I haven’t been able to find anything about the opposite. 

For the most part there isn’t a problem with just taking the text (or raw text) of the Slack message and inserting it as the body of the Gmail action, with the exception of links. For example, a link formatted for Slack will come out as the following raw text. 

<https://www.example.com/|Example Link>

Does anyone have a clever solution for converting this into something more elegant for plaintext or HTML output? 

icon

Best answer by jaredwa 26 May 2022, 09:15

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

7 replies

Userlevel 7
Badge +14

Hi @jaredwa 

Good question.

You can try using the Formatter > Text > Markdown to HTML option: https://zapier.com/apps/formatter/help

Userlevel 1

Hi @jaredwa 

Good question.

You can try using the Formatter > Text > Markdown to HTML option: https://zapier.com/apps/formatter/help

Thank you for the reply. 

Unfortunately, because Slack markdown is (presumably) substantially different from what the Markdown to HTML option expects from markdown, this results in equally broken output. For example, a message like the following,

Example paragraph 1
Example paragraph 2, <https://www.example.com/|Example Link> goes here。

will unfortunately result in some mostly unusable HTML.

<p>Example paragraph 1</p>
<p>Example paragraph 2, <a href="https://www.example.com/|Example Link">https://www.example.com/|Example Link</a>goes here。</p>

While not a total waste of an action, I would have to go in with a script after to add line breaks and fix all of the links, and were it possible I was hoping for a more elegant solution. 

Userlevel 7
Badge +12

Hi @jaredwa!

I found this info from Slack on how to remove formatting

  1. Detect all sub-strings matching <(.*?)>
  2. Within those sub-strings, format content starting with #C as a channel link
  3. Format content starting with @U or @W as a user mention
  4. Format content starting with !subteam as a user group mention
  5. Format content starting with ! according to the rules for special mentions
  6. For any other content within those sub-strings, format as a URL link
  7. Once the format has been determined, check for a pipe (|) - if present, use the text following the pipe as the label for the link or mention.

If you’re good with code, you might be able to whip up a bit of javascript that handles that, but I’m afraid that I don’t know how 🙈

Userlevel 1

Hi @jaredwa!

I found this info from Slack on how to remove formatting

  1. Detect all sub-strings matching <(.*?)>
  2. Within those sub-strings, format content starting with #C as a channel link
  3. Format content starting with @U or @W as a user mention
  4. Format content starting with !subteam as a user group mention
  5. Format content starting with ! according to the rules for special mentions
  6. For any other content within those sub-strings, format as a URL link
  7. Once the format has been determined, check for a pipe (|) - if present, use the text following the pipe as the label for the link or mention.

If you’re good with code, you might be able to whip up a bit of javascript that handles that, but I’m afraid that I don’t know how 🙈

Thank you for the idea. While not a Javascript whiz by any stretch of the imagination, I whipped up a short script to fix HTTPS links into standard markdown so that the Markdown to HTML option could be used. 

const { text } = inputData;
const regex = /<(https?:\/\/[\w-]+(?:\.[\w]+)+(?:\/[\w?=%&@$#_.+]+)*\/?)(?:\|((?:[^>])+))?>/g;
const result = text.replace(regex, (`[$2]($1)`));
output = [{ result }];

Thanks to everyone for helping sort this one out!

Userlevel 7
Badge +12

Awesome, thanks so much for sharing your the script you used! 🙌🏻

Hi @jaredwa!

I found this info from Slack on how to remove formatting

  1. Detect all sub-strings matching <(.*?)>
  2. Within those sub-strings, format content starting with #C as a channel link
  3. Format content starting with @U or @W as a user mention
  4. Format content starting with !subteam as a user group mention
  5. Format content starting with ! according to the rules for special mentions
  6. For any other content within those sub-strings, format as a URL link
  7. Once the format has been determined, check for a pipe (|) - if present, use the text following the pipe as the label for the link or mention.

If you’re good with code, you might be able to whip up a bit of javascript that handles that, but I’m afraid that I don’t know how 🙈

Thank you for the idea. While not a Javascript whiz by any stretch of the imagination, I whipped up a short script to fix HTTPS links into standard markdown so that the Markdown to HTML option could be used. 

const { text } = inputData;
const regex = /<(https?:\/\/[\w-]+(?:\.[\w]+)+(?:\/[\w?=%&@$#_.+]+)*\/?)(?:\|((?:[^>])+))?>/g;
const result = text.replace(regex, (`[$2]($1)`));
output = [{ result }];

Thanks to everyone for helping sort this one out!

Any insight as to how to have it replace every URL it finds instead of only the first occurrence? 

Userlevel 1

Any insight as to how to have it replace every URL it finds instead of only the first occurrence? 

As written I haven’t had a problem at described; it’s replaced every URL.

However, I did have a few separate problems with links including unicode characters, mailto links, channels, @ mentions and whatnot. Here’s the updated script I’m using. The penultimate line is just adding linebreaks for readability. 

const { text } = inputData;
const slackAtFix = text.replace(/<((?:@|!)(?:.*?))>/g, (`$1`));
const slackChannelFix = slackAtFix.replace(/<#(?:.*?)\|(.*?)>/g, (`#$1`));
const linkFix = slackChannelFix.replace(/<(https?:\/\/[\w-]+(?:\.[\w]+)+(?:\/[^|]+)*\/?)(?:\|((?:[^>])+))?>/g, (`[$2]($1)`));
const mailtoFix = linkFix.replace(/<(mailto?:(?:[^|>])+)?(?:\|((?:[^>])+))?>/g, (`[$2]($1)`));
const result = mailtoFix.replace(/(?:\r\n|\r|\n)/g, '<br>');
output = [{ result }];

Unsure if this will help you or not, but cheers.