Skip to content

Commit

Permalink
Add support for mentions containing dots
Browse files Browse the repository at this point in the history
Fixes #175
  • Loading branch information
nfrasser committed Jan 5, 2017
1 parent e15b636 commit 70ca8ea
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/linkify/plugins/mention.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function mention(linkify) {
const TT_SLASH = TT.SLASH;
const TT_TLD = TT.TLD;
const TT_UNDERSCORE = TT.UNDERSCORE;
const TT_DOT = TT.DOT;

function MENTION(value) {
this.v = value;
Expand All @@ -29,14 +30,16 @@ export default function mention(linkify) {
const S_AT = S_START.jump(TT.AT); // @
const S_AT_SYMS = new State();
const S_MENTION = new State(MENTION);
const S_MENTION_SLASH = new State();
const S_MENTION_SLASH_SYMS = new State();
const S_MENTION_DIVIDER = new State();
const S_MENTION_DIVIDER_SYMS = new State();

// @_,
S_AT.on(TT_UNDERSCORE, S_AT_SYMS);

// @_*
S_AT_SYMS.on(TT_UNDERSCORE, S_AT_SYMS);
S_AT_SYMS
.on(TT_UNDERSCORE, S_AT_SYMS)
.on(TT_DOT, S_AT_SYMS);

// Valid mention (not made up entirely of symbols)
S_AT
Expand All @@ -59,23 +62,26 @@ export default function mention(linkify) {
.on(TT_NUM, S_MENTION)
.on(TT_UNDERSCORE, S_MENTION);

// Mention with a slash
S_MENTION.on(TT_SLASH, S_MENTION_SLASH);
// Mention with a divider
S_MENTION
.on(TT_SLASH, S_MENTION_DIVIDER)
.on(TT_DOT, S_MENTION_DIVIDER);

// Mention _ trailing stash plus syms
S_MENTION_SLASH.on(TT_UNDERSCORE, S_MENTION_SLASH_SYMS);
S_MENTION_SLASH_SYMS.on(TT_UNDERSCORE, S_MENTION_SLASH_SYMS);
S_MENTION_DIVIDER.on(TT_UNDERSCORE, S_MENTION_DIVIDER_SYMS);
S_MENTION_DIVIDER_SYMS.on(TT_UNDERSCORE, S_MENTION_DIVIDER_SYMS);

// Once we get a word token, mentions can start up again
S_MENTION_SLASH
S_MENTION_DIVIDER
.on(TT_DOMAIN, S_MENTION)
.on(TT_LOCALHOST, S_MENTION)
.on(TT_TLD, S_MENTION)
.on(TT_NUM, S_MENTION);

S_MENTION_SLASH_SYMS
S_MENTION_DIVIDER_SYMS
.on(TT_DOMAIN, S_MENTION)
.on(TT_LOCALHOST, S_MENTION)
.on(TT_TLD, S_MENTION)
.on(TT_NUM, S_MENTION);

}
22 changes: 21 additions & 1 deletion test/spec/linkify/plugins/mention-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('linkify/plugins/mention', () => {
}]);
});

it('parses mentions github team-style mentions with slashes', () => {
it('parses github team-style mentions with slashes', () => {
expect(linkify.find('Hey @500px/web please review this')).to.deep.equal([{
type: 'mention',
value: '@500px/web',
Expand All @@ -78,6 +78,26 @@ describe('linkify/plugins/mention', () => {
}]);
});

it('parses mentions with dots', () => {
expect(linkify.find('Hey @john.doe please review this')).to.deep.equal([{
type: 'mention',
value: '@john.doe',
href: '/john.doe'
}]);
});

it('ignores extra dots at the end of mentions', () => {
expect(linkify.find('We should get ...@soapbox._developers.@soapbox.cs.... to be awesome')).to.deep.equal([{
type: 'mention',
value: '@soapbox._developers',
href: '/soapbox._developers'
}, {
type: 'mention',
value: '@soapbox.cs',
href: '/soapbox.cs'
}]);
});

it('does not treat @/.* as a mention', () => {
expect(linkify.find('What about @/ and @/nfrasser?')).to.deep.equal([]);
});
Expand Down

0 comments on commit 70ca8ea

Please sign in to comment.