Improved remove markdown utility

This commit is contained in:
Erik 2022-05-08 14:35:04 +03:00
parent d9476e43b7
commit 07a4fe0a11
Signed by untrusted user: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
3 changed files with 5 additions and 2 deletions

View File

@ -47,6 +47,7 @@
"nightbird", "nightbird",
"slurs", "slurs",
"an alt", "an alt",
"nyquil",
"lol!", "lol!",
"coco", "coco",
"slur", "slur",

View File

@ -281,7 +281,7 @@ module.exports = class FilterUtility {
const diff = Math.abs(fullWord.length - match[0].length); const diff = Math.abs(fullWord.length - match[0].length);
if (diff > 3) { if (diff > 3) {
this.logger.debug(`Match length diff: ${diff} MATCH: ${match[0]} FULL: ${fullWord}`); this.logger.debug(`Match length diff: ${diff} MATCH: ${match[0]} FULL: ${fullWord}, CONTENT: ${content}`);
continue; continue;
} }
// log += `\nMessage matched with "${reg}" in the regex list.\nMatch: ${match[0]}, Full word: ${fullWord}\nFull content: ${content}`; // log += `\nMessage matched with "${reg}" in the regex list.\nMatch: ${match[0]}, Full word: ${fullWord}\nFull content: ${content}`;

View File

@ -123,6 +123,7 @@ class Util {
*/ */
static get formattingPatterns() { static get formattingPatterns() {
return [ return [
['\\|{2}(.*)\\|{2}', '$1'],
['\\*{1,3}([^*]*)\\*{1,3}', '$1'], ['\\*{1,3}([^*]*)\\*{1,3}', '$1'],
['_{1,3}([^_]*)_{1,3}', '$1'], ['_{1,3}([^_]*)_{1,3}', '$1'],
['`{1,3}([^`]*)`{1,3}', '$1'], ['`{1,3}([^`]*)`{1,3}', '$1'],
@ -139,7 +140,8 @@ class Util {
static removeMarkdown(content) { static removeMarkdown(content) {
if (!content) throw new Error('Missing content'); if (!content) throw new Error('Missing content');
this.formattingPatterns.forEach(([pattern, replacer]) => { this.formattingPatterns.forEach(([pattern, replacer]) => {
content = content.replace(new RegExp(pattern, 'gu'), replacer); const reg = new RegExp(pattern, 'gu');
while(reg.test(content)) content = content.replace(reg, replacer);
}); });
return content.trim(); return content.trim();
} }