feat: add template engine with {{variable}} substitution
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
b6f7d0db64
commit
d87b60d98e
1 changed files with 33 additions and 0 deletions
33
src/utils/template-engine.ts
Normal file
33
src/utils/template-engine.ts
Normal 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();
|
||||
}
|
||||
Loading…
Reference in a new issue