Skip to content

Commit d340bc9

Browse files
authored
Merge pull request ninenine#31 from pitaj/master
Filter tags, refactor `is_topic`, fix tests
2 parents d27ebb3 + e0f34b9 commit d340bc9

4 files changed

Lines changed: 39 additions & 14 deletions

File tree

index.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ var Beep = {
2222
banned_urls: null,
2323
illegal_words: null,
2424

25-
parseContent: function (content, is_topic=false) {
25+
parseContent: function (content, symbol) {
2626
var nil = '^(?!x)x';
27-
return parseContent(content, Beep.banned_words || nil, Beep.banned_urls || nil, Beep.censorWholeWord, is_topic);
27+
return parseContent(content, Beep.banned_words || nil, Beep.banned_urls || nil, Beep.censorWholeWord, symbol || '*');
2828
},
2929
toRegExp: toRegExp,
3030
loadList: function (callback) {
@@ -105,7 +105,7 @@ var Beep = {
105105
callback(null, data);
106106
},
107107
onListChange: function (hash) {
108-
if (hash === 'settings:beep') {
108+
if (hash && hash.plugin === 'beep') {
109109
Beep.loadList(function () {});
110110
}
111111
},
@@ -131,10 +131,28 @@ var Beep = {
131131
callback(null, data);
132132
},
133133
parseTopic: function (data, callback) {
134-
var is_topic=true
135-
data.topic.title = Beep.parseContent(data.topic.title, is_topic);
136-
data.topic.slug = Beep.parseContent(data.topic.slug, is_topic);
137-
data.topic.titleRaw = Beep.parseContent(data.topic.titleRaw, is_topic);
134+
// from http://htmlarrows.com/symbols/
135+
var starHTML = '⁎';
136+
data.topic.title = Beep.parseContent(data.topic.title, starHTML);
137+
data.topic.slug = Beep.parseContent(data.topic.slug, starHTML);
138+
data.topic.titleRaw = Beep.parseContent(data.topic.titleRaw, starHTML);
139+
140+
callback(null, data);
141+
},
142+
filterTags: function (data, callback) {
143+
var match;
144+
data.tags.some(function (tag) {
145+
match = tag && tag.match(Beep.illegal_words);
146+
return !!match;
147+
});
148+
149+
if (match) {
150+
return callback(new Error('You may not use the word "' + match[0] + '" in your tags.'));
151+
}
152+
153+
data.tags = data.tags.map(function (tag) {
154+
return Beep.parseContent(tag, '+');
155+
});
138156

139157
callback(null, data);
140158
},

lib/parseContent.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
var isLatin = /^\w+$/;
44

5-
function parseContent(content, banned_words, banned_urls, censorWholeWord, is_topic) {
5+
function parseContent(content, banned_words, banned_urls, censorWholeWord, symbol) {
66
if (!content) {
77
return content;
88
}
9-
// from http://htmlarrows.com/symbols/
10-
var sym = is_topic ? '*' : '⁎';
9+
10+
symbol = symbol || '*';
1111

1212
function censor(match) {
1313
if (!isLatin.test(match)) {
@@ -19,7 +19,7 @@ function parseContent(content, banned_words, banned_urls, censorWholeWord, is_to
1919

2020
var i = l - 2;
2121
while (i) {
22-
out += sym;
22+
out += symbol;
2323
i--;
2424
}
2525

plugin.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
{ "hook": "filter:topic.create", "method": "parseTopic" },
2020
{ "hook": "filter:topic.edit", "method": "parseTopic" },
21+
{ "hook": "filter:tags.filter", "method": "filterTags" },
2122

2223
{ "hook": "filter:topic.create", "method": "checkForIllegalWords" },
2324
{ "hook": "filter:topic.edit", "method": "checkForIllegalWords" },

tests/parseContent.spec.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ var parseContent = require('../lib/parseContent');
88
var bannedWords = toRegExp(['poop', 'shit'], true);
99
var bannedUrls = toRegExp(['http://example.com', 'http://foo.bar']);
1010
var nil = '^(?!x)x';
11-
var is_topic = true;
11+
var symbol = '⁎';
12+
13+
assert.strictEqual(parseContent(
14+
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
15+
bannedWords,
16+
nil
17+
), 'A whole lot of p**p causes a ton of s**t, shitzu, repoopulate');
1218

1319
assert.strictEqual(parseContent(
1420
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
1521
bannedWords,
1622
nil,
1723
false,
18-
is_topic
19-
), 'A whole lot of p**p causes a ton of s**t, shitzu, repoopulate');
24+
symbol
25+
), 'A whole lot of p⁎⁎p causes a ton of s⁎⁎t, shitzu, repoopulate');
2026

2127
assert.strictEqual(parseContent(
2228
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',

0 commit comments

Comments
 (0)