146 lines
6.3 KiB
JavaScript
146 lines
6.3 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const filePath = path.join(__dirname, 'process-data-with-bg-images.js');
|
|
let content = fs.readFileSync(filePath, 'utf8');
|
|
|
|
// 1. Update processPages to async
|
|
content = content.replace(
|
|
'function processPages(pagesEN, pagesDE, translationMapping, mediaMapping, assetMap) {',
|
|
'async function processPages(pagesEN, pagesDE, translationMapping, mediaMapping, assetMap) {'
|
|
);
|
|
|
|
// 2. Update processPosts to async
|
|
content = content.replace(
|
|
'function processPosts(postsEN, postsDE, translationMapping, mediaMapping, assetMap) {',
|
|
'async function processPosts(postsEN, postsDE, translationMapping, mediaMapping, assetMap) {'
|
|
);
|
|
|
|
// 3. Update main to async
|
|
content = content.replace(
|
|
'function main() {',
|
|
'async function main() {'
|
|
);
|
|
|
|
// 4. Update main() call
|
|
content = content.replace(
|
|
'if (require.main === module) {\n main();\n}',
|
|
'if (require.main === module) {\n main().catch(console.error);\n}'
|
|
);
|
|
|
|
// 5. Update processPages English loop
|
|
content = content.replace(
|
|
'pagesEN.forEach(page => {',
|
|
'for (const page of pagesEN) {'
|
|
);
|
|
|
|
// 6. Update processPages German loop
|
|
content = content.replace(
|
|
'pagesDE.forEach(page => {',
|
|
'for (const page of pagesDE) {'
|
|
);
|
|
|
|
// 7. Add video processing in processPages English
|
|
content = content.replace(
|
|
'contentHtml = replaceUrlsWithLocalPaths(contentHtml, assetMap);\n \n let excerptHtml = decodeContent(page.excerptHtml);',
|
|
'contentHtml = replaceUrlsWithLocalPaths(contentHtml, assetMap);\n \n // Process video attributes and download videos\n const videoResult = await processVideoAttributes(contentHtml);\n contentHtml = videoResult.html;\n \n let excerptHtml = decodeContent(page.excerptHtml);'
|
|
);
|
|
|
|
// 8. Add video processing in processPages German
|
|
const germanPattern = /contentHtml = replaceUrlsWithLocalPaths\(contentHtml, assetMap\);\n \n let excerptHtml = decodeContent\(page\.excerptHtml\);\n excerptHtml = replaceBgImageIds\(excerptHtml, mediaMapping\);\n excerptHtml = replaceUrlsWithLocalPaths\(excerptHtml, assetMap\);\n \n processed\.push\(\{/;
|
|
content = content.replace(
|
|
germanPattern,
|
|
`contentHtml = replaceUrlsWithLocalPaths(contentHtml, assetMap);
|
|
|
|
// Process video attributes and download videos
|
|
const videoResult = await processVideoAttributes(contentHtml);
|
|
contentHtml = videoResult.html;
|
|
|
|
let excerptHtml = decodeContent(page.excerptHtml);
|
|
excerptHtml = replaceBgImageIds(excerptHtml, mediaMapping);
|
|
excerptHtml = replaceUrlsWithLocalPaths(excerptHtml, assetMap);
|
|
|
|
processed.push({`
|
|
);
|
|
|
|
// 9. Update processPosts English loop
|
|
content = content.replace(
|
|
'postsEN.forEach(post => {',
|
|
'for (const post of postsEN) {'
|
|
);
|
|
|
|
// 10. Update processPosts German loop
|
|
content = content.replace(
|
|
'postsDE.forEach(post => {',
|
|
'for (const post of postsDE) {'
|
|
);
|
|
|
|
// 11. Add video processing in processPosts English
|
|
const postsEnglishPattern = /contentHtml = replaceUrlsWithLocalPaths\(contentHtml, assetMap\);\n \n let excerptHtml = decodeContent\(post\.excerptHtml\);\n excerptHtml = replaceBgImageIds\(excerptHtml, mediaMapping\);\n excerptHtml = replaceUrlsWithLocalPaths\(excerptHtml, assetMap\);\n \n processed\.push\(\{/;
|
|
content = content.replace(
|
|
postsEnglishPattern,
|
|
`contentHtml = replaceUrlsWithLocalPaths(contentHtml, assetMap);
|
|
|
|
// Process video attributes and download videos
|
|
const videoResult = await processVideoAttributes(contentHtml);
|
|
contentHtml = videoResult.html;
|
|
|
|
let excerptHtml = decodeContent(post.excerptHtml);
|
|
excerptHtml = replaceBgImageIds(excerptHtml, mediaMapping);
|
|
excerptHtml = replaceUrlsWithLocalPaths(excerptHtml, assetMap);
|
|
|
|
processed.push({`
|
|
);
|
|
|
|
// 12. Add video processing in processPosts German
|
|
const postsGermanPattern = /contentHtml = replaceUrlsWithLocalPaths\(contentHtml, assetMap\);\n \n let excerptHtml = decodeContent\(post\.excerptHtml\);\n excerptHtml = replaceBgImageIds\(excerptHtml, mediaMapping\);\n excerptHtml = replaceUrlsWithLocalPaths\(excerptHtml, assetMap\);\n \n processed\.push\(\{[\s\S]*?translation: enMatch \? \{ locale: 'en', id: enMatch\.en \} : null\n \}\);\n \}\n \n return processed;\n\}/;
|
|
content = content.replace(
|
|
postsGermanPattern,
|
|
`contentHtml = replaceUrlsWithLocalPaths(contentHtml, assetMap);
|
|
|
|
// Process video attributes and download videos
|
|
const videoResult = await processVideoAttributes(contentHtml);
|
|
contentHtml = videoResult.html;
|
|
|
|
let excerptHtml = decodeContent(post.excerptHtml);
|
|
excerptHtml = replaceBgImageIds(excerptHtml, mediaMapping);
|
|
excerptHtml = replaceUrlsWithLocalPaths(excerptHtml, assetMap);
|
|
|
|
processed.push({
|
|
id: post.id,
|
|
translationKey: translationKey,
|
|
locale: 'de',
|
|
slug: post.slug,
|
|
path: \`/de/blog/\${post.slug}\`,
|
|
title: post.titleHtml.replace(/<[^>]*>/g, ''),
|
|
titleHtml: post.titleHtml,
|
|
contentHtml: sanitizeHTML(contentHtml),
|
|
excerptHtml: processExcerptShortcodes(excerptHtml) || generateExcerpt(contentHtml),
|
|
featuredImage: post.featuredImage,
|
|
datePublished: post.datePublished,
|
|
updatedAt: post.updatedAt,
|
|
translation: enMatch ? { locale: 'en', id: enMatch.en } : null
|
|
});
|
|
}
|
|
|
|
return processed;
|
|
}`
|
|
);
|
|
|
|
// 13. Update main() to await processPages and processPosts
|
|
content = content.replace(
|
|
'const pages = processPages(pagesEN, pagesDE, translationMapping, mediaMapping, assetMap);\n const posts = processPosts(postsEN, postsDE, translationMapping, mediaMapping, assetMap);',
|
|
'const pages = await processPages(pagesEN, pagesDE, translationMapping, mediaMapping, assetMap);\n const posts = await processPosts(postsEN, postsDE, translationMapping, mediaMapping, assetMap);'
|
|
);
|
|
|
|
// 14. Update module.exports
|
|
content = content.replace(
|
|
'module.exports = {\n processPages,\n processPosts,\n processProducts,\n processProductCategories,\n processMedia,\n generateAssetMap,\n replaceBgImageIds,\n replaceUrlsWithLocalPaths\n};',
|
|
'module.exports = {\n processPages,\n processPosts,\n processProducts,\n processProductCategories,\n processMedia,\n generateAssetMap,\n replaceBgImageIds,\n replaceUrlsWithLocalPaths,\n processVideoAttributes\n};'
|
|
);
|
|
|
|
fs.writeFileSync(filePath, content);
|
|
console.log('✅ Updated process-data-with-bg-images.js to be async');
|