mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
add /replace command
This commit is contained in:
@ -58,7 +58,7 @@ import { autoSelectPersona, retriggerFirstMessageOnEmptyChat, setPersonaLockStat
|
||||
import { addEphemeralStoppingString, chat_styles, flushEphemeralStoppingStrings, power_user } from './power-user.js';
|
||||
import { SERVER_INPUTS, textgen_types, textgenerationwebui_settings } from './textgen-settings.js';
|
||||
import { decodeTextTokens, getAvailableTokenizers, getFriendlyTokenizerName, getTextTokens, getTokenCountAsync, selectTokenizer } from './tokenizers.js';
|
||||
import { debounce, delay, equalsIgnoreCaseAndAccents, findChar, getCharIndex, isFalseBoolean, isTrueBoolean, onlyUnique, showFontAwesomePicker, stringToRange, trimToEndSentence, trimToStartSentence, waitUntilCondition } from './utils.js';
|
||||
import { debounce, delay, equalsIgnoreCaseAndAccents, findChar, getCharIndex, isFalseBoolean, isTrueBoolean, onlyUnique, regexFromString, showFontAwesomePicker, stringToRange, trimToEndSentence, trimToStartSentence, waitUntilCondition } from './utils.js';
|
||||
import { registerVariableCommands, resolveVariable } from './variables.js';
|
||||
import { background_settings } from './backgrounds.js';
|
||||
import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js';
|
||||
@ -1902,7 +1902,7 @@ export function initDefaultSlashCommands() {
|
||||
returns: 'uppercase string',
|
||||
unnamedArgumentList: [
|
||||
new SlashCommandArgument(
|
||||
'string', [ARGUMENT_TYPE.STRING], true, false,
|
||||
'text to affect', [ARGUMENT_TYPE.STRING], true, false,
|
||||
),
|
||||
],
|
||||
helpString: 'Converts the provided string to uppercase.',
|
||||
@ -1914,7 +1914,7 @@ export function initDefaultSlashCommands() {
|
||||
returns: 'lowercase string',
|
||||
unnamedArgumentList: [
|
||||
new SlashCommandArgument(
|
||||
'string', [ARGUMENT_TYPE.STRING], true, false,
|
||||
'text to affect', [ARGUMENT_TYPE.STRING], true, false,
|
||||
),
|
||||
],
|
||||
helpString: 'Converts the provided string to lowercase.',
|
||||
@ -1934,7 +1934,7 @@ export function initDefaultSlashCommands() {
|
||||
],
|
||||
unnamedArgumentList: [
|
||||
new SlashCommandArgument(
|
||||
'string', [ARGUMENT_TYPE.STRING], true, false,
|
||||
'text to affect', [ARGUMENT_TYPE.STRING], true, false,
|
||||
),
|
||||
],
|
||||
helpString: `
|
||||
@ -1998,6 +1998,62 @@ export function initDefaultSlashCommands() {
|
||||
return '';
|
||||
},
|
||||
}));
|
||||
SlashCommandParser.addCommandObject(SlashCommand.fromProps({
|
||||
name: 'replace',
|
||||
aliases: ['re'],
|
||||
callback: (async ({mode = 'literal', pattern, replacer = ''}, text) => {
|
||||
if (pattern === '')
|
||||
throw new Error("Argument of 'pattern=' cannot be empty");
|
||||
switch (mode) {
|
||||
case 'literal':
|
||||
return text.replaceAll(pattern, replacer);
|
||||
case 'regex':
|
||||
return text.replace(regexFromString(pattern), replacer);
|
||||
default:
|
||||
throw new Error("Invalid '/replace mode=' argument specified!");
|
||||
}
|
||||
}),
|
||||
returns: 'replaced text',
|
||||
namedArgumentList: [
|
||||
SlashCommandNamedArgument.fromProps({
|
||||
name: 'mode',
|
||||
description: 'Replaces occurrence(s) of a pattern',
|
||||
typeList: [ARGUMENT_TYPE.STRING],
|
||||
defaultValue: 'literal',
|
||||
enumList: ['literal', 'regex'],
|
||||
}),
|
||||
new SlashCommandNamedArgument(
|
||||
'pattern', 'pattern to search with', [ARGUMENT_TYPE.STRING], true, false,
|
||||
),
|
||||
new SlashCommandNamedArgument(
|
||||
'replacer', 'replacement text for matches', [ARGUMENT_TYPE.STRING], false, false, '',
|
||||
),
|
||||
],
|
||||
unnamedArgumentList: [
|
||||
new SlashCommandArgument(
|
||||
'text to affect', [ARGUMENT_TYPE.STRING], true, false,
|
||||
),
|
||||
],
|
||||
helpString: `
|
||||
<div>
|
||||
Replaces text within the provided string based on the pattern.
|
||||
</div>
|
||||
<div>
|
||||
If <code>mode</code> is <code>literal</code> (or omitted), <code>pattern</code> is a literal search string (case-sensitive).<br />
|
||||
If <code>mode</code> is <code>regex</code>, <code>pattern</code> is parsed as an ECMAScript Regular Expression.<br />
|
||||
The <code>replacer</code> replaces based on the <code>pattern</code> in the input text.<br />
|
||||
If <code>replacer</code> is omitted, the replacement(s) will be an empty string.<br />
|
||||
</div>
|
||||
<div>
|
||||
<strong>Example:</strong>
|
||||
<pre>/let x Blue house and blue car || </pre>
|
||||
<pre>/replace pattern="blue" {{var::x}} | /echo |/# Blue house and car ||</pre>
|
||||
<pre>/replace pattern="blue" replacer="red" {{var::x}} | /echo |/# Blue house and red car ||</pre>
|
||||
<pre>/replace mode=regex pattern="/blue/i" replacer="red" {{var::x}} | /echo |/# red house and blue car ||</pre>
|
||||
<pre>/replace mode=regex pattern="/blue/gi" replacer="red" {{var::x}} | /echo |/# red house and red car ||</pre>
|
||||
</div>
|
||||
`,
|
||||
}));
|
||||
|
||||
registerVariableCommands();
|
||||
}
|
||||
|
Reference in New Issue
Block a user