Unregister function for macro registration

This commit is contained in:
Wolfsblvt 2024-06-28 03:01:33 +02:00
parent cf56bfb6a9
commit b8295ac8f5
2 changed files with 25 additions and 0 deletions

View File

@ -7800,6 +7800,7 @@ window['SillyTavern'].getContext = function () {
*/
registerHelper: () => { },
registerMacro: MacrosParser.registerMacro.bind(MacrosParser),
unregisterMacro: MacrosParser.unregisterMacro.bind(MacrosParser),
registedDebugFunction: registerDebugFunction,
/**
* @deprecated Use renderExtensionTemplateAsync instead.

View File

@ -58,6 +58,30 @@ export class MacrosParser {
this.#macros.set(key, value);
}
/**
* Unregisters a global macro with the given key
*
* @param {string} key Macro name (key)
*/
static unregisterMacro(key) {
if (typeof key !== 'string') {
throw new Error('Macro key must be a string');
}
// Allowing surrounding whitespace would just create more confusion...
key = key.trim();
if (!key) {
throw new Error('Macro key must not be empty or whitespace only');
}
const deleted = this.#macros.delete(key);
if (!deleted) {
console.warn(`Macro ${key} was not registered`);
}
}
/**
* Populate the env object with macro values from the current context.
* @param {EnvObject} env Env object for the current evaluation context