mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
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:
@@ -14,18 +14,22 @@ 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) {
|
||||
// Parse input
|
||||
var m = input.match(/(\/?)(.+)\1([a-z]*)/i);
|
||||
|
||||
// Invalid flags
|
||||
if (m[3] && !/^(?!.*?(.).*?\1)[gmixXsuUAJ]+$/.test(m[3])) {
|
||||
return RegExp(input);
|
||||
try {
|
||||
// Parse input
|
||||
var m = input.match(/(\/?)(.+)\1([a-z]*)/i);
|
||||
|
||||
// Invalid flags
|
||||
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 } = {}) {
|
||||
@@ -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];
|
||||
|
Reference in New Issue
Block a user