const fs = require('fs');
const serverPath = require("./serverPath.js")
const filePath = serverPath+'/blogs/index.html';
const blogs = require(serverPath+"/blogs/blogs.js")
function daysIntoYear(date){
return (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) - Date.UTC(date.getFullYear(), 0, 0)) / 24 / 60 / 60 / 1000;
}
function escapeQuotes(value) {
return value.replace(/["'&<>]/g, function (char) {
switch (char) {
case '"':
return """;
case "'":
return "'";
case "&":
return "&";
case "<":
return "<";
case ">":
return ">";
default:
return char;
}
});
}
console.log(blogs)
let linksText = "ALL POSTS
"
let dateObject
let month
let year
let oldMonth = "blibidy blob"
let oldYear = "bloopy aw"
for (let page = blogs.length - 1; page >= 0; page--) {
date = new Date(blogs[page][2])
day_of_year = daysIntoYear(date).toString(16)
year = date.getUTCFullYear().toString(16)
if (year != oldYear) {
linksText += (`${year}
`)
}
linksText += (`- ${day_of_year}: ${escapeQuotes(blogs[page][0])}
`)
oldYear = year
}
linksText += "
"
// Step 2: Read the HTML file
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
// Step 3: Identify the target div
const targetDivId = 'links';
// Step 4: Create the new content (e.g., a paragraph element)
const newContent = linksText
// Step 5: Replace the existing content in the target div
const regex = new RegExp(`([\\s\\S]*?)<\/div>`);
const updatedData = data.replace(regex, `
${newContent}
`);
// Step 6: Save the updated content back to the file
fs.writeFile(filePath, updatedData, 'utf8', (err) => {
if (err) {
console.error(err);
return;
}
console.log('Content replaced successfully in ', filePath);
});
});