Advertisement
z3ntu

slack_links.js

Aug 31st, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module.exports = (webhook, matrix) => {
  2.     // Reference: https://api.slack.com/docs/message-formatting#linking_to_urls
  3.     // Slack also accepts e.g. <example.com|DISPLAY_STR> but that results in a relative path
  4.  
  5.     // Match <PROTOCOL://REST_OF_URL|DISPLAY_STR>
  6.     const linkRegex = /<([a-zA-Z]+):\/\/([^|>]+?)\|([^|>]+?)>/g;
  7.     // Match <PROTOCOL://REST_OF_URL>
  8.     const linkRegex2 = /<([a-zA-Z]+):\/\/([^|>]+?)>/g;
  9.     // Match <mailto:ADDRESS|DISPLAY_STR>
  10.     const mailtoRegex = /<mailto:([^|>]+?)\|([^|>]+?)>/g;
  11.     // Match <mailto:ADDRESS>
  12.     const mailtoRegex2 = /<mailto:([^|>]+?)>/g;
  13.  
  14.     // Apply regex'es
  15.     matrix.event.body = matrix.event.body.replace(linkRegex, "<a href='$1://$2'>$3</a>");
  16.     matrix.event.body = matrix.event.body.replace(linkRegex2, "<a href='$1://$2'>$1://$2</a>");
  17.     matrix.event.body = matrix.event.body.replace(mailtoRegex, "<a href='mailto:$1'>$2</a>");
  18.     matrix.event.body = matrix.event.body.replace(mailtoRegex2, "<a href='mailto:$1'>mailto:$1</a>");
  19.  
  20.     // TODO: Put this somewhere else
  21.     // Handle Slack Multiline Messages
  22.     matrix.event.body = matrix.event.body.replace('\n', '<br>');
  23. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement