2015-09-11 02:42:15 +00:00
|
|
|
/* jshint -W101 */
|
2016-09-19 17:48:38 +00:00
|
|
|
import { regexes } from './smileys';
|
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
|
|
|
|
2016-09-19 17:48:38 +00:00
|
|
|
//Change email addresses to mailto: links.
|
2014-02-08 20:18:23 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-19 17:48:38 +00:00
|
|
|
for(var smiley in regexes) {
|
|
|
|
if(regexes.hasOwnProperty(smiley)) {
|
|
|
|
body = body.replace(regexes[smiley],
|
2014-10-10 10:57:00 +00:00
|
|
|
'<img class="smiley" src="images/smileys/' + smiley + '.svg">');
|
|
|
|
}
|
|
|
|
}
|
2014-03-01 07:39:39 +00:00
|
|
|
|
|
|
|
return body;
|
|
|
|
}
|