Add post-process fn to evaluation
This commit is contained in:
parent
8f373cf1dc
commit
5c90c8b1f6
|
@ -2522,10 +2522,11 @@ export function scrollChatToBottom() {
|
||||||
* Substitutes {{macro}} parameters in a string.
|
* Substitutes {{macro}} parameters in a string.
|
||||||
* @param {string} content - The string to substitute parameters in.
|
* @param {string} content - The string to substitute parameters in.
|
||||||
* @param {Record<string,any>} additionalMacro - Additional environment variables for substitution.
|
* @param {Record<string,any>} additionalMacro - Additional environment variables for substitution.
|
||||||
|
* @param {(x: string) => string} [postProcessFn] - Post-processing function for each substituted macro.
|
||||||
* @returns {string} The string with substituted parameters.
|
* @returns {string} The string with substituted parameters.
|
||||||
*/
|
*/
|
||||||
export function substituteParamsExtended(content, additionalMacro = {}) {
|
export function substituteParamsExtended(content, additionalMacro = {}, postProcessFn = (x) => x) {
|
||||||
return substituteParams(content, undefined, undefined, undefined, undefined, true, additionalMacro);
|
return substituteParams(content, undefined, undefined, undefined, undefined, true, additionalMacro, postProcessFn);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2537,9 +2538,10 @@ export function substituteParamsExtended(content, additionalMacro = {}) {
|
||||||
* @param {string} [_group] - The group members list for {{group}} substitution.
|
* @param {string} [_group] - The group members list for {{group}} substitution.
|
||||||
* @param {boolean} [_replaceCharacterCard] - Whether to replace character card macros.
|
* @param {boolean} [_replaceCharacterCard] - Whether to replace character card macros.
|
||||||
* @param {Record<string,any>} [additionalMacro] - Additional environment variables for substitution.
|
* @param {Record<string,any>} [additionalMacro] - Additional environment variables for substitution.
|
||||||
|
* @param {(x: string) => string} [postProcessFn] - Post-processing function for each substituted macro.
|
||||||
* @returns {string} The string with substituted parameters.
|
* @returns {string} The string with substituted parameters.
|
||||||
*/
|
*/
|
||||||
export function substituteParams(content, _name1, _name2, _original, _group, _replaceCharacterCard = true, additionalMacro = {}) {
|
export function substituteParams(content, _name1, _name2, _original, _group, _replaceCharacterCard = true, additionalMacro = {}, postProcessFn = (x) => x) {
|
||||||
if (!content) {
|
if (!content) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
@ -2597,7 +2599,7 @@ export function substituteParams(content, _name1, _name2, _original, _group, _re
|
||||||
Object.assign(environment, additionalMacro);
|
Object.assign(environment, additionalMacro);
|
||||||
}
|
}
|
||||||
|
|
||||||
return evaluateMacros(content, environment);
|
return evaluateMacros(content, environment, postProcessFn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -424,13 +424,15 @@ function getTimeDiffMacro() {
|
||||||
* @param {string} content - The string to substitute parameters in.
|
* @param {string} content - The string to substitute parameters in.
|
||||||
* @param {EnvObject} env - Map of macro names to the values they'll be substituted with. If the param
|
* @param {EnvObject} env - Map of macro names to the values they'll be substituted with. If the param
|
||||||
* values are functions, those functions will be called and their return values are used.
|
* values are functions, those functions will be called and their return values are used.
|
||||||
|
* @param {function(string): string} postProcessFn - Function to run on the macro value before replacing it.
|
||||||
* @returns {string} The string with substituted parameters.
|
* @returns {string} The string with substituted parameters.
|
||||||
*/
|
*/
|
||||||
export function evaluateMacros(content, env) {
|
export function evaluateMacros(content, env, postProcessFn) {
|
||||||
if (!content) {
|
if (!content) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
postProcessFn = typeof postProcessFn === 'function' ? postProcessFn : (x => x);
|
||||||
const rawContent = content;
|
const rawContent = content;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -514,7 +516,7 @@ export function evaluateMacros(content, env) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
content = content.replace(macro.regex, macro.replace);
|
content = content.replace(macro.regex, (...args) => postProcessFn(macro.replace(...args)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return content;
|
return content;
|
||||||
|
|
Loading…
Reference in New Issue