Merge pull request #3377 from Succubyss/fnr Adds /replace

f155cc7e925977197db89af8eb9eb1889da4368f

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +60 -4Ignore whitespace
public/scripts/slash-commands.js+60 -4
@@ -59,7 +59,7 @@ import { autoSelectPersona, retriggerFirstMessageOnEmptyChat, setPersonaLockStat
5959import { addEphemeralStoppingString, chat_styles, flushEphemeralStoppingStrings, power_user } from './power-user.js';
6060import { SERVER_INPUTS, textgen_types, textgenerationwebui_settings } from './textgen-settings.js';
6161import { decodeTextTokens, getAvailableTokenizers, getFriendlyTokenizerName, getTextTokens, getTokenCountAsync, selectTokenizer } from './tokenizers.js';
6262import { debounce, delay, equalsIgnoreCaseAndAccents, findChar, getCharIndex, isFalseBoolean, isTrueBoolean, onlyUnique, regexFromString, showFontAwesomePicker, stringToRange, trimToEndSentence, trimToStartSentence, waitUntilCondition } from './utils.js';
6363import { registerVariableCommands, resolveVariable } from './variables.js';
6464import { background_settings } from './backgrounds.js';
6565import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
@@ -1903,7 +1903,7 @@ export function initDefaultSlashCommands() {
19031903 returns: 'uppercase string',
19041904 unnamedArgumentList: [
19051905 new SlashCommandArgument(
19061906 'stringtext to affect', [ARGUMENT_TYPE.STRING], true, false,
19071907 ),
19081908 ],
19091909 helpString: 'Converts the provided string to uppercase.',
@@ -1915,7 +1915,7 @@ export function initDefaultSlashCommands() {
19151915 returns: 'lowercase string',
19161916 unnamedArgumentList: [
19171917 new SlashCommandArgument(
19181918 'stringtext to affect', [ARGUMENT_TYPE.STRING], true, false,
19191919 ),
19201920 ],
19211921 helpString: 'Converts the provided string to lowercase.',
@@ -1935,7 +1935,7 @@ export function initDefaultSlashCommands() {
19351935 ],
19361936 unnamedArgumentList: [
19371937 new SlashCommandArgument(
19381938 'stringtext to affect', [ARGUMENT_TYPE.STRING], true, false,
19391939 ),
19401940 ],
19411941 helpString: `
@@ -1999,6 +1999,62 @@ export function initDefaultSlashCommands() {
19991999 return '';
20002000 },
20012001 }));
2002+ SlashCommandParser.addCommandObject(SlashCommand.fromProps({
2003+ name: 'replace',
2004+ aliases: ['re'],
2005+ callback: (async ({mode = 'literal', pattern, replacer = ''}, text) => {
2006+ if (pattern === '')
2007+ throw new Error("Argument of 'pattern=' cannot be empty");
2008+ switch (mode) {
2009+ case 'literal':
2010+ return text.replaceAll(pattern, replacer);
2011+ case 'regex':
2012+ return text.replace(regexFromString(pattern), replacer);
2013+ default:
2014+ throw new Error("Invalid '/replace mode=' argument specified!");
2015+ }
2016+ }),
2017+ returns: 'replaced text',
2018+ namedArgumentList: [
2019+ SlashCommandNamedArgument.fromProps({
2020+ name: 'mode',
2021+ description: 'Replaces occurrence(s) of a pattern',
2022+ typeList: [ARGUMENT_TYPE.STRING],
2023+ defaultValue: 'literal',
2024+ enumList: ['literal', 'regex'],
2025+ }),
2026+ new SlashCommandNamedArgument(
2027+ 'pattern', 'pattern to search with', [ARGUMENT_TYPE.STRING], true, false,
2028+ ),
2029+ new SlashCommandNamedArgument(
2030+ 'replacer', 'replacement text for matches', [ARGUMENT_TYPE.STRING], false, false, '',
2031+ ),
2032+ ],
2033+ unnamedArgumentList: [
2034+ new SlashCommandArgument(
2035+ 'text to affect', [ARGUMENT_TYPE.STRING], true, false,
2036+ ),
2037+ ],
2038+ helpString: `
2039+ <div>
2040+ Replaces text within the provided string based on the pattern.
2041+ </div>
2042+ <div>
2043+ If <code>mode</code> is <code>literal</code> (or omitted), <code>pattern</code> is a literal search string (case-sensitive).<br />
2044+ If <code>mode</code> is <code>regex</code>, <code>pattern</code> is parsed as an ECMAScript Regular Expression.<br />
2045+ The <code>replacer</code> replaces based on the <code>pattern</code> in the input text.<br />
2046+ If <code>replacer</code> is omitted, the replacement(s) will be an empty string.<br />
2047+ </div>
2048+ <div>
2049+ <strong>Example:</strong>
2050+ <pre>/let x Blue house and blue car || </pre>
2051+ <pre>/replace pattern="blue" {{var::x}} | /echo |/# Blue house and car ||</pre>
2052+ <pre>/replace pattern="blue" replacer="red" {{var::x}} | /echo |/# Blue house and red car ||</pre>
2053+ <pre>/replace mode=regex pattern="/blue/i" replacer="red" {{var::x}} | /echo |/# red house and blue car ||</pre>
2054+ <pre>/replace mode=regex pattern="/blue/gi" replacer="red" {{var::x}} | /echo |/# red house and red car ||</pre>
2055+ </div>
2056+ `,
2057+ }));
20022058
20032059 registerVariableCommands();
20042060}