feat: implement Dynamic Data Binding system and tags registry
All checks were successful
Production Build & Deploy / build-and-deploy (push) Successful in 1m0s
All checks were successful
Production Build & Deploy / build-and-deploy (push) Successful in 1m0s
This commit is contained in:
49
src/utils/dynamicDataBinding.ts
Normal file
49
src/utils/dynamicDataBinding.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
export interface DynamicFieldDefinition {
|
||||
key: string; // e.g. 'site.siteName', 'post.title', 'custom.clientName'
|
||||
label: string;
|
||||
category: 'site' | 'post' | 'author' | 'custom';
|
||||
defaultValue: string;
|
||||
}
|
||||
|
||||
export const DYNAMIC_FIELDS_REGISTRY: DynamicFieldDefinition[] = [
|
||||
{ key: 'site.siteName', label: 'Website Name', category: 'site', defaultValue: 'N&D IT Solutions' },
|
||||
{ key: 'site.metaDescription', label: 'Website Beschreibung', category: 'site', defaultValue: 'High-Performance Webdesign & Digital Solutions' },
|
||||
{ key: 'site.contactEmail', label: 'Kontakt E-Mail', category: 'site', defaultValue: 'kontakt@ndsolutions.de' },
|
||||
{ key: 'site.phone', label: 'Telefonnummer', category: 'site', defaultValue: '+49 (0) 123 456789' },
|
||||
{ key: 'post.title', label: 'Beitragstitel (Dynamisch)', category: 'post', defaultValue: 'Beispiel Artikel' },
|
||||
{ key: 'post.excerpt', label: 'Beitrags Auszug', category: 'post', defaultValue: 'Ein kurzer Auszug des Artikels...' },
|
||||
{ key: 'author.name', label: 'Autor Name', category: 'author', defaultValue: 'N&D Team' }
|
||||
];
|
||||
|
||||
/**
|
||||
* Replaces dynamic binding tags like `{{site.siteName}}` or evaluates binding keys against a data context.
|
||||
*/
|
||||
export function resolveDynamicBinding(
|
||||
content: string,
|
||||
contextData: Record<string, any>
|
||||
): string {
|
||||
if (!content) return content;
|
||||
|
||||
return content.replace(/\{\{([^}]+)\}\}/g, (_, key) => {
|
||||
const trimmedKey = key.trim();
|
||||
const keys = trimmedKey.split('.');
|
||||
let val: any = contextData;
|
||||
|
||||
for (const k of keys) {
|
||||
if (val && typeof val === 'object' && k in val) {
|
||||
val = val[k];
|
||||
} else {
|
||||
val = undefined;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (val !== undefined && val !== null) {
|
||||
return String(val);
|
||||
}
|
||||
|
||||
// Fallback to registry default if available
|
||||
const regItem = DYNAMIC_FIELDS_REGISTRY.find(f => f.key === trimmedKey);
|
||||
return regItem ? regItem.defaultValue : `{{${trimmedKey}}}`;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user