Update main.js

This commit is contained in:
Mark Randall Havens △ The Empathic Technologist ⟁ Doctor Who 42 2025-10-16 18:24:10 -05:00 committed by GitHub
parent 7bd519911c
commit 293bb57cc5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

104
main.js
View file

@ -1,46 +1,88 @@
// main.js — dynamic markdown loader for The Fold Within // main.js — client router + markdown renderer
// Routes:
// #/ -> index
// #/post/:id -> render that post
console.log("Main.js loaded"); const state = {
posts: [],
bySlug: new Map()
};
document.addEventListener("DOMContentLoaded", async () => { function $(sel) { return document.querySelector(sel); }
const postsContainer = document.getElementById("posts");
const main = document.querySelector("main");
// Load post metadata from posts.json window.addEventListener("hashchange", router);
const response = await fetch("posts/posts.json"); document.addEventListener("DOMContentLoaded", init);
const posts = await response.json();
// Render index of posts async function init() {
posts.forEach(post => { try {
const res = await fetch("posts/posts.json", { cache: "no-cache" });
if (!res.ok) throw new Error("Could not load posts index.");
state.posts = await res.json();
state.bySlug = new Map(state.posts.map(p => [p.slug, p]));
// render index initially, or route if hash present
router();
} catch (err) {
const container = $("#posts");
if (container) container.innerHTML = `<p class="error">⚠️ ${err.message}</p>`;
}
}
function router() {
const hash = location.hash.replace(/^#/, "");
const parts = hash.split("/").filter(Boolean);
if (parts[0] === "post" && parts[1]) {
renderPost(parts[1]);
} else {
renderIndex();
}
}
function renderIndex() {
const postsContainer = $("#posts");
if (!postsContainer) return;
postsContainer.innerHTML = "";
state.posts.forEach(post => {
const article = document.createElement("article"); const article = document.createElement("article");
article.innerHTML = ` article.innerHTML = `
<div class="thumb"></div> <div class="thumb"></div>
<h3>${post.title}</h3> <h3>${post.title}</h3>
<p class="date">${post.date}</p> <p class="date">${new Date(post.date).toLocaleDateString()}</p>
<p>${post.excerpt}</p> <p>${post.excerpt}</p>
`; `;
article.addEventListener("click", () => loadPost(post.file)); article.addEventListener("click", () => {
location.hash = `/post/${post.slug}`;
});
postsContainer.appendChild(article); postsContainer.appendChild(article);
}); });
}
// Load a markdown post dynamically async function renderPost(slug) {
async function loadPost(filename) { const main = document.querySelector("main");
try { const meta = state.bySlug.get(slug);
const res = await fetch(`posts/${filename}`);
if (!res.ok) throw new Error("Post not found.");
const md = await res.text();
const html = marked.parse(md);
main.innerHTML = ` if (!meta) {
<section class="post"> main.innerHTML = `<p class="error">⚠️ Post not found.</p>`;
<a href="#" id="back"> Back to Archive</a> return;
<div class="markdown">${html}</div>
</section>
`;
document.getElementById("back").addEventListener("click", () => location.reload());
} catch (err) {
main.innerHTML = `<p class="error">⚠️ ${err.message}</p>`;
}
} }
});
try {
const res = await fetch(`posts/${meta.file}`, { cache: "no-cache" });
if (!res.ok) throw new Error("Post file missing.");
const md = await res.text();
const html = marked.parse(md);
main.innerHTML = `
<section class="post">
<a href="#/" id="back"> Back to Archive</a>
<div class="markdown">${html}</div>
</section>
`;
$("#back").addEventListener("click", () => (location.hash = "/"));
} catch (err) {
main.innerHTML = `<p class="error">⚠️ ${err.message}</p>`;
}
}