From 8e3cf7a1c345f72734f3b3e5507937946328cb41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mark=20Randall=20Havens=20=E2=96=B3=20The=20Empathic=20Tec?= =?UTF-8?q?hnologist=20=E2=9F=81=20Doctor=20Who=2042?= Date: Thu, 16 Oct 2025 17:39:09 -0500 Subject: [PATCH] Update main.js --- main.js | 55 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/main.js b/main.js index 11d75b5..4faba91 100644 --- a/main.js +++ b/main.js @@ -1,43 +1,44 @@ -// main.js -// Client-side markdown renderer for "The Fold Within" +// main.js — dynamic markdown loader for The Fold Within -document.addEventListener('DOMContentLoaded', async () => { - const content = document.querySelector('main'); - const postsDir = 'posts/'; - - // Load Markdown renderer - const rendererScript = document.createElement('script'); - rendererScript.src = 'https://cdn.jsdelivr.net/npm/marked/marked.min.js'; - document.head.appendChild(rendererScript); +document.addEventListener("DOMContentLoaded", async () => { + const postsContainer = document.getElementById("posts"); + const main = document.querySelector("main"); - rendererScript.onload = () => { - // Attach click handlers to all articles - document.querySelectorAll('article').forEach(article => { - article.addEventListener('click', async () => { - const slug = article.querySelector('h3').textContent.trim() - .toLowerCase().replace(/\s+/g, '-'); - loadPost(slug); - }); - }); - }; + // Load post metadata from posts.json + const response = await fetch("posts/posts.json"); + const posts = await response.json(); - async function loadPost(slug) { + // Render index of posts + posts.forEach(post => { + const article = document.createElement("article"); + article.innerHTML = ` +
+

${post.title}

+

${post.date}

+

${post.excerpt}

+ `; + article.addEventListener("click", () => loadPost(post.file)); + postsContainer.appendChild(article); + }); + + // Load a markdown post dynamically + async function loadPost(filename) { try { - const res = await fetch(`${postsDir}${slug}.md`); - if (!res.ok) throw new Error('Post not found.'); + 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); - content.innerHTML = ` + main.innerHTML = `
- ← Back + ← Back to Archive
${html}
`; - document.getElementById('back').addEventListener('click', () => location.reload()); + document.getElementById("back").addEventListener("click", () => location.reload()); } catch (err) { - content.innerHTML = `

⚠️ ${err.message}

`; + main.innerHTML = `

⚠️ ${err.message}

`; } } }); \ No newline at end of file