feat: preset stili contenuto con whitelist

This commit is contained in:
2026-07-05 15:40:09 +02:00
parent 5f1d9f3740
commit 6f51aec4c6
3 changed files with 74 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
export const STYLE_GROUPS = {
size: ['s', 'm', 'l'],
color: ['accent', 'accent-dark', 'dark', 'heading', 'text', 'text-light', 'white'],
weight: ['normal', 'bold'],
style: ['normal', 'italic'],
align: ['left', 'center', 'right'],
} as const;
export type StyleGroup = keyof typeof STYLE_GROUPS;
export function validateStyles(input: unknown):
| { ok: true; styles: Record<string, string> }
| { ok: false; error: string } {
if (typeof input !== 'object' || input === null || Array.isArray(input)) {
return { ok: false, error: 'Stili non validi.' };
}
const out: Record<string, string> = {};
for (const [k, v] of Object.entries(input)) {
const allowed = (STYLE_GROUPS as Record<string, readonly string[]>)[k];
if (!allowed) return { ok: false, error: `Gruppo stile sconosciuto: ${k}` };
if (typeof v !== 'string' || !allowed.includes(v)) {
return { ok: false, error: `Valore non ammesso per ${k}: ${String(v)}` };
}
out[k] = v;
}
return { ok: true, styles: out };
}
export function stylesToClasses(styles: Record<string, string>): string {
return Object.entries(styles).map(([k, v]) => `cs-${k}-${v}`).join(' ');
}