Skip to content

Instantly share code, notes, and snippets.

@shivkanthb
Created January 25, 2017 02:47
Show Gist options
  • Save shivkanthb/bb0bcd90f56bbca92551574ab06f2879 to your computer and use it in GitHub Desktop.
Save shivkanthb/bb0bcd90f56bbca92551574ab06f2879 to your computer and use it in GitHub Desktop.
Remove the slack formatting on URL's, email addresses, channels, groups and user ids.
function removeSlackFormatting(input)
{
var text = input;
text = text.replace(/<([@#!])?([^>|]+)(?:\|([^>]+))?>/g, (function(_this) {
return function(m, type, link, label) {
var channel, user;
switch (type) {
case '@':
if (label) {
return label;
}
user = _this.client.getUserByID(link);
if (user) {
return "@" + user.name;
}
break;
case '#':
if (label) {
return label;
}
channel = _this.client.getChannelByID(link);
if (channel) {
return "\#" + channel.name;
}
break;
case '!':
if (link === 'channel' || link === 'group' || link === 'everyone') {
return "@" + link;
}
break;
default:
link = link.replace(/^mailto:/, '');
if (label && -1 === link.indexOf(label)) {
return label + " (" + link + ")";
} else {
return link;
}
}
};
})(this));
text = text.replace(/&lt;/g, '<');
text = text.replace(/&gt;/g, '>');
text = text.replace(/&amp;/g, '&');
return text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment