2025-10-16 17:39:09 -05:00
|
|
|
// main.js — dynamic markdown loader for The Fold Within
|
2025-10-16 17:25:32 -05:00
|
|
|
|
2025-10-16 17:52:19 -05:00
|
|
|
console.log("Main.js loaded");
|
|
|
|
|
|
2025-10-16 17:39:09 -05:00
|
|
|
document.addEventListener("DOMContentLoaded", async () => {
|
|
|
|
|
const postsContainer = document.getElementById("posts");
|
|
|
|
|
const main = document.querySelector("main");
|
2025-10-16 17:25:32 -05:00
|
|
|
|
2025-10-16 17:39:09 -05:00
|
|
|
// Load post metadata from posts.json
|
|
|
|
|
const response = await fetch("posts/posts.json");
|
|
|
|
|
const posts = await response.json();
|
2025-10-16 17:25:32 -05:00
|
|
|
|
2025-10-16 17:39:09 -05:00
|
|
|
// Render index of posts
|
|
|
|
|
posts.forEach(post => {
|
|
|
|
|
const article = document.createElement("article");
|
|
|
|
|
article.innerHTML = `
|
|
|
|
|
<div class="thumb"></div>
|
|
|
|
|
<h3>${post.title}</h3>
|
|
|
|
|
<p class="date">${post.date}</p>
|
|
|
|
|
<p>${post.excerpt}</p>
|
|
|
|
|
`;
|
|
|
|
|
article.addEventListener("click", () => loadPost(post.file));
|
|
|
|
|
postsContainer.appendChild(article);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Load a markdown post dynamically
|
|
|
|
|
async function loadPost(filename) {
|
2025-10-16 17:25:32 -05:00
|
|
|
try {
|
2025-10-16 17:39:09 -05:00
|
|
|
const res = await fetch(`posts/${filename}`);
|
|
|
|
|
if (!res.ok) throw new Error("Post not found.");
|
2025-10-16 17:25:32 -05:00
|
|
|
const md = await res.text();
|
|
|
|
|
const html = marked.parse(md);
|
|
|
|
|
|
2025-10-16 17:39:09 -05:00
|
|
|
main.innerHTML = `
|
2025-10-16 17:25:32 -05:00
|
|
|
<section class="post">
|
2025-10-16 17:39:09 -05:00
|
|
|
<a href="#" id="back">← Back to Archive</a>
|
2025-10-16 17:25:32 -05:00
|
|
|
<div class="markdown">${html}</div>
|
|
|
|
|
</section>
|
|
|
|
|
`;
|
|
|
|
|
|
2025-10-16 17:39:09 -05:00
|
|
|
document.getElementById("back").addEventListener("click", () => location.reload());
|
2025-10-16 17:25:32 -05:00
|
|
|
} catch (err) {
|
2025-10-16 17:39:09 -05:00
|
|
|
main.innerHTML = `<p class="error">⚠️ ${err.message}</p>`;
|
2025-10-16 17:25:32 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|