Created
January 25, 2017 02:47
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(/</g, '<'); | |
text = text.replace(/>/g, '>'); | |
text = text.replace(/&/g, '&'); | |
return text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment