Fix non-ASCII word soft mutes
This commit is contained in:
parent
d1ab75beed
commit
ba1e96a020
1 changed files with 23 additions and 22 deletions
|
@ -6,10 +6,6 @@ export type Muted = {
|
||||||
|
|
||||||
const NotMuted = { muted: false, matched: [] };
|
const NotMuted = { muted: false, matched: [] };
|
||||||
|
|
||||||
function escapeRegExp(x: string) {
|
|
||||||
return x.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkWordMute(
|
function checkWordMute(
|
||||||
note: NoteLike,
|
note: NoteLike,
|
||||||
mutedWords: Array<string | string[]>,
|
mutedWords: Array<string | string[]>,
|
||||||
|
@ -17,38 +13,43 @@ function checkWordMute(
|
||||||
const text = ((note.cw ?? "") + " " + (note.text ?? "")).trim();
|
const text = ((note.cw ?? "") + " " + (note.text ?? "")).trim();
|
||||||
if (text === "") return NotMuted;
|
if (text === "") return NotMuted;
|
||||||
|
|
||||||
|
let result = { muted: false, matched: [] };
|
||||||
|
|
||||||
for (const mutePattern of mutedWords) {
|
for (const mutePattern of mutedWords) {
|
||||||
let mute: RegExp;
|
|
||||||
let matched: string[];
|
|
||||||
if (Array.isArray(mutePattern)) {
|
if (Array.isArray(mutePattern)) {
|
||||||
matched = mutePattern.filter((keyword) => keyword !== "");
|
// Clean up
|
||||||
if (matched.length === 0) {
|
const keywords = mutePattern.filter((keyword) => keyword !== "");
|
||||||
continue;
|
|
||||||
|
if (
|
||||||
|
keywords.length > 0 &&
|
||||||
|
keywords.every((keyword) => text.includes(keyword))
|
||||||
|
) {
|
||||||
|
result.muted = true;
|
||||||
|
result.matched.push(...keywords);
|
||||||
}
|
}
|
||||||
mute = new RegExp(
|
|
||||||
`\\b${matched.map(escapeRegExp).join("\\b.*\\b")}\\b`,
|
|
||||||
"g",
|
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
|
// represents RegExp
|
||||||
const regexp = mutePattern.match(/^\/(.+)\/(.*)$/);
|
const regexp = mutePattern.match(/^\/(.+)\/(.*)$/);
|
||||||
|
|
||||||
// This should never happen due to input sanitisation.
|
// This should never happen due to input sanitisation.
|
||||||
if (!regexp) {
|
if (!regexp) {
|
||||||
console.warn(`Found invalid regex in word mutes: ${mutePattern}`);
|
console.warn(`Found invalid regex in word mutes: ${mutePattern}`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
mute = new RegExp(regexp[1], regexp[2]);
|
|
||||||
matched = [mutePattern];
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
if (mute.test(text)) {
|
if (new RegExp(regexp[1], regexp[2]).test(text)) {
|
||||||
return { muted: true, matched };
|
result.muted = true;
|
||||||
|
result.matched.push(mutePattern);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// This should never happen due to input sanitisation.
|
// This should never happen due to input sanitisation.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return NotMuted;
|
result.matched = [...new Set(result.matched)];
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getWordSoftMute(
|
export function getWordSoftMute(
|
||||||
|
|
Loading…
Reference in a new issue