// transformation-rules.js
const transformations = {
// Text transformations
text: {
// Truncate to max length
truncate: (value, maxLength) => {
if (!value || value.length <= maxLength) return value;
return value.substring(0, maxLength - 3) + '...';
},
// Clean HTML tags
stripHtml: (value) => {
return value?.replace(/<[^>]*>/g, '') || '';
},
// Generate slug from title
slugify: (value) => {
return value
?.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '') || '';
}
},
// HTML transformations
html: {
// Fix relative URLs to absolute
fixUrls: (html, baseUrl) => {
return html?.replace(
/(?:src|href)="(\/[^"]+)"/g,
(match, path) => match.replace(path, baseUrl + path)
) || '';
},
// Remove WordPress shortcodes
removeShortcodes: (html) => {
return html?.replace(/\[[^\]]+\]/g, '') || '';
},
// Clean empty paragraphs
cleanEmpty: (html) => {
return html?.replace(/<p>\s*<\/p>/g, '') || '';
}
},
// Date transformations
date: {
// Convert to ISO format
toISO: (value) => {
const date = new Date(value);
return isNaN(date) ? null : date.toISOString();
},
// Parse various formats
parse: (value, format) => {
// Add custom parsing logic for specific formats
return new Date(value).toISOString();
}
},
// Reference transformations
reference: {
// Convert IDs to slugs
idToSlug: (id, lookupTable) => {
return lookupTable[id] || null;
},
// Ensure array for one-to-many
ensureArray: (value) => {
if (!value) return [];
return Array.isArray(value) ? value : [value];
}
},
// Media transformations
media: {
// Ensure HTTPS
secureUrl: (url) => {
return url?.replace(/^http:/, 'https:') || '';
},
// Extract CDN URL
extractCdn: (mediaObject) => {
return mediaObject?.url || mediaObject?.src || mediaObject || '';
}
}
};