Add post-process fn to evaluation

5c90c8b1f67c23fef041f20be21f2a6932afa732

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

2 files changed, +10 -6Ignore whitespace
public/script.js+6 -4
@@ -2522,10 +2522,11 @@ export function scrollChatToBottom() {
25222522 * Substitutes {{macro}} parameters in a string.
25232523 * @param {string} content - The string to substitute parameters in.
25242524 * @param {Record<string,any>} additionalMacro - Additional environment variables for substitution.
2525+ * @param {(x: string) => string} [postProcessFn] - Post-processing function for each substituted macro.
25252526 * @returns {string} The string with substituted parameters.
25262527 */
25272528export function substituteParamsExtended(content, additionalMacro = {}, postProcessFn = (x) => x) {
25282529 return substituteParams(content, undefined, undefined, undefined, undefined, true, additionalMacro, postProcessFn);
25292530}
25302531
25312532/**
@@ -2537,9 +2538,10 @@ export function substituteParamsExtended(content, additionalMacro = {}) {
25372538 * @param {string} [_group] - The group members list for {{group}} substitution.
25382539 * @param {boolean} [_replaceCharacterCard] - Whether to replace character card macros.
25392540 * @param {Record<string,any>} [additionalMacro] - Additional environment variables for substitution.
2541+ * @param {(x: string) => string} [postProcessFn] - Post-processing function for each substituted macro.
25402542 * @returns {string} The string with substituted parameters.
25412543 */
25422544export function substituteParams(content, _name1, _name2, _original, _group, _replaceCharacterCard = true, additionalMacro = {}, postProcessFn = (x) => x) {
25432545 if (!content) {
25442546 return '';
25452547 }
@@ -2597,7 +2599,7 @@ export function substituteParams(content, _name1, _name2, _original, _group, _re
25972599 Object.assign(environment, additionalMacro);
25982600 }
25992601
26002602 return evaluateMacros(content, environment, postProcessFn);
26012603}
26022604
26032605
public/scripts/macros.js+4 -2
@@ -424,13 +424,15 @@ function getTimeDiffMacro() {
424424 * @param {string} content - The string to substitute parameters in.
425425 * @param {EnvObject} env - Map of macro names to the values they'll be substituted with. If the param
426426 * values are functions, those functions will be called and their return values are used.
427+ * @param {function(string): string} postProcessFn - Function to run on the macro value before replacing it.
427428 * @returns {string} The string with substituted parameters.
428429 */
429430export function evaluateMacros(content, env, postProcessFn) {
430431 if (!content) {
431432 return '';
432433 }
433434
435+ postProcessFn = typeof postProcessFn === 'function' ? postProcessFn : (x => x);
434436 const rawContent = content;
435437
436438 /**
@@ -514,7 +516,7 @@ export function evaluateMacros(content, env) {
514516 break;
515517 }
516518
517519 content = content.replace(macro.regex, (...args) => postProcessFn(macro.replace(...args)));
518520 }
519521
520522 return content;