From b8295ac8f59e0ab1654b746a390e55935179e07d Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Fri, 28 Jun 2024 03:01:33 +0200 Subject: [PATCH] Unregister function for macro registration --- public/script.js | 1 + public/scripts/macros.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/public/script.js b/public/script.js index 8d5da9ae5..8b7b8d8f9 100644 --- a/public/script.js +++ b/public/script.js @@ -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. diff --git a/public/scripts/macros.js b/public/scripts/macros.js index 992150894..ecdb453c6 100644 --- a/public/scripts/macros.js +++ b/public/scripts/macros.js @@ -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