2019-02-10 18:27:22 +00:00
|
|
|
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');
|
|
|
|
|
|
2019-02-07 16:55:36 +00:00
|
|
|
export function highlightUsers (text, userName, displayName) {
|
2019-02-10 18:27:22 +00:00
|
|
|
const currentUser = [`@${userName}`, `@${displayName}`].map(escapeRegExp);
|
|
|
|
|
|
|
|
|
|
text = text.replace(finalMentionRegEx, (fullMatched, preMention, mentionStr, postMention) => {
|
|
|
|
|
if (preMention && preMention.includes('<a') || Boolean(postMention)) {
|
|
|
|
|
return fullMatched;
|
|
|
|
|
}
|
2019-02-07 16:55:36 +00:00
|
|
|
|
2019-02-15 22:01:33 +00:00
|
|
|
const isUserMention = currentUser.includes(mentionStr) ? 'at-highlight' : '';
|
2019-02-07 16:55:36 +00:00
|
|
|
|
2019-02-15 22:01:33 +00:00
|
|
|
return fullMatched.replace(mentionStr, `<span class="at-text ${isUserMention}">${mentionStr}</span>`);
|
2019-02-10 18:27:22 +00:00
|
|
|
});
|
2019-02-07 16:55:36 +00:00
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|