diff --git a/common/at.js b/common/at.js index 17dd5f48c4..ec37f7b852 100644 --- a/common/at.js +++ b/common/at.js @@ -20,7 +20,7 @@ var _ = require('lodash'); * @return {Array} 用户名数组 */ var fetchUsers = function (text) { - var ignore_regexs = [ + var ignoreRegexs = [ /```.+?```/g, // 去除单行的 ``` /^```[\s\S]+?^```/gm, // ``` 里面的是 pre 标签内容 /`[\s\S]+?`/g, // 同一行中,`some code` 中内容也不该被解析 @@ -29,11 +29,10 @@ var fetchUsers = function (text) { /\[@.+?\]\(\/.+?\)/g, // 已经被 link 的 username ]; - ignore_regexs.forEach(function(ignore_regex) { + ignoreRegexs.forEach(function (ignore_regex) { text = text.replace(ignore_regex, ''); }); - var results = text.match(/@[a-z0-9\-_]+\b/igm); var names = []; if (results) { @@ -44,6 +43,7 @@ var fetchUsers = function (text) { names.push(s); } } + names = _.uniq(names); return names; }; exports.fetchUsers = fetchUsers; diff --git a/test/common/at.test.js b/test/common/at.test.js index 256a4409c4..1dd7d4a7c9 100644 --- a/test/common/at.test.js +++ b/test/common/at.test.js @@ -68,9 +68,17 @@ describe('test/common/at.test.js', function () { ``` [@be_link](/user/be_link) [@be_link2](/user/be_link2) + + @alsotang @alsotang + aldjf + @alsotang @tangzhanli */}); - var matched_users = ['A-aZ-z0-9_', 'begin_with_spaces', 'multi_in_oneline', 'around_text', 'end_with_no_space', 'begin_with_no_spaces', 'end_with_no_space2', 'begin_with_no_spaces2', 'alsotang', 'alsotang2']; + var matched_users = ['A-aZ-z0-9_', 'begin_with_spaces', + 'multi_in_oneline', 'around_text', 'end_with_no_space', + 'begin_with_no_spaces', 'end_with_no_space2', + 'begin_with_no_spaces2', 'alsotang', 'alsotang2', + 'tangzhanli']; var linkedText = multiline.stripIndent(function(){/* [@A-aZ-z0-9_](/user/A-aZ-z0-9_) @@ -115,6 +123,10 @@ code: `@in_code` ``` [@be_link](/user/be_link) [@be_link2](/user/be_link2) + +[@alsotang](/user/alsotang) [@alsotang](/user/alsotang) +aldjf +[@alsotang](/user/alsotang) [@tangzhanli](/user/tangzhanli) */}); describe('#fetchUsers()', function () {