2015-09-11 02:42:15 +00:00
|
|
|
/* jshint -W101 */
|
2015-01-07 14:54:03 +00:00
|
|
|
var Smileys = require("./smileys.json");
|
2015-12-11 14:48:16 +00:00
|
|
|
|
2014-02-08 20:18:23 +00:00
|
|
|
/**
|
|
|
|
* Processes links and smileys in "body"
|
|
|
|
*/
|
2015-12-11 14:48:16 +00:00
|
|
|
export function processReplacements(body) {
|
2014-02-08 20:18:23 +00:00
|
|
|
//make links clickable
|
|
|
|
body = linkify(body);
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2014-02-08 20:18:23 +00:00
|
|
|
//add smileys
|
|
|
|
body = smilify(body);
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2014-02-08 20:18:23 +00:00
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-03-01 07:39:39 +00:00
|
|
|
* Finds and replaces all links in the links in "body"
|
2014-02-08 20:18:23 +00:00
|
|
|
* with their <a href=""></a>
|
|
|
|
*/
|
2015-12-11 14:48:16 +00:00
|
|
|
export function linkify(inputText) {
|
2014-02-08 20:18:23 +00:00
|
|
|
var replacedText, replacePattern1, replacePattern2, replacePattern3;
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2014-02-08 20:18:23 +00:00
|
|
|
//URLs starting with http://, https://, or ftp://
|
|
|
|
replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
|
|
|
|
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2014-02-08 20:18:23 +00:00
|
|
|
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
|
|
|
|
replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
|
|
|
|
replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2014-02-08 20:18:23 +00:00
|
|
|
//Change email addresses to mailto:: links.
|
|
|
|
replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
|
|
|
|
replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2014-02-08 20:18:23 +00:00
|
|
|
return replacedText;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces common smiley strings with images
|
|
|
|
*/
|
2015-12-11 14:48:16 +00:00
|
|
|
function smilify(body) {
|
2014-10-10 10:57:00 +00:00
|
|
|
if(!body) {
|
2014-02-08 20:18:23 +00:00
|
|
|
return body;
|
2014-10-10 10:57:00 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 03:05:53 +00:00
|
|
|
var regexs = Smileys.regexs;
|
2014-10-10 10:57:00 +00:00
|
|
|
for(var smiley in regexs) {
|
|
|
|
if(regexs.hasOwnProperty(smiley)) {
|
|
|
|
body = body.replace(regexs[smiley],
|
|
|
|
'<img class="smiley" src="images/smileys/' + smiley + '.svg">');
|
|
|
|
}
|
|
|
|
}
|
2014-03-01 07:39:39 +00:00
|
|
|
|
|
|
|
return body;
|
|
|
|
}
|