Best answer

Does anyone know how to convert markup text for Slack?

  • 5 August 2020
  • 5 replies
  • 330 views

Userlevel 3

Hey All,

I’m pulling source data from an app that only outputs markup text. The markup text has zero or many links and needs to be formatted for Slack’s annoyingly specific markeup language.

For example, changing all links in a body of text from this:

[Text to make into a link](http://www.zapier.com)

To this:

<http://www.zapier.com|Text to make into a link>

I’ve tried using Formatter > Extract Pattern, but that only works for the first match (this thread suggested this is possible with a code block which brought me here).

Thanks

~Brian

icon

Best answer by ikbelkirasan 5 August 2020, 19:10

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.

5 replies

Userlevel 7
Badge +9

Hey! Yes - This would be possible with a code block -Can you give an example of a more difficult conversion you are trying to make please?

Userlevel 7
Badge +11

@brianswichkow - You might want to try the following code snippet. Make sure to map the input text to an input field called text.

const { text } = inputData;const regex = /(\[[^\]]+\])(\([^\)]+\))/g;const result = text.replace(regex, (_, match1, match2) => {  const m = match1.replace(/[\[\]]/g, "");  const n = match2.replace(/[\(\)]/g, "");  return `<${m}|${n}>`;});output = [{ result }];

 

Userlevel 3

Holy hell, @ikbelkirasan, thank you! 

 

The code works! One minor change in ordering...

 

The output is <text|link> and should be <link|text>.

 

I took a few wild guesses, but couldn’t get it quite right.

 

 

 

Userlevel 7
Badge +11

@brianswichkow - Awesome! I’ve just made a fix. Feel free to give it a try.

const { text } = inputData;const regex = /(\[[^\]]+\])(\([^\)]+\))/g;const result = text.replace(regex, (_, match1, match2) => {  const txt = match1.replace(/[\[\]]/g, "");  const link = match2.replace(/[\(\)]/g, "");  return `<${link}|${txt}>`;});output = [{ result }];
Userlevel 3

@ikbelkirasan got it on the ~5th wild guess! Thank you!

 

If you PM me your email, we’d love to give you R$ for this gift.


Changed it to...

const { text } = inputData;const regex = /(\[[^\]]+\])(\([^\)]+\))/g;const result = text.replace(regex, (_, match2, match1) => {  const m = match2.replace(/[\[\]]/g, "");  const n = match1.replace(/[\(\)]/g, "");  return `<${n}|${m}>`;});output = [{ result }];

Output...