Highlight blacklist (#15623)

This commit is contained in:
Kashargul
2023-12-19 19:43:00 +01:00
committed by GitHub
parent f16671df06
commit 354dc022bf
4 changed files with 126 additions and 29 deletions
+60
View File
@@ -189,7 +189,9 @@ class ChatRenderer {
highlightSettings.map((id) => {
const setting = highlightSettingById[id];
const text = setting.highlightText;
const blacklist = setting.blacklistText;
const highlightColor = setting.highlightColor;
const highlightBlacklist = setting.highlightBlacklist;
const highlightWholeMessage = setting.highlightWholeMessage;
const matchWord = setting.matchWord;
const matchCase = setting.matchCase;
@@ -214,6 +216,55 @@ class ChatRenderer {
if (lines.length === 0) {
return;
}
const blacklistLines = String(blacklist)
.split(',')
.map((str) => str.trim())
.filter(
(str) =>
// Must be longer than one character
str &&
str.length > 1 &&
// Must be alphanumeric (with some punctuation)
allowedRegex.test(str) &&
// Reset lastIndex so it does not mess up the next word
((allowedRegex.lastIndex = 0) || true)
);
let blacklistWords;
let blacklistregex;
if (highlightBlacklist && blacklistLines.length > 0) {
let blacklistRegexExpressions = [];
for (let line of blacklistLines) {
// Regex expression syntax is /[exp]/
if (line.charAt(0) === '/' && line.charAt(line.length - 1) === '/') {
const expr = line.substring(1, line.length - 1);
// Check if this is more than one character
if (/^(\[.*\]|\\.|.)$/.test(expr)) {
continue;
}
blacklistRegexExpressions.push(expr);
} else {
// Lazy init
if (!blacklistWords) {
blacklistWords = [];
}
// We're not going to let regex characters fuck up our RegEx operation.
line = line.replace(regexEscapeCharacters, '\\$&');
blacklistWords.push('^\\s*' + line);
blacklistWords.push('^\\[\\d+:\\d+\\]\\s*' + line);
}
}
const regexStrBL = blacklistWords.join('|');
const flagsBL = 'i';
// We wrap this in a try-catch to ensure that broken regex doesn't break
// the entire chat.
try {
blacklistregex = new RegExp('(' + regexStrBL + ')', flagsBL);
} catch {
// We just reset it if it's invalid.
blacklistregex = null;
}
}
let regexExpressions = [];
// Organize each highlight entry into regex expressions and words
for (let line of lines) {
@@ -263,6 +314,8 @@ class ChatRenderer {
highlightRegex,
highlightColor,
highlightWholeMessage,
highlightBlacklist,
blacklistregex,
});
});
}
@@ -415,6 +468,13 @@ class ChatRenderer {
// Highlight text
if (!message.avoidHighlighting && this.highlightParsers) {
this.highlightParsers.map((parser) => {
if (
parser.highlightBlacklist &&
parser.blacklistregex &&
parser.blacklistregex.test(node.textContent)
) {
return;
}
const highlighted = highlightNode(
node,
parser.highlightRegex,
@@ -290,7 +290,9 @@ const TextHighlightSetting = (props, context) => {
const {
highlightColor,
highlightText,
blacklistText,
highlightWholeMessage,
highlightBlacklist,
matchWord,
matchCase,
} = highlightSettingById[id];
@@ -311,6 +313,22 @@ const TextHighlightSetting = (props, context) => {
}
/>
</Flex.Item>
<Flex.Item>
<Button.Checkbox
checked={highlightBlacklist}
content="Highlight Blacklist"
tooltip="If this option is selected, you can blacklist senders not to highlight their messages."
mr="5px"
onClick={() =>
dispatch(
updateHighlightSetting({
id: id,
highlightBlacklist: !highlightBlacklist,
})
)
}
/>
</Flex.Item>
<Flex.Item>
<Button.Checkbox
checked={highlightWholeMessage}
@@ -389,6 +407,23 @@ const TextHighlightSetting = (props, context) => {
)
}
/>
{highlightBlacklist ? (
<TextArea
height="3em"
value={blacklistText}
placeholder="Put names of senders you don't want highlighted here. Separate names with commas, i.e. (name1, name2, name3)"
onChange={(e, value) =>
dispatch(
updateHighlightSetting({
id: id,
blacklistText: value,
})
)
}
/>
) : (
''
)}
</Flex.Item>
);
};
@@ -6,7 +6,9 @@ import { createUuid } from 'common/uuid';
export const createHighlightSetting = (obj) => ({
id: createUuid(),
highlightText: '',
blacklistText: '',
highlightColor: '#ffdd44',
highlightBlacklist: false,
highlightWholeMessage: true,
matchWord: false,
matchCase: false,
File diff suppressed because one or more lines are too long