improve blogpost and disable cider

This commit is contained in:
Enstrayed
2024-06-12 17:43:42 -07:00
parent 7caaf7fdc2
commit 2e6b4db4fd
2 changed files with 104 additions and 77 deletions

View File

@@ -7,19 +7,33 @@ app.get("/blogposts", (rreq, rres) => {
if (Date.now() < timeSinceLastQuery+10000) { // if it has been <10 seconds since last request
rres.set("Access-Control-Allow-Origin","*")
rres.send(cachedResult) // send cached json
if (rreq.query.format === "html") { // if ?format=html then send HTML
rres.send(cachedResult.asHtml)
} else { // otherwise send json
rres.send(cachedResult.asJson)
}
} else {
timeSinceLastQuery = Date.now()
cachedResult = parseFiles()
rres.set("Access-Control-Allow-Origin","*")
rres.send(cachedResult);
if (rreq.query.format === "html") { // if ?format=html then send HTML
rres.send(cachedResult.asHtml)
} else { // otherwise send json
rres.send(cachedResult.asJson)
}
}
})
function parseFiles() {
let files = fs.readdirSync(globalConfig.blog.postsDirectory)
let parsedFiles = []
let result = {
asJson: [],
asHtml: ""
}
for (x in files) {
if (files[x].endsWith(".html") === false) { break } // If file/dir is not .html then ignore
@@ -29,12 +43,13 @@ function parseFiles() {
date = date.replace(/.{2}/g,"$&-").replace("-","").slice(0,-1) // Insert a dash every 2 characters, remove the first dash, remove the last character
let name = files[x].slice(9).replace(/÷/g," ").replace(".html","") // Strip Date, replace seperator with space & remove file extension
let name = files[x].slice(9).replace(/-/g," ").replace(".html","") // Strip Date, replace seperator with space & remove file extension
parsedFiles.push({ "date": date, "name": name, "path": `${globalConfig.blog.postsDirUrl}/${files[x]}`}) // Add metadata as JSON to array
result.asJson.unshift({ "date": date, "name": name, "path": `${globalConfig.blog.postsDirUrl}/${files[x]}`}) // Add to asJson array in the result
result.asHtml = `<span>${date} <a href="${globalConfig.blog.postsDirUrl}/${files[x]}">${name}</a></span>`+result.asHtml
}
return parsedFiles.reverse()
return result
}
module.exports = {app}