allow char scpoed regex (#2271)

* Update engine.js to allow char scpoed regex

no ui because i'm not good at it, but works.

* add typedef

* update

* little fix

* Rework scoped scripts UI

* Add locale attributes

* Purge allowance on delete

* add d&d for `saved_scoped_scripts`

* better code

* Save settings on regex scope toggle

* Fix reordering logic

* Fix scoped setter

* Add unique ids for regex scripts

* Wording

* Reload chat after deleting scripts

* Reload chat after toggling scoped regex

---------

Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>
This commit is contained in:
steve green
2024-05-26 22:19:00 +08:00
committed by GitHub
parent 31f4a34f5a
commit 00fc40408a
11 changed files with 423 additions and 78 deletions

View File

@ -1,4 +1,4 @@
import { substituteParams } from '../../../script.js';
import { characters, substituteParams, this_chid } from '../../../script.js';
import { extension_settings } from '../../extensions.js';
import { regexFromString } from '../../utils.js';
export {
@ -22,6 +22,22 @@ const regex_placement = {
WORLD_INFO: 5,
};
function getScopedRegex() {
const isAllowed = extension_settings?.character_allowed_regex?.includes(characters?.[this_chid]?.avatar);
if (!isAllowed) {
return [];
}
const scripts = characters[this_chid]?.data?.extensions?.regex_scripts;
if (!Array.isArray(scripts)) {
return [];
}
return scripts;
}
/**
* Parent function to fetch a regexed version of a raw string
* @param {string} rawString The raw string to be regexed
@ -42,7 +58,8 @@ function getRegexedString(rawString, placement, { characterOverride, isMarkdown,
return finalString;
}
extension_settings.regex.forEach((script) => {
const allRegex = [...(extension_settings.regex ?? []), ...(getScopedRegex() ?? [])];
allRegex.forEach((script) => {
if (
// Script applies to Markdown and input is Markdown
(script.markdownOnly && isMarkdown) ||
@ -95,7 +112,7 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) {
}
// Run replacement. Currently does not support the Overlay strategy
newString = rawString.replace(findRegex, function(match) {
newString = rawString.replace(findRegex, function (match) {
const args = [...arguments];
const replaceString = regexScript.replaceString.replace(/{{match}}/gi, '$0');
const replaceWithGroups = replaceString.replaceAll(/\$(\d+)/g, (_, num) => {