mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add enumerator for MacrosParser
This commit is contained in:
@ -26,6 +26,12 @@ Handlebars.registerHelper('helperMissing', function () {
|
||||
* @typedef {(nonce: string) => string} MacroFunction
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} CustomMacro
|
||||
* @property {string} key - Macro name (key)
|
||||
* @property {string} description - Optional description of the macro
|
||||
*/
|
||||
|
||||
export class MacrosParser {
|
||||
/**
|
||||
* A map of registered macros.
|
||||
@ -33,12 +39,29 @@ export class MacrosParser {
|
||||
*/
|
||||
static #macros = new Map();
|
||||
|
||||
/**
|
||||
* A map of macro descriptions.
|
||||
* @type {Map<string, string>}
|
||||
*/
|
||||
static #descriptions = new Map();
|
||||
|
||||
/**
|
||||
* Returns an iterator over all registered macros.
|
||||
* @returns {IterableIterator<CustomMacro>}
|
||||
*/
|
||||
static [Symbol.iterator] = function* () {
|
||||
for (const macro of this.#macros.values()) {
|
||||
yield { key: macro, description: this.#descriptions.get(macro) };
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Registers a global macro that can be used anywhere where substitution is allowed.
|
||||
* @param {string} key Macro name (key)
|
||||
* @param {string|MacroFunction} value A string or a function that returns a string
|
||||
* @param {string} [description] Optional description of the macro
|
||||
*/
|
||||
static registerMacro(key, value) {
|
||||
static registerMacro(key, value, description = '') {
|
||||
if (typeof key !== 'string') {
|
||||
throw new Error('Macro key must be a string');
|
||||
}
|
||||
@ -64,6 +87,10 @@ export class MacrosParser {
|
||||
}
|
||||
|
||||
this.#macros.set(key, value);
|
||||
|
||||
if (typeof description === 'string' && description) {
|
||||
this.#descriptions.set(key, description);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -88,6 +115,8 @@ export class MacrosParser {
|
||||
if (!deleted) {
|
||||
console.warn(`Macro ${key} was not registered`);
|
||||
}
|
||||
|
||||
this.#descriptions.delete(key);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user