From d87b60d98e6ff312e0ed1a4f99b1cbb33f0fbadb Mon Sep 17 00:00:00 2001 From: tolvitty Date: Fri, 13 Feb 2026 13:12:43 +0100 Subject: [PATCH] feat: add template engine with {{variable}} substitution Co-Authored-By: Claude Opus 4.6 --- src/utils/template-engine.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/utils/template-engine.ts diff --git a/src/utils/template-engine.ts b/src/utils/template-engine.ts new file mode 100644 index 0000000..5489e63 --- /dev/null +++ b/src/utils/template-engine.ts @@ -0,0 +1,33 @@ +/** + * Simple {{variable}} substitution engine. + * Built-in variables: {{date}}, {{time}}, {{datetime}}. + * All other {{keys}} are resolved from the provided values map. + */ +export function renderTemplate( + template: string, + values: Record, +): string { + const now = new Date(); + const builtins: Record = { + date: now.toISOString().slice(0, 10), + time: now.toTimeString().slice(0, 5), + datetime: `${now.toISOString().slice(0, 10)} ${now.toTimeString().slice(0, 5)}`, + }; + + const allValues = { ...builtins, ...values }; + + return template.replace(/\{\{(\w[\w-]*)\}\}/g, (match, key: string) => { + return allValues[key] ?? match; + }); +} + +/** + * Sanitize a string for use as a file name. + * Removes characters illegal on Windows/macOS/Linux. + */ +export function sanitizeFileName(name: string): string { + return name + .replace(/[\\/:*?"<>|]/g, '-') + .replace(/\s+/g, ' ') + .trim(); +}