Skip to content

Commit

Permalink
ignore mentions inside markdown links
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Dec 5, 2024
1 parent b7e1dd3 commit 140c523
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
10 changes: 8 additions & 2 deletions apps/meteor/app/mentions/lib/MentionsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,17 @@ export class MentionsParser {
return this.roomTemplate({ prefix, reference, channel, mention });
});

getUserMentions(str: string) {
getUserMentions(msg: string) {
// First remove the text inside md links
const str = msg.replace(/\[[^\]]*\]\([^)]+\)/g, '');
// Then do the match
return (str.match(this.userMentionRegex) || []).map((match) => match.trim());
}

getChannelMentions(str: string) {
getChannelMentions(msg: string) {
// First remove the text inside md links
const str = msg.replace(/\[[^\]]*\]\([^)]+\)/g, '');
// Then do the match
return (str.match(this.channelMentionRegex) || []).map((match) => match.trim());
}

Expand Down
60 changes: 60 additions & 0 deletions apps/meteor/tests/unit/app/mentions/server.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,64 @@ describe('Mention Server', () => {
expect(result).to.be.deep.equal(expected);
});
});

describe('getUserMentions', () => {
describe('for message with only an md link', () => {
const result = [];
[
'[@rocket.cat](https://rocket.chat)',
'[@rocket.cat](https://rocket.chat) hello',
'[@rocket.cat](https://rocket.chat) hello how are you?',
'[test](https://rocket.chat)',
].forEach((text) => {
it(`should return "${JSON.stringify(result)}" from "${text}"`, () => {
expect(result).to.be.deep.equal(mention.getUserMentions(text));
});
});
});

describe('for message with md link and text', () => {
const result = ['@sauron'];
[
'@sauron please work on [user@password](https://rocket.chat)',
'@sauron hello [user@password](https://rocket.chat) hello',
'[user@password](https://rocket.chat) hello @sauron',
'@sauron please work on [user@password](https://rocket.chat) hello',
].forEach((text) => {
it(`should return "${JSON.stringify(result)}" from "${text}"`, () => {
expect(result).to.be.deep.equal(mention.getUserMentions(text));
});
});
});
});

describe('getChannelMentions', () => {
describe('for message with md link', () => {
const result = [];
[
'[#general](https://rocket.chat)',
'[#general](https://rocket.chat) hello',
'[#general](https://rocket.chat) hello how are you?',
'[test #general #other](https://rocket.chat)',
].forEach((text) => {
it(`should return "${JSON.stringify(result)}" from "${text}"`, () => {
expect(result).to.be.deep.equal(mention.getChannelMentions(text));
});
});
});

describe('for message with md link and text', () => {
const result = ['#somechannel'];
[
'#somechannel please [user#password](https://rocket.chat)',
'#somechannel hello [user#password](https://rocket.chat) hello',
'[user#password](https://rocket.chat) hello #somechannel',
'#somechannel join [#general on #other](https://rocket.chat)',
].forEach((text) => {
it(`should return "${JSON.stringify(result)}" from "${text}"`, () => {
expect(result).to.be.deep.equal(mention.getChannelMentions(text));
});
});
});
});
});

0 comments on commit 140c523

Please sign in to comment.