Regex: Fix markdown format bugs

If a regex cannot be parsed, silently return out and don't run the
script. May be a good idea to display a toast message saying the
script didn't run.

Also only reload the chat if a chat is actually loaded.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri
2023-07-05 15:08:03 -04:00
parent 13ba5cec49
commit 8212206d50
2 changed files with 25 additions and 12 deletions

View File

@@ -14,18 +14,22 @@ const regex_placement = {
SENDAS: 4 SENDAS: 4
} }
// From: https://github.com/IonicaBizau/regex-parser.js/blob/master/lib/index.js // Originally from: https://github.com/IonicaBizau/regex-parser.js/blob/master/lib/index.js
function regexFromString(input) { function regexFromString(input) {
// Parse input try {
var m = input.match(/(\/?)(.+)\1([a-z]*)/i); // Parse input
var m = input.match(/(\/?)(.+)\1([a-z]*)/i);
// Invalid flags
if (m[3] && !/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(m[3])) { // Invalid flags
return RegExp(input); if (m[3] && !/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(m[3])) {
return RegExp(input);
}
// Create the regular expression
return new RegExp(m[2], m[3]);
} catch {
return;
} }
// Create the regular expression
return new RegExp(m[2], m[3]);
} }
function getRegexedString(rawString, placement, { characterOverride } = {}) { function getRegexedString(rawString, placement, { characterOverride } = {}) {
@@ -52,6 +56,12 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) {
let match; let match;
let newString; let newString;
const findRegex = regexFromString(regexScript.substituteRegex ? substituteParams(regexScript.findRegex) : regexScript.findRegex); const findRegex = regexFromString(regexScript.substituteRegex ? substituteParams(regexScript.findRegex) : regexScript.findRegex);
// The user skill issued. Return with nothing.
if (!findRegex) {
return;
}
while ((match = findRegex.exec(rawString)) !== null) { while ((match = findRegex.exec(rawString)) !== null) {
const fencedMatch = match[0]; const fencedMatch = match[0];
const capturedMatch = match[1]; const capturedMatch = match[1];

View File

@@ -1,4 +1,4 @@
import { callPopup, eventSource, event_types, reloadCurrentChat, saveSettingsDebounced } from "../../../script.js"; import { callPopup, eventSource, event_types, getCurrentChatId, reloadCurrentChat, saveSettingsDebounced } from "../../../script.js";
import { extension_settings } from "../../extensions.js"; import { extension_settings } from "../../extensions.js";
import { uuidv4, waitUntilCondition } from "../../utils.js"; import { uuidv4, waitUntilCondition } from "../../utils.js";
import { regex_placement } from "./engine.js"; import { regex_placement } from "./engine.js";
@@ -50,7 +50,10 @@ async function saveRegexScript(regexScript, existingScriptIndex) {
// Markdown is global, so reload the chat. // Markdown is global, so reload the chat.
if (regexScript.placement.includes(regex_placement.MD_DISPLAY)) { if (regexScript.placement.includes(regex_placement.MD_DISPLAY)) {
await reloadCurrentChat(); const currentChatId = getCurrentChatId();
if (currentChatId !== undefined && currentChatId !== null) {
await reloadCurrentChat();
}
} }
} }