wordfilter update
This commit is contained in:
@@ -18,11 +18,23 @@ export async function applyWordFilter(content) {
|
|||||||
let filtered = content;
|
let filtered = content;
|
||||||
for (const f of filters) {
|
for (const f of filters) {
|
||||||
const escaped = escapeRegExp(f.word);
|
const escaped = escapeRegExp(f.word);
|
||||||
const startBoundary = /^\w/.test(f.word) ? '\\b' : '';
|
const regex = new RegExp(escaped, 'gi');
|
||||||
const endBoundary = /\w$/.test(f.word) ? '\\b' : '';
|
filtered = filtered.replace(regex, (match) => {
|
||||||
|
// 1. Check if the matched substring is entirely UPPERCASE
|
||||||
const regex = new RegExp(`${startBoundary}${escaped}${endBoundary}`, 'gi');
|
if (match === match.toUpperCase() && match !== match.toLowerCase()) {
|
||||||
filtered = filtered.replace(regex, f.replacement);
|
return f.replacement.toUpperCase();
|
||||||
|
}
|
||||||
|
// 2. Check if the matched substring is Capitalized / Titlecase
|
||||||
|
if (match[0] === match[0].toUpperCase() && match[0] !== match[0].toLowerCase()) {
|
||||||
|
return f.replacement.charAt(0).toUpperCase() + f.replacement.slice(1);
|
||||||
|
}
|
||||||
|
// 3. Check if the matched substring is lowercase
|
||||||
|
if (match === match.toLowerCase()) {
|
||||||
|
return f.replacement.toLowerCase();
|
||||||
|
}
|
||||||
|
// Fallback to exact replacement string
|
||||||
|
return f.replacement;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return filtered;
|
return filtered;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
Reference in New Issue
Block a user