Skip to content

Commit

Permalink
重复替换 at 用户名的问题,fix #481
Browse files Browse the repository at this point in the history
  • Loading branch information
alsotang committed Feb 4, 2015
1 parent abde0a4 commit bd7bf58
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 3 additions & 3 deletions common/at.js
Expand Up @@ -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` 中内容也不该被解析
Expand All @@ -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) {
Expand All @@ -44,6 +43,7 @@ var fetchUsers = function (text) {
names.push(s);
}
}
names = _.uniq(names);
return names;
};
exports.fetchUsers = fetchUsers;
Expand Down
14 changes: 13 additions & 1 deletion test/common/at.test.js
Expand Up @@ -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_)
Expand Down Expand Up @@ -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 () {
Expand Down

1 comment on commit bd7bf58

@i5ting
Copy link
Member

@i5ting i5ting commented on bd7bf58 Feb 5, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已阅,准,哈哈

Please sign in to comment.