Add 3-state find substitution select Fixes #3104
| @@ -89,7 +89,7 @@ | ||
| 89 | 89 | * @property {boolean} markdownOnly - Whether the script only applies to Markdown |
| 90 | 90 | * @property {boolean} promptOnly - Whether the script only applies to prompts |
| 91 | 91 | * @property {boolean} runOnEdit - Whether the script runs on edit |
| 92 | 92 | * @property {booleannumber} substituteRegex - Whether the regex should be substituted |
| 93 | 93 | * @property {number} minDepth - The minimum depth |
| 94 | 94 | * @property {number} maxDepth - The maximum depth |
| 95 | 95 | */ |
| @@ -121,12 +121,16 @@ | ||
| 121 | 121 | <input type="checkbox" name="run_on_edit" /> |
| 122 | 122 | <span data-i18n="Run On Edit">Run On Edit</span> |
| 123 | 123 | </label> |
| 124 | 124 | <label class="checkbox flex-container flexNoGap marginBot5" data-i18n="[title]ext_regex_substitute_regex_desc" title="Substitute {{macros}} in Find Regex before running it"> |
| 125 | - <input type="checkbox" name="substitute_regex" /> | |
| 126 | 125 | <span> |
| 127 | 126 | <spansmall data-i18n="SubstituteMacro in Find Regex">SubstituteMacros in Find Regex</spansmall> |
| 128 | 127 | <span class="fa-solid fa-circle-question note-link-span"></span> |
| 129 | 128 | </span> |
| 129 | + <select name="substitute_regex" class="text_pole textarea_compact margin0"> | |
| 130 | + <option value="0" data-i18n="Don't substitute">Don't substitute</option> | |
| 131 | + <option value="1" data-i18n="Substitute (raw)">Substitute (raw)</option> | |
| 132 | + <option value="2" data-i18n="Substitute (escaped)">Substitute (escaped)</option> | |
| 133 | + </select> | |
| 130 | 134 | </label> |
| 131 | 135 | <span> |
| 132 | 136 | <small data-i18n="ext_regex_other_options" data-i18n="Ephemerality">Ephemerality</small> |
| @@ -22,6 +22,12 @@ const regex_placement = { | ||
| 22 | 22 | WORLD_INFO: 5, |
| 23 | 23 | }; |
| 24 | 24 | |
| 25 | +export const substitute_find_regex = { | |
| 26 | + NONE: 0, | |
| 27 | + RAW: 1, | |
| 28 | + ESCAPED: 2, | |
| 29 | +}; | |
| 30 | + | |
| 25 | 31 | function sanitizeRegexMacro(x) { |
| 26 | 32 | return (x && typeof x === 'string') ? |
| 27 | 33 | x.replaceAll(/[\n\r\t\v\f\0.^$*+?{}[\]\\/|()]/gs, function (s) { |
| @@ -131,9 +137,20 @@ function runRegexScript(regexScript, rawString, { characterOverride } = {}) { | ||
| 131 | 137 | return newString; |
| 132 | 138 | } |
| 133 | 139 | |
| 134 | 140 | const regexStringgetRegexString = regexScript.substituteRegex() => { |
| 135 | - ? substituteParamsExtended(regexScript.findRegex, {}, sanitizeRegexMacro) | |
| 141 | + switch(Number(regexScript.substituteRegex)) { | |
| 136 | - : regexScript.findRegex; | |
| 142 | + case substitute_find_regex.NONE: | |
| 143 | + return regexScript.findRegex; | |
| 144 | + case substitute_find_regex.RAW: | |
| 145 | + return substituteParamsExtended(regexScript.findRegex); | |
| 146 | + case substitute_find_regex.ESCAPED: | |
| 147 | + return substituteParamsExtended(regexScript.findRegex, {}, sanitizeRegexMacro); | |
| 148 | + default: | |
| 149 | + console.warn(`runRegexScript: Unknown substituteRegex value ${regexScript.substituteRegex}. Using raw regex.`); | |
| 150 | + return regexScript.findRegex; | |
| 151 | + } | |
| 152 | + }; | |
| 153 | + const regexString = getRegexString(); | |
| 137 | 154 | const findRegex = regexFromString(regexString); |
| 138 | 155 | |
| 139 | 156 | // The user skill issued. Return with nothing. |
| @@ -8,7 +8,7 @@ import { enumIcons } from '../../slash-commands/SlashCommandCommonEnumsProvider. | ||
| 8 | 8 | import { SlashCommandEnumValue, enumTypes } from '../../slash-commands/SlashCommandEnumValue.js'; |
| 9 | 9 | import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; |
| 10 | 10 | import { download, getFileText, getSortableDelay, uuidv4 } from '../../utils.js'; |
| 11 | 11 | import { regex_placement, runRegexScript, substitute_find_regex } from './engine.js'; |
| 12 | 12 | import { t } from '../../i18n.js'; |
| 13 | 13 | |
| 14 | 14 | /** |
| @@ -227,7 +227,7 @@ async function onRegexEditorOpenClick(existingId, isScoped) { | ||
| 227 | 227 | editorHtml.find('input[name="only_format_display"]').prop('checked', existingScript.markdownOnly ?? false); |
| 228 | 228 | editorHtml.find('input[name="only_format_prompt"]').prop('checked', existingScript.promptOnly ?? false); |
| 229 | 229 | editorHtml.find('input[name="run_on_edit"]').prop('checked', existingScript.runOnEdit ?? false); |
| 230 | 230 | editorHtml.find('inputselect[name="substitute_regex"]').propval('checked', existingScript.substituteRegex ?? falsesubstitute_find_regex.NONE); |
| 231 | 231 | editorHtml.find('input[name="min_depth"]').val(existingScript.minDepth ?? ''); |
| 232 | 232 | editorHtml.find('input[name="max_depth"]').val(existingScript.maxDepth ?? ''); |
| 233 | 233 | |
| @@ -267,7 +267,7 @@ async function onRegexEditorOpenClick(existingId, isScoped) { | ||
| 267 | 267 | findRegex: editorHtml.find('.find_regex').val(), |
| 268 | 268 | replaceString: editorHtml.find('.regex_replace_string').val(), |
| 269 | 269 | trimStrings: String(editorHtml.find('.regex_trim_strings').val()).split('\n').filter((e) => e.length !== 0) || [], |
| 270 | 270 | substituteRegex: Number(editorHtml.find('inputselect[name="substitute_regex"]').propval('checked')), |
| 271 | 271 | }; |
| 272 | 272 | const rawTestString = String(editorHtml.find('#regex_test_input').val()); |
| 273 | 273 | const result = runRegexScript(testScript, rawTestString); |
| @@ -295,7 +295,7 @@ async function onRegexEditorOpenClick(existingId, isScoped) { | ||
| 295 | 295 | markdownOnly: editorHtml.find('input[name="only_format_display"]').prop('checked'), |
| 296 | 296 | promptOnly: editorHtml.find('input[name="only_format_prompt"]').prop('checked'), |
| 297 | 297 | runOnEdit: editorHtml.find('input[name="run_on_edit"]').prop('checked'), |
| 298 | 298 | substituteRegex: Number(editorHtml.find('inputselect[name="substitute_regex"]').propval('checked')), |
| 299 | 299 | minDepth: parseInt(String(editorHtml.find('input[name="min_depth"]').val())), |
| 300 | 300 | maxDepth: parseInt(String(editorHtml.find('input[name="max_depth"]').val())), |
| 301 | 301 | }; |