mirror of
https://example.com
synced 2024-11-23 10:06:40 +09:00
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: [] };
|
||||
|
||||
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(
|
||||
|
|
Loading…
Reference in a new issue