Skip to content

Commit 505fb2b

Browse files
authored
Merge pull request ninenine#30 from ninenine/revert-29-master
Revert "Unicode support, settings updated correctly"
2 parents 6d8a930 + dc82e40 commit 505fb2b

5 files changed

Lines changed: 37 additions & 68 deletions

File tree

index.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var Beep = {
2424

2525
parseContent: function (content) {
2626
var nil = '^(?!x)x';
27-
return parseContent(content, Beep.banned_words || nil, Beep.banned_urls || nil, Beep.censorWholeWord);
27+
return parseContent(content, Beep.banned_words || nil, Beep.banned_urls || nil, Beep.censorWholeWord || nil);
2828
},
2929
toRegExp: toRegExp,
3030
loadList: function (callback) {
@@ -34,27 +34,21 @@ var Beep = {
3434
return callback(err);
3535
}
3636

37-
Beep.illegal_words = Beep.toRegExp(hash.illegal, true);
37+
Beep.illegal_words = new RegExp('\\b(?:' + Beep.toRegExp(hash.illegal) + ')\\b', 'ig');
3838

3939
if (hash.id && hash.id.length) {
4040
var words = hash.id.split(',').filter(function (word) {
4141
return !Beep.illegal_words.test(word);
4242
});
43-
Beep.banned_words = Beep.toRegExp(words, true);
43+
Beep.banned_words = new RegExp('\\b(?:' + Beep.toRegExp(words) + ')\\b', 'ig');
4444
Beep.banned_words_raw = hash.id;
4545
} else {
46-
Beep.banned_words = Beep.toRegExp(defaultBanList, true);
46+
Beep.banned_words = new RegExp('\\b(?:' + Beep.toRegExp(defaultBanList) + ')\\b', 'ig');
4747
Beep.banned_words_raw = defaultBanList.join(',');
4848
winston.info('Default list of Banned Words is enabled. Please go to administration panel to change the list.');
4949
}
50-
51-
Beep.banned_urls = Beep.toRegExp(hash.urls);
52-
50+
Beep.banned_urls = new RegExp(Beep.toRegExp(hash.urls), 'ig');
5351
Beep.censorWholeWord = hash.censorWholeWord === 'on';
54-
if (meta.config) {
55-
meta.config.beep = meta.config.beep || {};
56-
meta.config.beep.censorWholeWord = Beep.censorWholeWord;
57-
}
5852

5953
callback();
6054
});
@@ -147,6 +141,26 @@ var Beep = {
147141
callback(null, custom_header);
148142
}
149143
},
144+
category: {
145+
get: function (data, callback) {
146+
var topics = data.category.topics;
147+
topics.forEach(function (topic) {
148+
topic.title = Beep.parseContent(topic.title);
149+
topic.slug = Beep.parseContent(topic.slug);
150+
});
151+
callback(null, data);
152+
},
153+
topics: {
154+
get: function (data, callback) {
155+
data.topics.forEach(function (topic) {
156+
topic.title = Beep.parseContent(topic.title);
157+
topic.slug = Beep.parseContent(topic.slug);
158+
topic.titleRaw = Beep.parseContent(topic.titleRaw);
159+
});
160+
callback(null, data);
161+
}
162+
}
163+
},
150164
post: {
151165
getFields: function (data, callback) {
152166
if (data.fields.indexOf('content') !== -1) {

lib/parseContent.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
'use strict';
22

3-
var isLatin = /^\w+$/;
4-
53
function parseContent(content, banned_words, banned_urls, censorWholeWord) {
64
if (!content) {
75
return content;
86
}
97

108
function censor(match) {
11-
if (!isLatin.test(match)) {
12-
return '[censored]';
13-
}
14-
159
var l = match.length;
1610
var out = match[0];
1711

@@ -30,4 +24,4 @@ function parseContent(content, banned_words, banned_urls, censorWholeWord) {
3024
.replace(banned_urls, '[link removed]');
3125
}
3226

33-
module.exports = parseContent;
27+
module.exports = parseContent;

lib/toRegExp.js

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,15 @@
11
'use strict';
22

3-
var isLatin = /^\w+$/;
4-
5-
function toRegExp(arr, fullWord) {
3+
function toRegExp(arr) {
64
if (!Array.isArray(arr)) {
75
arr = (arr || '').toString().split(',');
86
}
9-
arr = arr.filter(Boolean);
10-
11-
var str;
12-
if (fullWord) {
13-
var latin = arr.filter(function (word) {
14-
return isLatin.test(word);
15-
}).map(function (word) {
16-
return word.trim().replace(/([-[\]{}()*+?.,\\^$|#\s])/g, '\\$1');
17-
}).join('|');
187

19-
var notLatin = arr.filter(function (word) {
20-
return !isLatin.test(word);
21-
}).map(function (word) {
22-
return word.trim().replace(/([-[\]{}()*+?.,\\^$|#\s])/g, '\\$1');
23-
}).join('|');
24-
25-
if (latin && notLatin) {
26-
str = '\\b(?:' + latin + ')\\b|(?:' + notLatin + ')';
27-
} else if (latin) {
28-
str = '\\b(?:' + latin + ')\\b';
29-
} else if (notLatin) {
30-
str = notLatin;
31-
}
32-
} else {
33-
str = arr.filter(Boolean).map(function (word) {
34-
return word.trim().replace(/([-[\]{}()*+?.,\\^$|#\s])/g, '\\$1');
35-
}).join('|');
36-
}
8+
var str = arr.filter(Boolean).map(function (word) {
9+
return word.trim().replace(/([-[\]{}()*+?.,\\^$|#\s])/g, '\\$1');
10+
}).join('|');
3711

38-
return new RegExp(str || '^(?!x)x', 'ig');
12+
return str || '^(?!x)x';
3913
}
4014

41-
module.exports = toRegExp;
15+
module.exports = toRegExp;

tests/parseContent.spec.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ var assert = require('assert');
55
var toRegExp = require('../lib/toRegExp');
66
var parseContent = require('../lib/parseContent');
77

8-
var bannedWords = toRegExp(['poop', 'shit'], true);
9-
var bannedUrls = toRegExp(['http://example.com', 'http://foo.bar']);
8+
var bannedWords = new RegExp('\\b(?:' + toRegExp(['poop', 'shit']) + ')\\b', 'ig');
9+
var bannedUrls = new RegExp(toRegExp(['http://example.com', 'http://foo.bar']), 'ig');
1010
var nil = '^(?!x)x';
1111

1212
assert.strictEqual(parseContent(
@@ -29,13 +29,3 @@ assert.strictEqual(parseContent(
2929
bannedUrls,
3030
false
3131
), 'My favorite website is [link removed]. I also love [link removed].');
32-
33-
var unicodeBannedWords = toRegExp(['今', '野'], true);
34-
assert.strictEqual(parseContent(
35-
'載点代示早面通今就焼初哲野質',
36-
unicodeBannedWords,
37-
nil,
38-
false
39-
), '載点代示早面通[censored]就焼初哲[censored]質');
40-
41-
console.log('parseContent passed');

tests/toRegExp.spec.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ var assert = require('assert');
44

55
var toRegExp = require('../lib/toRegExp');
66

7-
assert.equal(toRegExp(['bad', 'words', 'here'], true), '/\\b(?:bad|words|here)\\b/gi');
8-
assert.equal(toRegExp('bad,words, here'), '/bad|words|here/gi');
9-
assert.equal(toRegExp('今,野', true), '/今|野/gi');
10-
assert.equal(toRegExp(null), '/^(?!x)x/gi');
11-
12-
console.log('toRegExp passed');
7+
assert.deepStrictEqual(toRegExp(['bad', 'words', 'here']), 'bad|words|here');
8+
assert.deepStrictEqual(toRegExp('bad,words, here'), 'bad|words|here');
9+
assert.deepStrictEqual(toRegExp(null), '^(?!x)x');

0 commit comments

Comments
 (0)