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,8 +14,9 @@ const regex_placement = {
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) {
try {
// Parse input
var m = input.match(/(\/?)(.+)\1([a-z]*)/i);
@@ -26,6 +27,9 @@ function regexFromString(input) {
// Create the regular expression
return new RegExp(m[2], m[3]);
} catch {
return;
}
}
function getRegexedString(rawString, placement, { characterOverride } = {}) {
@@ -52,6 +56,12 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) {
let match;
let newString;
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) {
const fencedMatch = match[0];
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 { uuidv4, waitUntilCondition } from "../../utils.js";
import { regex_placement } from "./engine.js";
@@ -50,9 +50,12 @@ async function saveRegexScript(regexScript, existingScriptIndex) {
// Markdown is global, so reload the chat.
if (regexScript.placement.includes(regex_placement.MD_DISPLAY)) {
const currentChatId = getCurrentChatId();
if (currentChatId !== undefined && currentChatId !== null) {
await reloadCurrentChat();
}
}
}
async function deleteRegexScript({ existingId }) {
let scriptName = $(`#${existingId}`).find('.regex_script_name').text();