Files
habitica/website/client/libs/highlightUsers.js
Sabe Jones 98fd509530 Hotfix: correct issues from PRs rollout (#10993)
* fix(various): correct issues from PRs rollout
1. Send Gems modal opens from profiles again
2. Contributor titles appear on hover again
3. Tags dropdown in tasks appears again
4. Only user's own @mentions get highlighted again

* fix(test): correct order of css classes in expect
2019-02-15 16:01:33 -06:00

24 lines
942 B
JavaScript

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 currentUser = [`@${userName}`, `@${displayName}`].map(escapeRegExp);
text = text.replace(finalMentionRegEx, (fullMatched, preMention, mentionStr, postMention) => {
if (preMention && preMention.includes('<a') || Boolean(postMention)) {
return fullMatched;
}
const isUserMention = currentUser.includes(mentionStr) ? 'at-highlight' : '';
return fullMatched.replace(mentionStr, `<span class="at-text ${isUserMention}">${mentionStr}</span>`);
});
return text;
}