feat: shuffle macro

This commit is contained in:
2026-01-23 16:16:32 -06:00
parent 5e2d4821b9
commit ada87e2b0a
4 changed files with 93 additions and 55 deletions

View File

@@ -24,7 +24,7 @@ Get a random word from a WordList. can be used as a Macro or a Placeholder. Macr
### Shuffle ### Shuffle
Shuffle a list of values. can be used as Placeholder only. Placeholder format: `%%Shuffle::Value1::Value2::Value3;;separator%%`. Example: `%%Shuffle::Value1::Value2::Value3;;, %%` will return `Value3, Value1, Value2`. Shuffle a list of values. can be used as Placeholder only. Macro format: `{{Shuffle::Value1::Value2::Value3;;separator}}`. Placeholder format: `%%Shuffle::Value1::Value2::Value3;;separator%%`. Example: `%%Shuffle::Value1::Value2::Value3;;, %%` will return `Value3, Value1, Value2`.
### Pick ### Pick

108
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -1,7 +1,9 @@
import { setupRandomWordMacros } from '@/services/randomWord' import { setupRandomWordMacros } from '@/services/randomWord'
import { setupShuffleMacro } from '@/services/shuffle'
const initializeMacros = () => { const initializeMacros = () => {
setupRandomWordMacros() setupRandomWordMacros()
setupShuffleMacro()
} }
export default initializeMacros export default initializeMacros

View File

@@ -10,6 +10,42 @@ const fishersYatesShuffle = <Type>(arr: Type[]): Type[] => {
} }
return shuffled return shuffled
} }
export const setupShuffleMacro = () => {
const { macros } = SillyTavern.getContext()
macros.register('shuffle', {
category: 'random',
description:
'Shuffle a list of items, last item can contain a separator with the \"item;;separator\" format',
returns: 'A shuffled list of items',
exampleUsage: [
`{{shuffle::item1::item2::item3}}`,
`{{shuffle::lemon::orange::apple::banana;;, }}`
],
strictArgs: true,
list: { min: 1, max: Number.POSITIVE_INFINITY },
handler: ({ raw, list }) => {
if (
raw === undefined ||
list === null ||
list === undefined ||
list?.length === 0
) {
return ``
}
const items = raw.replace('shuffle::', '').split('::')
const lastItem = items[items.length - 1] as string
let separator = ''
if (lastItem.includes(';;')) {
const lastItemSeparator = lastItem.split(';;')
items[items.length - 1] = lastItemSeparator[0] as string
separator = lastItemSeparator[1] as string
}
return fishersYatesShuffle(items).join(separator)
}
})
}
const shufflePlaceholderRegex = /%%shuffle::([\w\W]*?)(;;[\w\W]*?)?%%/g const shufflePlaceholderRegex = /%%shuffle::([\w\W]*?)(;;[\w\W]*?)?%%/g