Skip to content

Commit

Permalink
Update index.js
Browse files Browse the repository at this point in the history
Added conditional logic for woking
on both development and production
Some formatting and cleanup
  • Loading branch information
NecRaul committed Dec 14, 2024
1 parent 198d2df commit fed153e
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions javascript/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,41 @@ document.addEventListener("DOMContentLoaded", function () {
function loadPosts() {
let postPairs = new Array();
const postList = document.getElementById("postList");
fetch("posts/")
fetch("/posts/")
.then((response) => response.text())
.then((data) => {
const fileNames = data
.match(/href="([^"]+\.html)"/g)
.map(match => match.replace(/href="|"/g, ''));
.map((match) => match.replace(/href="|"/g, ""));
return Promise.all(
fileNames.map((fileName) => {
return fetch(`/posts/${fileName}`)
let postUrl = "";
if (
window.location.hostname === "localhost" ||
window.location.hostname === "127.0.0.1"
) {
postUrl = `${fileName}`;
} else {
postUrl = `/posts/${fileName}`;
}
return fetch(postUrl)
.then((response) => response.text())
.then((postContent) => {
const doc = new DOMParser().parseFromString(
postContent,
"text/html"
"text/html",
);
const title = doc.title;
const date = doc.getElementById("date").textContent;
const listItem = document.createElement("li");
listItem.className = "postitem";
listItem.innerHTML = `<a href="/posts/${fileName}">${title}</a> - <em>${date}</em>`;
listItem.innerHTML = `<a href="${postUrl}">${title}</a> - <em>${date}</em>`;
postPairs.push([new Date(date), listItem]);
})
.catch((error) =>
console.error(`Error fetching post ${fileName}: ${error}`)
console.error(`Error fetching post ${postUrl}: ${error}`),
);
})
}),
);
})
.then(() => {
Expand Down

0 comments on commit fed153e

Please sign in to comment.