Fix non-ASCII word soft mutes

This commit is contained in:
naskya 2023-05-05 12:23:44 +09:00
parent 433b492a2b
commit 9bb79161af

View file

@ -6,10 +6,6 @@ export type Muted = {
const NotMuted = { muted: false, matched: [] };
function escapeRegExp(x: string) {
return x.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}
function checkWordMute(
note: NoteLike,
mutedWords: Array<string | string[]>,
@ -17,38 +13,43 @@ function checkWordMute(
const text = ((note.cw ?? "") + " " + (note.text ?? "")).trim();
if (text === "") return NotMuted;
let result = { muted: false, matched: [] };
for (const mutePattern of mutedWords) {
let mute: RegExp;
let matched: string[];
if (Array.isArray(mutePattern)) {
matched = mutePattern.filter((keyword) => keyword !== "");
if (matched.length === 0) {
continue;
// Clean up
const keywords = mutePattern.filter((keyword) => keyword !== "");
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 {
// represents RegExp
const regexp = mutePattern.match(/^\/(.+)\/(.*)$/);
// This should never happen due to input sanitisation.
if (!regexp) {
console.warn(`Found invalid regex in word mutes: ${mutePattern}`);
continue;
}
mute = new RegExp(regexp[1], regexp[2]);
matched = [mutePattern];
}
try {
if (mute.test(text)) {
return { muted: true, matched };
try {
if (new RegExp(regexp[1], regexp[2]).test(text)) {
result.muted = true;
result.matched.push(mutePattern);
}
} catch (err) {
// This should never happen due to input sanitisation.
}
} catch (err) {
// This should never happen due to input sanitisation.
}
}
return NotMuted;
result.matched = [...new Set(result.matched)];
return result;
}
export function getWordSoftMute(