Skip to content

Enhance blacklist handling: regex replacements and partial date removal #5718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

Gykes
Copy link
Contributor

@Gykes Gykes commented Mar 12, 2025

So this is still a draft as I am TERRIBLE with Regex and just as bad with coding. Please let me know if there is anything that needs fixed, I'm happy to do so.

  • Added support for regex-based replacements in blacklist processing
  • Implemented || delimiter to allow custom replacements (e.g., "bwb||bigwetbutts")
  • Improved regex handling for partial date removal:
    • Allows removing only the day (-DD) from YYYY-MM-DD while keeping YYYY-MM
    • Example: "(\d{4}-\d{2})-\d{2}||$1" removes -DD but preserves YYYY-MM
  • Prevented regex errors using try/catch
  • Ensured spaces are normalized after replacements

Copy link
Collaborator

@WithoutPants WithoutPants left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run make fmt-ui to fix up the prettier errors.

Edit: don't forget to add some documentation somewhere

Comment on lines +131 to +148
const regexReplacements = new Map<string, string>();
const regexs = blacklist
.map((entry) => {
let [pattern, replacement] = entry.split("||"); // Extract regex and replacement

try {
const compiledRegex = new RegExp(pattern, "gi");

if (replacement !== undefined ){
regexReplacements.set(compiledRegex.source, replacement); // Store replacement
}

return compiledRegex;
} catch {
return null; // Ignore invalid regex patterns
}
})
.filter((r) => r !== null) as RegExp[];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not ideal to use map and then mutate stuff outside it. Instead, I would return a tuple (return [compiledRegex, replacement]). Then, these pairs of values can be handled in the code below. I would also move this into a separate function.

Comment on lines +151 to +159
regexs.forEach((regex) => {
const replacement = regexReplacements.get(regex.source);
if (replacement) {
str = str.replace(regex, (match:string, ...groups: string[]) => {
return replacement.replace(/\\(\d+)/g, (_, groupIndex) => groups[parseInt(groupIndex, 10) - 1] || "");
});
} else {
str = str.replace(regex, "");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be moved into a separate function. The code between 154-156 needs some explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants