fix(email): preserve personalization tokens for template rendering
This commit is contained in:
1 parent
8bc8fe85c3
commit
e1ea6e2977
5 files changed
+41
-3
No files matched your search
@@ -27,9 +27,11 @@ export function resolveDocumentVariables(
|
||||
document: BlockDocument,
|
||||
catalog: Record<string, "string" | "number" | "date" | "boolean">,
|
||||
data: Record<string, unknown> = {},
|
||||
options: { requireValues?: boolean } = {},
|
||||
options: { requireValues?: boolean; preserveTokens?: boolean } = {},
|
||||
): Result<BlockDocument> {
|
||||
const diagnostics: Diagnostic[] = [];
|
||||
if (options.preserveTokens && options.requireValues)
|
||||
return { ok: false, errors: [{ code: "variable.mode", message: "Template tokens cannot be preserved when resolved values are required.", severity: "error" }] };
|
||||
const visit = (value: JsonValue, path: string): JsonValue => {
|
||||
if (typeof value === "string")
|
||||
return value.replace(TOKEN, (raw, key: string, fallback: string | undefined) => {
|
||||
@@ -38,6 +40,7 @@ export function resolveDocumentVariables(
|
||||
diagnostics.push({ code: "variable.unknown", message: `Unknown variable ${key}.`, severity: "error", path });
|
||||
return raw;
|
||||
}
|
||||
if (options.preserveTokens) return raw;
|
||||
const supplied = data[key];
|
||||
if (supplied === undefined || supplied === null || supplied === "") {
|
||||
if (fallback !== undefined) return fallback.trim();
|
||||
|
||||
Reference in new issue
Block a user