This commit is contained in:
Jarek Radosz 2024-02-09 16:46:29 +01:00
parent d93debc634
commit 4362460441
No known key found for this signature in database
GPG Key ID: 62D0FBAE5BF9B953
6 changed files with 30 additions and 53 deletions

View File

@ -1,9 +1,5 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import {
createWatchedWordRegExp,
toWatchedWord,
} from "discourse-common/utils/watched-words";
export default class WatchedWordTest extends Component {
@tracked value;
@ -31,7 +27,8 @@ export default class WatchedWordTest extends Component {
if (this.isReplace || this.isLink) {
const matches = [];
this.args.model.watchedWord.words.forEach((word) => {
const regexp = createWatchedWordRegExp(word);
const caseFlag = word.case_sensitive ? "" : "i";
const regexp = new RegExp(word.regexp, `${caseFlag}gu`);
let match;
while ((match = regexp.exec(this.value)) !== null) {
@ -45,7 +42,8 @@ export default class WatchedWordTest extends Component {
} else if (this.isTag) {
const matches = {};
this.args.model.watchedWord.words.forEach((word) => {
const regexp = createWatchedWordRegExp(word);
const caseFlag = word.case_sensitive ? "" : "i";
const regexp = new RegExp(word.regexp, `${caseFlag}gu`);
let match;
while ((match = regexp.exec(this.value)) !== null) {
@ -66,12 +64,13 @@ export default class WatchedWordTest extends Component {
}));
} else {
let matches = [];
this.args.model.watchedWord.compiledRegularExpression.forEach(
(regexp) => {
const wordRegexp = createWatchedWordRegExp(toWatchedWord(regexp));
matches.push(...(this.value.match(wordRegexp) || []));
}
);
this.args.model.watchedWord.compiledRegularExpression.forEach((entry) => {
const [regexp, options] = Object.entries(entry)[0];
const caseFlag = options.case_sensitive ? "" : "i";
const wordRegexp = new RegExp(regexp, `${caseFlag}gu`);
matches.push(...(this.value.match(wordRegexp) || []));
});
return matches;
}

View File

@ -1,9 +0,0 @@
export function createWatchedWordRegExp(word) {
const caseFlag = word.case_sensitive ? "" : "i";
return new RegExp(word.regexp, `${caseFlag}gu`);
}
export function toWatchedWord(regexp) {
const [[regexpString, options]] = Object.entries(regexp);
return { ...options, regexp: regexpString };
}

View File

@ -196,7 +196,7 @@ function applyEmoji(
enableShortcuts,
inlineEmoji,
customEmojiTranslation,
watchedWordsReplacer,
watchedWordsReplace,
emojiDenyList
) {
let result = null;
@ -206,19 +206,16 @@ function applyEmoji(
content = emojiUnicodeReplacer(content);
}
if (watchedWordsReplacer) {
const watchedWordRegex = Object.keys(watchedWordsReplacer);
watchedWordRegex.forEach((watchedWord) => {
if (content?.match(watchedWord)) {
const regex = new RegExp(watchedWord, "g");
if (content && watchedWordsReplace) {
Object.entries(watchedWordsReplace).forEach(([regexpString, options]) => {
if (content.match(regexpString)) {
const regex = new RegExp(regexpString, "g");
const matches = content.match(regex);
const replacement = watchedWordsReplacer[watchedWord].replacement;
matches.forEach(() => {
const matchingRegex = regex.exec(content);
if (matchingRegex) {
content = content.replace(matchingRegex[1], replacement);
content = content.replace(matchingRegex[1], options.replacement);
}
});
}
@ -226,9 +223,9 @@ function applyEmoji(
}
// prevent denied emoji and aliases from being rendered
if (emojiDenyList?.length > 0) {
if (content && emojiDenyList?.length > 0) {
emojiDenyList.forEach((emoji) => {
if (content?.match(emoji)) {
if (content.match(emoji)) {
const regex = new RegExp(`:${emoji}:`, "g");
content = content.replace(regex, "");
}

View File

@ -1,8 +1,3 @@
import {
createWatchedWordRegExp,
toWatchedWord,
} from "discourse-common/utils/watched-words";
const MAX_MATCHES = 100;
function isLinkOpen(str) {
@ -58,11 +53,11 @@ export function setup(helper) {
if (md.options.discourse.watchedWordsReplace) {
Object.entries(md.options.discourse.watchedWordsReplace).forEach(
([regexpString, options]) => {
const word = toWatchedWord({ [regexpString]: options });
const caseFlag = options.case_sensitive ? "" : "i";
matchers.push({
word: new RegExp(options.regexp, options.case_sensitive ? "" : "i"),
pattern: createWatchedWordRegExp(word),
word: new RegExp(options.regexp, caseFlag),
pattern: new RegExp(regexpString, `${caseFlag}gu`),
replacement: options.replacement,
link: false,
});
@ -73,11 +68,11 @@ export function setup(helper) {
if (md.options.discourse.watchedWordsLink) {
Object.entries(md.options.discourse.watchedWordsLink).forEach(
([regexpString, options]) => {
const word = toWatchedWord({ [regexpString]: options });
const caseFlag = options.case_sensitive ? "" : "i";
matchers.push({
word: new RegExp(options.regexp, options.case_sensitive ? "" : "i"),
pattern: createWatchedWordRegExp(word),
word: new RegExp(options.regexp, caseFlag),
pattern: new RegExp(regexpString, `${caseFlag}gu`),
replacement: options.replacement,
link: true,
});

View File

@ -1,13 +1,9 @@
import {
createWatchedWordRegExp,
toWatchedWord,
} from "discourse-common/utils/watched-words";
export function censorFn(regexpList, replacementLetter) {
export function censorFn(regexpList, replacementLetter = "■") {
if (regexpList?.length) {
replacementLetter = replacementLetter || "■";
let censorRegexps = regexpList.map((regexp) => {
return createWatchedWordRegExp(toWatchedWord(regexp));
const censorRegexps = regexpList.map((entry) => {
const [regexp, options] = Object.entries(entry)[0];
const caseFlag = options.case_sensitive ? "" : "i";
return new RegExp(regexp, `${caseFlag}gu`);
});
return function (text) {

View File

@ -89,7 +89,6 @@ module PrettyText
discourse-common/addon/lib/deprecated
discourse-common/addon/lib/escape
discourse-common/addon/lib/avatar-utils
discourse-common/addon/utils/watched-words
discourse/app/lib/to-markdown
discourse/app/static/markdown-it/features
].each do |f|