Make sure we do only one replacement, not one over another for messages.

This commit is contained in:
damencho 2018-10-17 15:07:57 -05:00 committed by Saúl Ibarra Corretgé
parent 2a61968566
commit cd77a9176c
1 changed files with 41 additions and 25 deletions

View File

@ -1,5 +1,22 @@
import { regexes } from './smileys'; import { regexes } from './smileys';
/* 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 */
/** /**
* Processes links and smileys in "body". * Processes links and smileys in "body".
* *
@ -8,39 +25,38 @@ import { regexes } from './smileys';
*/ */
export function processReplacements(body) { export function processReplacements(body) {
// make links clickable + add smileys // make links clickable + add smileys
return smilify(linkify(body));
}
// 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 = [];
/** for (const token of tokens) {
* Finds and replaces all links in the links in "body" with an href tag. let replacedText;
* const tokenLength = token.length;
* @param {string} inputText - The message body.
* @returns {string} The text replaced with HTML tags for links.
*/
function linkify(inputText) {
let replacedText;
/* eslint-disable no-useless-escape, max-len */ for (const newString in replacePatterns) { // eslint-disable-line guard-for-in, max-len
const replacePattern = replacePatterns[newString];
// URLs starting with http://, https://, or ftp:// replacedText = token.replace(replacePattern, newString);
const replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>'); // replacement was done, stop doing any other replacements
if (replacedText.length > tokenLength) {
break;
}
replacedText = null;
}
// URLs starting with "www." (without // before it, or it'd re-link the ones done above). // no replacement was done, then just check for smiley
const replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim; if (!replacedText) {
replacedText = smilify(token);
}
replacedText = replacedText.replace(replacePattern2, '$1<a href="https://$2" target="_blank" rel="noopener noreferrer">$2</a>'); resultText.push(replacedText);
}
// Change email addresses to mailto: links. return resultText.join(' ');
const replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
/* eslint-enable no-useless-escape */
return replacedText;
} }
/** /**