no longer crashes on invalid regex

This commit is contained in:
camila314 2023-11-19 18:25:11 -06:00
parent 4b4ad65c6e
commit 808c664d93

View file

@ -34,6 +34,14 @@ async function addRegex(updater: () => void) {
updater(); updater();
} }
function safeMatchesRegex(s: string, r: string) {
try {
return s.match(new RegExp(r));
} catch {
return false;
}
}
const settings = definePluginSettings({ const settings = definePluginSettings({
replace: { replace: {
type: OptionType.COMPONENT, type: OptionType.COMPONENT,
@ -107,7 +115,7 @@ export default definePlugin({
}, },
applyRegexes(m) { applyRegexes(m) {
if (regexes.some(r => r != "" && m.content.match(new RegExp(r)))) { if (regexes.some(r => r != "" && safeMatchesRegex(m.content, r))) {
m.mentions.push(me); m.mentions.push(me);
} }
}, },