refactored the highlightUsers-method, only matches mentions and not emails (#10982)

This commit is contained in:
negue
2019-02-10 19:27:22 +01:00
committed by Matteo Pagliazzi
parent 0155491a68
commit b9aaccdf13
2 changed files with 33 additions and 20 deletions

View File

@@ -1,18 +1,24 @@
import escapeRegExp from 'lodash/escapeRegExp';
const optionalAnchorTagRegExStr = '(<\\w[^>]*)?'; // everything including the anchor tag is recognized
const mentionRegExStr = '(@[\\w-]+)';
const optionalPostMentionRegExStr = '(\\.\\w+)?'; // like dot-TLD
const finalMentionRegEx = new RegExp(`${optionalAnchorTagRegExStr}${mentionRegExStr}${optionalPostMentionRegExStr}`, 'gi');
export function highlightUsers (text, userName, displayName) {
const findAnyMentionRegex = '@[\\w-]+(?:\\b)';
const currentUser = [`@${userName}`, `@${displayName}`].map(escapeRegExp);
const atRegex = new RegExp(`${findAnyMentionRegex}`, 'gi');
const currentUser = [`@${userName}`, `@${displayName}`];
if (atRegex.test(text)) {
text = text.replace(atRegex, match => {
if (currentUser.includes(match)) {
return `<span class="at-highlight at-text">${match}</span>`;
}
text = text.replace(finalMentionRegEx, (fullMatched, preMention, mentionStr, postMention) => {
if (preMention && preMention.includes('<a') || Boolean(postMention)) {
return fullMatched;
}
return `<span class="at-text">${match}</span>`;
});
}
const isUserMention = currentUser.includes(mentionStr) ? 'at-text' : '';
return fullMatched.replace(mentionStr, `<span class="at-highlight ${isUserMention}">${mentionStr}</span>`);
});
return text;
}