indexNamespaces
TextReplaces
Manages text replacement handlers that process content enclosed in square brackets ([key]).
Handlers are called in the order they were added. For each handler, the current text is scanned
for all [key] patterns. If the handler's validation matches a key, the handler is
invoked with that key. If the handler returns a string, all occurrences of [key] are replaced
with the returned value. After a handler finishes processing the text, the next handler starts
on the updated text.
Each handler specifies whether it runs before or after translation via the type field in its
options (defaults to "before-translation").
Example
import { TextReplaces } from 'pixi-vn-ink'
import { getCharacterById } from "@drincs/pixi-vn";
// Replace [characterId] with the character's display name
TextReplaces.add(
(key) => {
const character = getCharacterById(key)
return character?.name
},
{
name: "character-name",
description: "Replaces character IDs with their display names",
validation: /^[a-z_]+$/,
type: "after-translation",
}
)
// "Hello [john], meet [jane]!" -> "Hello John, meet Jane!"