42 lines
1.3 KiB
TypeScript
Executable File
42 lines
1.3 KiB
TypeScript
Executable File
const PLACEHOLDER_RE = /\{\{(\w+)\}\}/g;
|
|
|
|
export function renderTemplate(
|
|
body: string,
|
|
data: Record<string, string | number | unknown>
|
|
): string {
|
|
return body.replace(PLACEHOLDER_RE, (_, key: string) => {
|
|
const val = data[key];
|
|
if (val === undefined || val === null) return `{{${key}}}`;
|
|
if (typeof val === "object") return JSON.stringify(val);
|
|
return String(val);
|
|
});
|
|
}
|
|
|
|
export function findMissingPlaceholders(
|
|
body: string,
|
|
data: Record<string, unknown>
|
|
): string[] {
|
|
const missing: string[] = [];
|
|
let match;
|
|
const re = /\{\{(\w+)\}\}/g;
|
|
while ((match = re.exec(body)) !== null) {
|
|
const key = match[1];
|
|
if (data[key] === undefined && !missing.includes(key)) {
|
|
missing.push(key);
|
|
}
|
|
}
|
|
return missing;
|
|
}
|
|
|
|
export const SAMPLE_PREVIEW_DATA: Record<string, unknown> = {
|
|
user_name: "Traveler",
|
|
context: { level: 5, recentReading: 42, exerciseDays: 3 },
|
|
topic: "Stoic philosophy",
|
|
week_summary: "You read 35 pages, prayed 4 days, and exercised twice.",
|
|
reading_progress: "Currently reading page 120 of 300",
|
|
exercise_summary: "2 exercise sessions this week",
|
|
prayer_summary: "4 of 5 morning prayers completed",
|
|
learning_topics: "Roman history, Latin roots",
|
|
chronicle_context: "Chapter 3: The Long Road — 45 days on the journey",
|
|
};
|