Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UX: Add notice when watched words are regexes #13493

Merged
merged 2 commits into from Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -18,33 +18,45 @@ export default Controller.extend(ModalFunctionality, {
)
matches(value, regexpString, words, isReplace, isTag, isLink) {
if (!value || !regexpString) {
return;
return [];
}

const regexp = new RegExp(regexpString, "ig");
const matches = value.match(regexp) || [];

if (isReplace || isLink) {
return matches.map((match) => ({
match,
replacement: words.find((word) =>
new RegExp(word.regexp, "ig").test(match)
).replacement,
}));
const matches = [];
words.forEach((word) => {
const regexp = new RegExp(word.regexp, "gi");
let match;
while ((match = regexp.exec(value)) !== null) {
matches.push({
match: match[1],
replacement: word.replacement,
});
}
});
return matches;
} else if (isTag) {
return matches.map((match) => {
const tags = new Set();

words.forEach((word) => {
if (new RegExp(word.regexp, "ig").test(match)) {
word.replacement.split(",").forEach((tag) => tags.add(tag));
const matches = {};
words.forEach((word) => {
const regexp = new RegExp(word.regexp, "gi");
let match;
while ((match = regexp.exec(value)) !== null) {
if (!matches[match[1]]) {
matches[match[1]] = new Set();
}
});

return { match, tags: Array.from(tags) };
let tags = matches[match[1]];
word.replacement.split(",").forEach((tag) => {
tags.add(tag);
});
}
});
}

return matches;
return Object.entries(matches).map((entry) => ({
match: entry[0],
tags: Array.from(entry[1]),
}));
} else {
return value.match(new RegExp(regexpString, "ig")) || [];
}
},
});
Expand Up @@ -26,6 +26,10 @@

<p class="about">{{actionDescription}}</p>

{{#if siteSettings.watched_words_regular_expressions}}
<p>{{html-safe (i18n "admin.watched_words.regex_warning" basePath=(base-path))}}</p>
{{/if}}

{{watched-word-form
actionKey=actionNameKey
action=(action "recordAdded")
Expand Down
1 change: 1 addition & 0 deletions config/locales/client.en.yml
Expand Up @@ -4761,6 +4761,7 @@ en:
clear_all: Clear All
clear_all_confirm: "Are you sure you want to clear all watched words for the %{action} action?"
invalid_regex: 'The watched word "%{word}" is an invalid regular expression.'
regex_warning: '<a href="%{basePath}/admin/site_settings/category/all_results?filter=watched%20words%20regular%20expressions%20">Watched words are regular expressions</a> and they do not automatically include word boundaries. If you want the regular expression to match whole words, include <code>\b</code> at the start and end of your regular expression.'
actions:
block: "Block"
censor: "Censor"
Expand Down