diff --git a/public/scripts/macros.js b/public/scripts/macros.js index e88971de1..5e6b37b7b 100644 --- a/public/scripts/macros.js +++ b/public/scripts/macros.js @@ -210,11 +210,22 @@ export function evaluateMacros(content, env) { return ''; } + // Legacy non-macro substitutions + content = content.replace(//gi, typeof env.user === 'function' ? env.user() : env.user); + content = content.replace(//gi, typeof env.char === 'function' ? env.char() : env.char); + content = content.replace(//gi, typeof env.group === 'function' ? env.group() : env.group); + content = content.replace(//gi, typeof env.group === 'function' ? env.group() : env.group); + + // Short circuit if there are no macros + if (!content.includes('{{')) { + return content; + } + content = diceRollReplace(content); content = replaceInstructMacros(content); content = replaceVariableMacros(content); content = content.replace(/{{newline}}/gi, '\n'); - content = content.replace(/{{input}}/gi, String($('#send_textarea').val())); + content = content.replace(/{{input}}/gi, () => String($('#send_textarea').val())); // Substitute passed-in variables for (const varName in env) { @@ -225,25 +236,19 @@ export function evaluateMacros(content, env) { } content = content.replace(/{{maxPrompt}}/gi, () => String(getMaxContextSize())); - content = content.replace(/{{lastMessage}}/gi, getLastMessage()); - content = content.replace(/{{lastMessageId}}/gi, getLastMessageId()); - content = content.replace(/{{firstIncludedMessageId}}/gi, getFirstIncludedMessageId()); - content = content.replace(/{{lastSwipeId}}/gi, getLastSwipeId()); - content = content.replace(/{{currentSwipeId}}/gi, getCurrentSwipeId()); - - // Legacy non-macro substitutions - content = content.replace(//gi, typeof env.user === 'function' ? env.user() : env.user); - content = content.replace(//gi, typeof env.char === 'function' ? env.char() : env.char); - content = content.replace(//gi, typeof env.group === 'function' ? env.group() : env.group); - content = content.replace(//gi, typeof env.group === 'function' ? env.group() : env.group); + content = content.replace(/{{lastMessage}}/gi, () => getLastMessage()); + content = content.replace(/{{lastMessageId}}/gi, () => getLastMessageId()); + content = content.replace(/{{firstIncludedMessageId}}/gi, () => getFirstIncludedMessageId()); + content = content.replace(/{{lastSwipeId}}/gi, () => getLastSwipeId()); + content = content.replace(/{{currentSwipeId}}/gi, () => getCurrentSwipeId()); content = content.replace(/\{\{\/\/([\s\S]*?)\}\}/gm, ''); - content = content.replace(/{{time}}/gi, moment().format('LT')); - content = content.replace(/{{date}}/gi, moment().format('LL')); - content = content.replace(/{{weekday}}/gi, moment().format('dddd')); - content = content.replace(/{{isotime}}/gi, moment().format('HH:mm')); - content = content.replace(/{{isodate}}/gi, moment().format('YYYY-MM-DD')); + content = content.replace(/{{time}}/gi, () => moment().format('LT')); + content = content.replace(/{{date}}/gi, () => moment().format('LL')); + content = content.replace(/{{weekday}}/gi, () => moment().format('dddd')); + content = content.replace(/{{isotime}}/gi, () => moment().format('HH:mm')); + content = content.replace(/{{isodate}}/gi, () => moment().format('YYYY-MM-DD')); content = content.replace(/{{datetimeformat +([^}]*)}}/gi, (_, format) => { const formattedTime = moment().format(format);