const fs = require("fs"); const path = require("path"); const repoRoot = path.resolve(__dirname, ".."); const dataPath = path.join(repoRoot, "data", "meals.json"); const indexPath = path.join(repoRoot, "index.html"); const startMarker = ""; const endMarker = ""; function escapeHtml(value) { return value .replace(/&/g, "&") .replace(/", `\t\t\t\t\t`, `\t\t\t\t\t

${escapeHtml(entry.title)}

`, `\t\t\t\t\t

${escapeHtml(entry.description)}

`, "\t\t\t\t", ].join(eol); } function renderGallery(entries, eol) { return entries.map((entry) => renderArticle(entry, eol)).join(eol); } function main() { const entries = JSON.parse(fs.readFileSync(dataPath, "utf8")); const indexHtml = fs.readFileSync(indexPath, "utf8"); const eol = indexHtml.includes("\r\n") ? "\r\n" : "\n"; const galleryMarkup = renderGallery(entries, eol); const replacement = [ `\t\t\t\t${startMarker}`, galleryMarkup, `\t\t\t\t${endMarker}`, ].join(eol); const markerPattern = new RegExp( `^[\\t ]*${startMarker}[\\s\\S]*?^[\\t ]*${endMarker}`, "m" ); if (!markerPattern.test(indexHtml)) { throw new Error("Could not find gallery markers in index.html"); } fs.writeFileSync(indexPath, indexHtml.replace(markerPattern, replacement)); } main();