Skip to content

Commit 1b21217

Browse files
committed
fix asterix
1 parent bf99833 commit 1b21217

7 files changed

Lines changed: 108 additions & 94 deletions

File tree

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Editor configuration, see http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false

LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
Copyright (c) 2013-2017, Davis Wainaina <davisjwn@gmail.com>
1+
Copyright (c) 2013-2019, Davis Wainaina <davisjwn@gmail.com>
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
55

66
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
77
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var winston = require.main.require('winston');
44
var meta = require.main.require('./src/meta');
55

6-
var translator = module.parent.require('../public/src/modules/translator');
6+
var translator = require.main.require('./src/translator');
77

88
var toRegExp = require('./lib/toRegExp');
99
var parseContent = require('./lib/parseContent');
@@ -26,7 +26,7 @@ var Beep = {
2626

2727
parseContent: function (content, symbol) {
2828
var nil = '^(?!x)x';
29-
return parseContent(content, Beep.banned_words || nil, Beep.banned_urls || nil, Beep.censorWholeWord, symbol || '*');
29+
return parseContent(content, Beep.banned_words || nil, Beep.banned_urls || nil, Beep.censorWholeWord, symbol || '&ast;');
3030
},
3131
toRegExp: toRegExp,
3232
loadList: function (callback) {
@@ -86,7 +86,7 @@ var Beep = {
8686
return callback(null, config);
8787
}
8888
config.beep = {
89-
censorWholeWord: censorWholeWord === 'on'
89+
censorWholeWord: censorWholeWord === 'on',
9090
};
9191
callback(err, config);
9292
});
@@ -97,13 +97,13 @@ var Beep = {
9797

9898
var titleMatch = postTitle && postTitle.match(Beep.illegal_words);
9999
if (titleMatch) {
100-
return translator.translate('[[beep:titleMatch.error, ' + titleMatch[0] + ']]', function(translated) {
100+
return translator.translate('[[beep:titleMatch.error, ' + titleMatch[0] + ']]', function (translated) {
101101
callback(new Error(translated));
102102
});
103103
}
104104
var contentMatch = postContent && postContent.match(Beep.illegal_words);
105105
if (contentMatch) {
106-
return translator.translate('[[beep:contentMatch.error, ' + contentMatch[0] + ']]', function(translated) {
106+
return translator.translate('[[beep:contentMatch.error, ' + contentMatch[0] + ']]', function (translated) {
107107
callback(new Error(translated));
108108
});
109109
}
@@ -137,8 +137,8 @@ var Beep = {
137137
callback(null, data);
138138
},
139139
parseTopic: function (data, callback) {
140-
// from http://htmlarrows.com/symbols/
141-
var starHTML = '&#8270;';
140+
// from http://htmlarrows.com/symbols/
141+
var starHTML = '*';
142142
data.topic.title = Beep.parseContent(data.topic.title, starHTML);
143143
data.topic.slug = Beep.parseContent(data.topic.slug, starHTML);
144144
data.topic.titleRaw = Beep.parseContent(data.topic.titleRaw, starHTML);
@@ -153,7 +153,7 @@ var Beep = {
153153
});
154154

155155
if (match) {
156-
return translator.translate('[[beep:tagMatch.error, ' + match[0] + ']]', function(translated) {
156+
return translator.translate('[[beep:tagMatch.error, ' + match[0] + ']]', function (translated) {
157157
callback(new Error(translated));
158158
});
159159
}
@@ -167,12 +167,12 @@ var Beep = {
167167
admin: {
168168
menu: function (custom_header, callback) {
169169
custom_header.plugins.push({
170-
'route': '/plugins/beep',
171-
'icon': 'fa-microphone-slash',
172-
'name': 'Censor Curse Words'
170+
route: '/plugins/beep',
171+
icon: 'fa-microphone-slash',
172+
name: 'Censor Curse Words',
173173
});
174174
callback(null, custom_header);
175-
}
175+
},
176176
},
177177
post: {
178178
getFields: function (data, callback) {
@@ -182,14 +182,14 @@ var Beep = {
182182
});
183183
}
184184
callback(null, data);
185-
}
185+
},
186186
},
187187
messaging: {
188188
getTeaser: function (data, callback) {
189189
data.teaser.content = Beep.parseContent(data.teaser.content);
190190
callback(null, data);
191-
}
192-
}
191+
},
192+
},
193193
};
194194

195195
module.exports = Beep;

lib/parseContent.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,34 @@
33
var isLatin = /^\w+$/;
44

55
function parseContent(content, banned_words, banned_urls, censorWholeWord, symbol) {
6-
if (!content) {
7-
return content;
8-
}
9-
10-
symbol = symbol || '*';
11-
12-
function censor(match) {
13-
if (!isLatin.test(match)) {
14-
return '[censored]';
15-
}
16-
17-
var l = match.length;
18-
var out = match[0];
19-
20-
var i = l - 2;
21-
while (i) {
22-
out += symbol;
23-
i--;
24-
}
25-
26-
return out + match[l - 1];
27-
}
28-
29-
var replacement = censorWholeWord ? '[censored]' : censor;
30-
return content
31-
.replace(banned_words, replacement)
32-
.replace(banned_urls, '[link removed]');
6+
if (!content) {
7+
return content;
8+
}
9+
10+
symbol = symbol || '*';
11+
12+
function censor(match) {
13+
if (!isLatin.test(match)) {
14+
return '[censored]';
15+
}
16+
17+
var l = match.length;
18+
var out = match[0];
19+
20+
var i = l - 2;
21+
while (i) {
22+
out += symbol;
23+
// eslint-disable-next-line no-plusplus
24+
i--;
25+
}
26+
27+
return out + match[l - 1];
28+
}
29+
30+
var replacement = censorWholeWord ? '[censored]' : censor;
31+
return content
32+
.replace(banned_words, replacement)
33+
.replace(banned_urls, '[link removed]');
3334
}
3435

3536
module.exports = parseContent;

lib/toRegExp.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,39 @@
33
var isLatin = /^\w+$/;
44

55
function toRegExp(arr, fullWord) {
6-
if (!Array.isArray(arr)) {
7-
arr = (arr || '').toString().split(',');
8-
}
9-
arr = arr.filter(Boolean);
6+
if (!Array.isArray(arr)) {
7+
arr = (arr || '').toString().split(',');
8+
}
9+
arr = arr.filter(Boolean);
1010

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('|');
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('|');
1818

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('|');
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('|');
2424

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-
}
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+
}
3737

38-
return new RegExp(str || '^(?!x)x', 'ig');
38+
return new RegExp(str || '^(?!x)x', 'ig');
3939
}
4040

4141
module.exports = toRegExp;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nodebb-plugin-beep",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"description": "NodeBB Censor Curse Words Plugin",
55
"main": "index.js",
66
"scripts": {

tests/parseContent.spec.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,39 @@ var nil = '^(?!x)x';
1111
var symbol = '&#8270;';
1212

1313
assert.strictEqual(parseContent(
14-
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
15-
bannedWords,
16-
nil
14+
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
15+
bannedWords,
16+
nil
1717
), 'A whole lot of p**p causes a ton of s**t, shitzu, repoopulate');
1818

1919
assert.strictEqual(parseContent(
20-
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
21-
bannedWords,
22-
nil,
23-
false,
24-
symbol
20+
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
21+
bannedWords,
22+
nil,
23+
false,
24+
symbol
2525
), 'A whole lot of p&#8270;&#8270;p causes a ton of s&#8270;&#8270;t, shitzu, repoopulate');
2626

2727
assert.strictEqual(parseContent(
28-
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
29-
bannedWords,
30-
nil,
31-
true
28+
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
29+
bannedWords,
30+
nil,
31+
true
3232
), 'A whole lot of [censored] causes a ton of [censored], shitzu, repoopulate');
3333

3434
assert.strictEqual(parseContent(
35-
'My favorite website is http://example.com. I also love http://foo.bar.',
36-
nil,
37-
bannedUrls,
38-
false
35+
'My favorite website is http://example.com. I also love http://foo.bar.',
36+
nil,
37+
bannedUrls,
38+
false
3939
), 'My favorite website is [link removed]. I also love [link removed].');
4040

4141
var unicodeBannedWords = toRegExp(['今', '野'], true);
4242
assert.strictEqual(parseContent(
43-
'載点代示早面通今就焼初哲野質',
44-
unicodeBannedWords,
45-
nil,
46-
false
43+
'載点代示早面通今就焼初哲野質',
44+
unicodeBannedWords,
45+
nil,
46+
false
4747
), '載点代示早面通[censored]就焼初哲[censored]質');
4848

4949
console.log('parseContent passed');

0 commit comments

Comments
 (0)