2016-09-19 17:48:38 +00:00
|
|
|
import { regexes } from './smileys';
|
2015-12-11 14:48:16 +00:00
|
|
|
|
2018-10-17 20:07:57 +00:00
|
|
|
/* eslint-disable no-useless-escape, max-len */
|
|
|
|
const replacePatterns = {
|
|
|
|
|
|
|
|
// URLs starting with http://, https://, or ftp://
|
|
|
|
'<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>':
|
|
|
|
/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,
|
|
|
|
|
|
|
|
// URLs starting with "www." (without // before it, or it'd re-link the ones done above).
|
|
|
|
'$1<a href="https://$2" target="_blank" rel="noopener noreferrer">$2</a>':
|
|
|
|
/(^|[^\/])(www\.[\S]+(\b|$))/gim,
|
|
|
|
|
|
|
|
// Change email addresses to mailto: links.
|
|
|
|
'<a href="mailto:$1">$1</a>':
|
|
|
|
/(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim
|
|
|
|
};
|
|
|
|
/* eslint-enable no-useless-escape, max-len */
|
|
|
|
|
2014-02-08 20:18:23 +00:00
|
|
|
/**
|
2018-08-29 17:24:25 +00:00
|
|
|
* Processes links and smileys in "body".
|
|
|
|
*
|
|
|
|
* @param {string} body - The message body.
|
|
|
|
* @returns {string} Message body with image tags and href tags.
|
2014-02-08 20:18:23 +00:00
|
|
|
*/
|
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
|
|
|
|
|
2018-10-17 20:07:57 +00:00
|
|
|
// non of the patterns we search contains a space, that's why we tokenize it
|
|
|
|
// and after processing each token we join it again with the results
|
|
|
|
// making sure we do only one replacement for a token
|
|
|
|
const tokens = body.split(' ');
|
|
|
|
const resultText = [];
|
2017-10-02 23:08:07 +00:00
|
|
|
|
2018-10-17 20:07:57 +00:00
|
|
|
for (const token of tokens) {
|
|
|
|
let replacedText;
|
|
|
|
const tokenLength = token.length;
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2018-10-17 20:07:57 +00:00
|
|
|
for (const newString in replacePatterns) { // eslint-disable-line guard-for-in, max-len
|
|
|
|
const replacePattern = replacePatterns[newString];
|
2017-10-12 23:02:29 +00:00
|
|
|
|
2018-10-17 20:07:57 +00:00
|
|
|
replacedText = token.replace(replacePattern, newString);
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2018-10-17 20:07:57 +00:00
|
|
|
// replacement was done, stop doing any other replacements
|
|
|
|
if (replacedText.length > tokenLength) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
replacedText = null;
|
|
|
|
}
|
2017-10-12 23:02:29 +00:00
|
|
|
|
2018-10-17 20:07:57 +00:00
|
|
|
// no replacement was done, then just check for smiley
|
|
|
|
if (!replacedText) {
|
|
|
|
replacedText = smilify(token);
|
|
|
|
}
|
2014-03-01 07:39:39 +00:00
|
|
|
|
2018-10-17 20:07:57 +00:00
|
|
|
resultText.push(replacedText);
|
|
|
|
}
|
2017-10-02 23:08:07 +00:00
|
|
|
|
2018-10-17 20:07:57 +00:00
|
|
|
return resultText.join(' ');
|
2014-02-08 20:18:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-29 17:24:25 +00:00
|
|
|
* Replaces common smiley strings with images.
|
|
|
|
*
|
|
|
|
* @param {string} body - The message body.
|
|
|
|
* @returns {string} Body returned with smiley replaced.
|
2014-02-08 20:18:23 +00:00
|
|
|
*/
|
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
|
|
|
}
|