mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-07-13 15:38:59 +00:00
Fix issue with username highlighting
This commit is contained in:
parent
0271d98b91
commit
f65240c217
2 changed files with 10 additions and 3 deletions
|
|
@ -17,6 +17,8 @@ describe.only('highlightMentions', () => {
|
|||
auth: { local: { username: 'user' } }, _id: '111',
|
||||
}, { auth: { local: { username: 'user2' } }, _id: '222',
|
||||
}, { auth: { local: { username: 'user3' } }, _id: '333',
|
||||
}, { auth: { local: { username: 'user-dash' } }, _id: '444',
|
||||
}, { auth: { local: { username: 'user_underscore' } }, _id: '555',
|
||||
},
|
||||
]);
|
||||
},
|
||||
|
|
@ -37,7 +39,12 @@ describe.only('highlightMentions', () => {
|
|||
it('highlights existing users', async () => {
|
||||
let text = '@user: message';
|
||||
let highlightedText = await highlightMentions(text);
|
||||
expect(highlightedText).to.equal('[@user](https://habitica.com/members/111): message');
|
||||
expect(highlightedText).to.equal('[@user](/profile/111): message');
|
||||
});
|
||||
it('highlights special characters', async () => {
|
||||
let text = '@user-dash: message @user_underscore';
|
||||
let highlightedText = await highlightMentions(text);
|
||||
expect(highlightedText).to.equal('[@user-dash](/profile/444): message [@user_underscore](/profile/555)');
|
||||
});
|
||||
it('doesn\'t highlight nonexisting users', async () => {
|
||||
let text = '@nouser message';
|
||||
|
|
@ -47,7 +54,7 @@ describe.only('highlightMentions', () => {
|
|||
it('highlights multiple existing users', async () => {
|
||||
let text = '@user message (@user2) @user3 @user';
|
||||
let highlightedText = await highlightMentions(text);
|
||||
expect(highlightedText).to.equal('[@user](https://habitica.com/members/111) message ([@user2](https://habitica.com/members/222)) [@user3](https://habitica.com/members/333) [@user](https://habitica.com/members/111)');
|
||||
expect(highlightedText).to.equal('[@user](/profile/111) message ([@user2](/profile/222)) [@user3](/profile/333) [@user](/profile/111)');
|
||||
});
|
||||
it('doesn\'t highlight more than 5 users', async () => {
|
||||
let text = '@user @user2 @user3 @user4 @user5 @user6';
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ export async function highlightMentions (text) {
|
|||
.exec();
|
||||
members.forEach((member) => {
|
||||
const username = member.auth.local.username;
|
||||
text = text.replace(new RegExp(`@${username}\\b`, 'g'), `[@${username}](/members/${member._id})`);
|
||||
text = text.replace(new RegExp(`@${username}(?![\\-\\w])`, 'g'), `[@${username}](/profile/${member._id})`);
|
||||
});
|
||||
}
|
||||
return text;
|
||||
|
|
|
|||
Loading…
Reference in a new issue