Add post-process fn to evaluation
| @@ -2522,10 +2522,11 @@ export function scrollChatToBottom() { | ||
| 2522 | 2522 | * Substitutes {{macro}} parameters in a string. |
| 2523 | 2523 | * @param {string} content - The string to substitute parameters in. |
| 2524 | 2524 | * @param {Record<string,any>} additionalMacro - Additional environment variables for substitution. |
| 2525 | + * @param {(x: string) => string} [postProcessFn] - Post-processing function for each substituted macro. | |
| 2525 | 2526 | * @returns {string} The string with substituted parameters. |
| 2526 | 2527 | */ |
| 2527 | 2528 | export function substituteParamsExtended(content, additionalMacro = {}, postProcessFn = (x) => x) { |
| 2528 | 2529 | return substituteParams(content, undefined, undefined, undefined, undefined, true, additionalMacro, postProcessFn); |
| 2529 | 2530 | } |
| 2530 | 2531 | |
| 2531 | 2532 | /** |
| @@ -2537,9 +2538,10 @@ export function substituteParamsExtended(content, additionalMacro = {}) { | ||
| 2537 | 2538 | * @param {string} [_group] - The group members list for {{group}} substitution. |
| 2538 | 2539 | * @param {boolean} [_replaceCharacterCard] - Whether to replace character card macros. |
| 2539 | 2540 | * @param {Record<string,any>} [additionalMacro] - Additional environment variables for substitution. |
| 2541 | + * @param {(x: string) => string} [postProcessFn] - Post-processing function for each substituted macro. | |
| 2540 | 2542 | * @returns {string} The string with substituted parameters. |
| 2541 | 2543 | */ |
| 2542 | 2544 | export function substituteParams(content, _name1, _name2, _original, _group, _replaceCharacterCard = true, additionalMacro = {}, postProcessFn = (x) => x) { |
| 2543 | 2545 | if (!content) { |
| 2544 | 2546 | return ''; |
| 2545 | 2547 | } |
| @@ -2597,7 +2599,7 @@ export function substituteParams(content, _name1, _name2, _original, _group, _re | ||
| 2597 | 2599 | Object.assign(environment, additionalMacro); |
| 2598 | 2600 | } |
| 2599 | 2601 | |
| 2600 | 2602 | return evaluateMacros(content, environment, postProcessFn); |
| 2601 | 2603 | } |
| 2602 | 2604 | |
| 2603 | 2605 | |
| @@ -424,13 +424,15 @@ function getTimeDiffMacro() { | ||
| 424 | 424 | * @param {string} content - The string to substitute parameters in. |
| 425 | 425 | * @param {EnvObject} env - Map of macro names to the values they'll be substituted with. If the param |
| 426 | 426 | * 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. | |
| 427 | 428 | * @returns {string} The string with substituted parameters. |
| 428 | 429 | */ |
| 429 | 430 | export function evaluateMacros(content, env, postProcessFn) { |
| 430 | 431 | if (!content) { |
| 431 | 432 | return ''; |
| 432 | 433 | } |
| 433 | 434 | |
| 435 | + postProcessFn = typeof postProcessFn === 'function' ? postProcessFn : (x => x); | |
| 434 | 436 | const rawContent = content; |
| 435 | 437 | |
| 436 | 438 | /** |
| @@ -514,7 +516,7 @@ export function evaluateMacros(content, env) { | ||
| 514 | 516 | break; |
| 515 | 517 | } |
| 516 | 518 | |
| 517 | 519 | content = content.replace(macro.regex, (...args) => postProcessFn(macro.replace(...args))); |
| 518 | 520 | } |
| 519 | 521 | |
| 520 | 522 | return content; |