const fs = require("fs"); const path = require("path"); // Directory containing the content files const CONTENT_DIR = "./content"; // Fields to transfer const FIELDS = [ "Date", "Categories", "Affiliatelink", "Autopublish", "Autopublishdate", "Tags", ]; // Function to process a single German file function processFile(deFilePath) { const enFilePath = deFilePath.replace(".de.md", ".en.md"); let firstChange = true; if (!fs.existsSync(enFilePath)) { console.log(`Skipping ${deFilePath} as no corresponding EN file found.`); return; } console.log(`Processing ${deFilePath} and ${enFilePath}`); // Read file contents const deContent = fs.readFileSync(deFilePath, "utf-8"); const enContent = fs.readFileSync(enFilePath, "utf-8"); // Split content into blocks const deBlocks = deContent.split("----").map((block) => block.trim()); let updatedDeBlocks = []; let updatedEnContent = enContent; for (const block of deBlocks) { if (!block) continue; // Skip empty blocks let isFieldTransferred = false; for (const field of FIELDS) { const fieldRegex = new RegExp(`^${field}:\\s*(.+)$`, "m"); const match = block.match(fieldRegex); if (match) { const value = match[1]; console.log( `Extracted value for ${field} in ${deFilePath}: '${value}'`, ); // Update or append the field in the EN file const enFieldRegex = new RegExp(`^${field}:`, "m"); if (updatedEnContent.match(enFieldRegex)) { updatedEnContent = updatedEnContent.replace( enFieldRegex, `${field}: ${value}`, ); } else { if (firstChange) { updatedEnContent += `\n\n----\n\n${field}: ${value}\n`; firstChange = false; } else { updatedEnContent += `\n----\n\n${field}: ${value}\n`; } } isFieldTransferred = true; } } // Keep blocks that do not contain transferred fields if (!isFieldTransferred) { updatedDeBlocks.push(block); } } // Remove empty blocks and reassemble the German file const newDeContent = updatedDeBlocks.filter(Boolean).join("\n\n----\n\n"); // Write the updated content back to the files fs.writeFileSync(deFilePath, newDeContent.trim() + "\n"); fs.writeFileSync(enFilePath, updatedEnContent.trim() + "\n"); console.log(`Updated ${deFilePath} and ${enFilePath}`); } // Recursive function to find and process all `.de.md` files function processDirectory(directory) { console.log("-------- Process:", directory); const entries = fs.readdirSync(directory, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(directory, entry.name); if (entry.isDirectory()) { processDirectory(fullPath); } else if (entry.isFile() && fullPath.endsWith(".de.md")) { processFile(fullPath); } } } // Start processing processDirectory(CONTENT_DIR); console.log("Field transfer and cleanup complete.");