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) {
|
2017-10-12 23:02:29 +00:00
|
|
|
// make links clickable + add smileys
|
|
|
|
return smilify(linkify(body));
|
2014-02-08 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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) {
|
2017-10-12 23:02:29 +00:00
|
|
|
let replacedText;
|
|
|
|
|
|
|
|
/* eslint-disable no-useless-escape, max-len */
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2017-10-12 23:02:29 +00:00
|
|
|
// URLs starting with http://, https://, or ftp://
|
|
|
|
const replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
|
2017-10-02 23:08:07 +00:00
|
|
|
|
2017-07-05 20:15:10 +00:00
|
|
|
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>');
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2017-10-12 23:02:29 +00:00
|
|
|
// URLs starting with "www." (without // before it, or it'd re-link the ones done above).
|
|
|
|
const replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
|
|
|
|
|
2017-07-05 20:39:53 +00:00
|
|
|
replacedText = replacedText.replace(replacePattern2, '$1<a href="https://$2" target="_blank" rel="noopener noreferrer">$2</a>');
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2017-10-12 23:02:29 +00:00
|
|
|
// Change email addresses to mailto: links.
|
|
|
|
const replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
|
|
|
|
|
2014-02-08 20:18:23 +00:00
|
|
|
replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2017-10-02 23:08:07 +00:00
|
|
|
/* eslint-enable no-useless-escape */
|
|
|
|
|
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) {
|
2017-10-12 23:02:29 +00:00
|
|
|
if (!body) {
|
2014-02-08 20:18:23 +00:00
|
|
|
return body;
|
2014-10-10 10:57:00 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 23:02:29 +00:00
|
|
|
let formattedBody = body;
|
|
|
|
|
|
|
|
for (const smiley in regexes) {
|
|
|
|
if (regexes.hasOwnProperty(smiley)) {
|
|
|
|
formattedBody = formattedBody.replace(regexes[smiley],
|
|
|
|
`<img class="smiley" src="images/smileys/${smiley}.svg">`);
|
2014-10-10 10:57:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2018-02-12 21:12:35 +00:00
|
|
|
return formattedBody;
|
2014-03-01 07:39:39 +00:00
|
|
|
}
|