feat: add template engine with {{variable}} substitution

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Luca Oelfke 2026-02-13 13:12:43 +01:00
parent b6f7d0db64
commit d87b60d98e

View file

@ -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, string>,
): string {
const now = new Date();
const builtins: Record<string, string> = {
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();
}