Add post-process fn to evaluation

5c90c8b1f67c23fef041f20be21f2a6932afa732

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

2 files changed, +10 -6Showing whitespace changes
public/script.js+6 -4
@@ -2522,10 +2522,11 @@ export function scrollChatToBottom() {
2522 * Substitutes {{macro}} parameters in a string.2522 * Substitutes {{macro}} parameters in a string.
2523 * @param {string} content - The string to substitute parameters in.2523 * @param {string} content - The string to substitute parameters in.
2524 * @param {Record<string,any>} additionalMacro - Additional environment variables for substitution.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 * @returns {string} The string with substituted parameters.2526 * @returns {string} The string with substituted parameters.
2526 */2527 */
2527export function substituteParamsExtended(content, additionalMacro = {}) {2528export function substituteParamsExtended(content, additionalMacro = {}, postProcessFn = (x) => x) {
2528 return substituteParams(content, undefined, undefined, undefined, undefined, true, additionalMacro);2529 return substituteParams(content, undefined, undefined, undefined, undefined, true, additionalMacro, postProcessFn);
2529}2530}
25302531
2531/**2532/**
@@ -2537,9 +2538,10 @@ export function substituteParamsExtended(content, additionalMacro = {}) {
2537 * @param {string} [_group] - The group members list for {{group}} substitution.2538 * @param {string} [_group] - The group members list for {{group}} substitution.
2538 * @param {boolean} [_replaceCharacterCard] - Whether to replace character card macros.2539 * @param {boolean} [_replaceCharacterCard] - Whether to replace character card macros.
2539 * @param {Record<string,any>} [additionalMacro] - Additional environment variables for substitution.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 * @returns {string} The string with substituted parameters.2542 * @returns {string} The string with substituted parameters.
2541 */2543 */
2542export function substituteParams(content, _name1, _name2, _original, _group, _replaceCharacterCard = true, additionalMacro = {}) {2544export function substituteParams(content, _name1, _name2, _original, _group, _replaceCharacterCard = true, additionalMacro = {}, postProcessFn = (x) => x) {
2543 if (!content) {2545 if (!content) {
2544 return '';2546 return '';
2545 }2547 }
@@ -2597,7 +2599,7 @@ export function substituteParams(content, _name1, _name2, _original, _group, _re
2597 Object.assign(environment, additionalMacro);2599 Object.assign(environment, additionalMacro);
2598 }2600 }
25992601
2600 return evaluateMacros(content, environment);2602 return evaluateMacros(content, environment, postProcessFn);
2601}2603}
26022604
26032605
public/scripts/macros.js+4 -2
@@ -424,13 +424,15 @@ function getTimeDiffMacro() {
424 * @param {string} content - The string to substitute parameters in.424 * @param {string} content - The string to substitute parameters in.
425 * @param {EnvObject} env - Map of macro names to the values they'll be substituted with. If the param425 * @param {EnvObject} env - Map of macro names to the values they'll be substituted with. If the param
426 * values are functions, those functions will be called and their return values are used.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 * @returns {string} The string with substituted parameters.428 * @returns {string} The string with substituted parameters.
428 */429 */
429export function evaluateMacros(content, env) {430export function evaluateMacros(content, env, postProcessFn) {
430 if (!content) {431 if (!content) {
431 return '';432 return '';
432 }433 }
433434
435 postProcessFn = typeof postProcessFn === 'function' ? postProcessFn : (x => x);
434 const rawContent = content;436 const rawContent = content;
435437
436 /**438 /**
@@ -514,7 +516,7 @@ export function evaluateMacros(content, env) {
514 break;516 break;
515 }517 }
516518
517 content = content.replace(macro.regex, macro.replace);519 content = content.replace(macro.regex, (...args) => postProcessFn(macro.replace(...args)));
518 }520 }
519521
520 return content;522 return content;