Files
klz-cables.com/scripts/fix-video-attrs.js
2026-01-06 13:55:04 +01:00

144 lines
4.9 KiB
JavaScript

#!/usr/bin/env node
/**
* Script to move video attributes from excerptHtml to contentHtml
* This fixes the issue where video background attributes are in excerptHtml
* but ContentRenderer never sees them because it processes contentHtml
*/
const fs = require('fs');
const path = require('path');
const PROCESSED_DIR = path.join(__dirname, '..', 'data', 'processed');
// Function to extract video attributes from excerptHtml
function extractVideoAttributes(excerptHtml) {
if (!excerptHtml) return null;
// Look for video attributes in vc_row elements
const videoMp4Match = excerptHtml.match(/video_mp4="([^"]*)"/i);
const videoWebmMatch = excerptHtml.match(/video_webm="([^"]*)"/i);
const videoBgMatch = excerptHtml.match(/video_bg="([^"]*)"/i);
// Also check for data attributes
const dataVideoMp4Match = excerptHtml.match(/data-video-mp4="([^"]*)"/i);
const dataVideoWebmMatch = excerptHtml.match(/data-video-webm="([^"]*)"/i);
const dataVideoBgMatch = excerptHtml.match(/data-video-bg="([^"]*)"/i);
const videoMp4 = videoMp4Match?.[1] || dataVideoMp4Match?.[1] || '';
const videoWebm = videoWebmMatch?.[1] || dataVideoWebmMatch?.[1] || '';
const videoBg = videoBgMatch?.[1] || dataVideoBgMatch?.[1] || '';
if (videoMp4 || videoWebm || videoBg) {
return { videoMp4, videoWebm, videoBg };
}
return null;
}
// Function to merge video attributes into contentHtml
function mergeVideoAttributes(contentHtml, videoAttrs) {
if (!contentHtml || !videoAttrs) return contentHtml;
let merged = contentHtml;
// Find the first vc-row element in contentHtml
const vcRowRegex = /<div class="vc-row[^"]*"[^>]*>/i;
const match = merged.match(vcRowRegex);
if (match) {
const existingDiv = match[0];
let newDiv = existingDiv;
// Add video attributes if they don't already exist
if (videoAttrs.videoMp4 && !existingDiv.includes('video_mp4=') && !existingDiv.includes('data-video-mp4=')) {
newDiv = newDiv.replace('>', ` video_mp4="${videoAttrs.videoMp4}">`);
}
if (videoAttrs.videoWebm && !existingDiv.includes('video_webm=') && !existingDiv.includes('data-video-webm=')) {
newDiv = newDiv.replace('>', ` video_webm="${videoAttrs.videoWebm}">`);
}
if (videoAttrs.videoBg && !existingDiv.includes('video_bg=') && !existingDiv.includes('data-video-bg=')) {
newDiv = newDiv.replace('>', ` video_bg="${videoAttrs.videoBg}">`);
}
// Also add data attributes for better compatibility
if (videoAttrs.videoMp4 && !existingDiv.includes('data-video-mp4=')) {
newDiv = newDiv.replace('>', ` data-video-mp4="${videoAttrs.videoMp4}">`);
}
if (videoAttrs.videoWebm && !existingDiv.includes('data-video-webm=')) {
newDiv = newDiv.replace('>', ` data-video-webm="${videoAttrs.videoWebm}">`);
}
if (videoAttrs.videoBg && !existingDiv.includes('data-video-bg=')) {
newDiv = newDiv.replace('>', ` data-video-bg="${videoAttrs.videoBg}">`);
}
merged = merged.replace(existingDiv, newDiv);
}
return merged;
}
// Main function
function main() {
console.log('🎬 Fixing video attributes in processed data...\n');
// Load pages.json
const pagesPath = path.join(PROCESSED_DIR, 'pages.json');
if (!fs.existsSync(pagesPath)) {
console.error('❌ pages.json not found');
process.exit(1);
}
const pages = JSON.parse(fs.readFileSync(pagesPath, 'utf8'));
let fixedCount = 0;
// Process each page
const updatedPages = pages.map(page => {
const videoAttrs = extractVideoAttributes(page.excerptHtml);
if (videoAttrs) {
console.log(`📄 Page: ${page.slug} (${page.locale})`);
console.log(` Found video attrs in excerpt: mp4="${videoAttrs.videoMp4}" webm="${videoAttrs.videoWebm}"`);
// Merge into contentHtml
const originalContent = page.contentHtml;
page.contentHtml = mergeVideoAttributes(page.contentHtml, videoAttrs);
if (page.contentHtml !== originalContent) {
console.log(` ✅ Merged into contentHtml`);
fixedCount++;
} else {
console.log(` ⚠️ Already present or no vc-row found`);
}
console.log('');
}
return page;
});
// Save updated pages
fs.writeFileSync(pagesPath, JSON.stringify(updatedPages, null, 2));
// Also update the main wordpress-data.json if it exists
const wordpressDataPath = path.join(PROCESSED_DIR, 'wordpress-data.json');
if (fs.existsSync(wordpressDataPath)) {
const wordpressData = JSON.parse(fs.readFileSync(wordpressDataPath, 'utf8'));
if (wordpressData.content && wordpressData.content.pages) {
wordpressData.content.pages = updatedPages;
fs.writeFileSync(wordpressDataPath, JSON.stringify(wordpressData, null, 2));
}
}
console.log(`✅ Fixed ${fixedCount} pages with video attributes`);
console.log('📁 Files updated:');
console.log(` ${pagesPath}`);
console.log(` ${wordpressDataPath}`);
}
if (require.main === module) {
main();
}