Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
MM-15749 Allow channel links in brackets (#2957)
Browse files Browse the repository at this point in the history
* MM-15749 Allow channel links in brackets

* Use toBe instead of toEqual
  • Loading branch information
hmhealey committed Jun 17, 2019
1 parent 0d4036d commit e81371d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
8 changes: 4 additions & 4 deletions utils/text_formatting.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,13 @@ function autolinkChannelMentions(text, tokens, channelNamesMap, team) {
return alias;
}

function replaceChannelMentionWithToken(fullMatch, spacer, mention, channelName) {
function replaceChannelMentionWithToken(fullMatch, mention, channelName) {
let channelNameLower = channelName.toLowerCase();

if (channelMentionExists(channelNameLower)) {
// Exact match
const alias = addToken(channelNameLower, mention, escapeHtml(channelNamesMap[channelNameLower].display_name));
return spacer + alias;
return alias;
}

// Not an exact match, attempt to truncate any punctuation to see if we can find a channel
Expand All @@ -231,7 +231,7 @@ function autolinkChannelMentions(text, tokens, channelNamesMap, team) {
const suffix = originalChannelName.substr(c - 1);
const alias = addToken(channelNameLower, '~' + channelNameLower,
escapeHtml(channelNamesMap[channelNameLower].display_name));
return spacer + alias + suffix;
return alias + suffix;
}
} else {
// If the last character is not punctuation, no point in going any further
Expand All @@ -243,7 +243,7 @@ function autolinkChannelMentions(text, tokens, channelNamesMap, team) {
}

let output = text;
output = output.replace(/(^|\s)(~([a-z0-9.\-_]*))/gi, replaceChannelMentionWithToken);
output = output.replace(/\B(~([a-z0-9.\-_]*))/gi, replaceChannelMentionWithToken);

return output;
}
Expand Down
68 changes: 47 additions & 21 deletions utils/text_formatting_channel_links.test.jsx
Original file line number Diff line number Diff line change
@@ -1,69 +1,95 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import assert from 'assert';

import * as TextFormatting from 'utils/text_formatting.jsx';

describe('TextFormatting.ChannelLinks', () => {
it('Not channel links', (done) => {
assert.equal(
TextFormatting.formatText('~123').trim(),
test('Not channel links', () => {
expect(
TextFormatting.formatText('~123').trim()
).toBe(
'<p>~123</p>'
);

assert.equal(
TextFormatting.formatText('~town-square').trim(),
expect(
TextFormatting.formatText('~town-square').trim()
).toBe(
'<p>~town-square</p>'
);

done();
});

describe('Channel links', () => {
afterEach(() => {
delete window.basename;
});

it('should link ~town-square', () => {
assert.equal(
test('should link ~town-square', () => {
expect(
TextFormatting.formatText('~town-square', {
channelNamesMap: {'town-square': {display_name: 'Town Square'}},
team: {name: 'myteam'},
}).trim(),
}).trim()
).toBe(
'<p><a class="mention-link" href="/myteam/channels/town-square" data-channel-mention="town-square">~Town Square</a></p>'
);
});

it('should link ~town-square followed by a period', () => {
assert.equal(
test('should link ~town-square followed by a period', () => {
expect(
TextFormatting.formatText('~town-square.', {
channelNamesMap: {'town-square': {display_name: 'Town Square'}},
team: {name: 'myteam'},
}).trim(),
}).trim()
).toBe(
'<p><a class="mention-link" href="/myteam/channels/town-square" data-channel-mention="town-square">~Town Square</a>.</p>'
);
});

it('should link ~town-square, with display_name an HTML string', () => {
assert.equal(
test('should link ~town-square, with display_name an HTML string', () => {
expect(
TextFormatting.formatText('~town-square', {
channelNamesMap: {'town-square': {display_name: '<b>Reception</b>'}},
team: {name: 'myteam'},
}).trim(),
}).trim()
).toBe(
'<p><a class="mention-link" href="/myteam/channels/town-square" data-channel-mention="town-square">~&lt;b&gt;Reception&lt;/b&gt;</a></p>'
);
});

it('should link ~town-square, with a basename defined', () => {
test('should link ~town-square, with a basename defined', () => {
window.basename = '/subpath';
assert.equal(
expect(
TextFormatting.formatText('~town-square', {
channelNamesMap: {'town-square': {display_name: '<b>Reception</b>'}},
team: {name: 'myteam'},
}).trim(),
}).trim()
).toBe(
'<p><a class="mention-link" href="/subpath/myteam/channels/town-square" data-channel-mention="town-square">~&lt;b&gt;Reception&lt;/b&gt;</a></p>'
);
});

test('should link in brackets', () => {
expect(
TextFormatting.formatText('(~town-square)', {
channelNamesMap: {'town-square': {display_name: 'Town Square'}},
team: {name: 'myteam'},
}).trim()
).toBe(
'<p>(<a class="mention-link" href="/myteam/channels/town-square" data-channel-mention="town-square">~Town Square</a>)</p>'
);
});
});

describe('invalid channel links', () => {
test('should not link when a ~ is in the middle of a word', () => {
expect(
TextFormatting.formatText('aa~town-square', {
channelNamesMap: {'town-square': {display_name: 'Town Square'}},
team: {name: 'myteam'},
}).trim()
).toBe(
'<p>aa~town-square</p>'
);
});
});
});
2 changes: 1 addition & 1 deletion utils/text_formatting_hashtags.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('TextFormatting.Hashtags with default setting', () => {
};
assert.equal(
TextFormatting.formatText('#~test', options).trim(),
'<p>#~test</p>'
'<p>#<a class="mention-link" href="/abcd/channels/test" data-channel-mention="test">~Test Channel</a></p>'
);

assert.equal(
Expand Down

0 comments on commit e81371d

Please sign in to comment.