Update main.js
This commit is contained in:
parent
ea6f05e78f
commit
8e3cf7a1c3
1 changed files with 28 additions and 27 deletions
53
main.js
53
main.js
|
|
@ -1,43 +1,44 @@
|
||||||
// main.js
|
// main.js — dynamic markdown loader for The Fold Within
|
||||||
// Client-side markdown renderer for "The Fold Within"
|
|
||||||
|
|
||||||
document.addEventListener('DOMContentLoaded', async () => {
|
document.addEventListener("DOMContentLoaded", async () => {
|
||||||
const content = document.querySelector('main');
|
const postsContainer = document.getElementById("posts");
|
||||||
const postsDir = 'posts/';
|
const main = document.querySelector("main");
|
||||||
|
|
||||||
// Load Markdown renderer
|
// Load post metadata from posts.json
|
||||||
const rendererScript = document.createElement('script');
|
const response = await fetch("posts/posts.json");
|
||||||
rendererScript.src = 'https://cdn.jsdelivr.net/npm/marked/marked.min.js';
|
const posts = await response.json();
|
||||||
document.head.appendChild(rendererScript);
|
|
||||||
|
|
||||||
rendererScript.onload = () => {
|
// Render index of posts
|
||||||
// Attach click handlers to all articles
|
posts.forEach(post => {
|
||||||
document.querySelectorAll('article').forEach(article => {
|
const article = document.createElement("article");
|
||||||
article.addEventListener('click', async () => {
|
article.innerHTML = `
|
||||||
const slug = article.querySelector('h3').textContent.trim()
|
<div class="thumb"></div>
|
||||||
.toLowerCase().replace(/\s+/g, '-');
|
<h3>${post.title}</h3>
|
||||||
loadPost(slug);
|
<p class="date">${post.date}</p>
|
||||||
});
|
<p>${post.excerpt}</p>
|
||||||
});
|
`;
|
||||||
};
|
article.addEventListener("click", () => loadPost(post.file));
|
||||||
|
postsContainer.appendChild(article);
|
||||||
|
});
|
||||||
|
|
||||||
async function loadPost(slug) {
|
// Load a markdown post dynamically
|
||||||
|
async function loadPost(filename) {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`${postsDir}${slug}.md`);
|
const res = await fetch(`posts/${filename}`);
|
||||||
if (!res.ok) throw new Error('Post not found.');
|
if (!res.ok) throw new Error("Post not found.");
|
||||||
const md = await res.text();
|
const md = await res.text();
|
||||||
const html = marked.parse(md);
|
const html = marked.parse(md);
|
||||||
|
|
||||||
content.innerHTML = `
|
main.innerHTML = `
|
||||||
<section class="post">
|
<section class="post">
|
||||||
<a href="#" id="back">← Back</a>
|
<a href="#" id="back">← Back to Archive</a>
|
||||||
<div class="markdown">${html}</div>
|
<div class="markdown">${html}</div>
|
||||||
</section>
|
</section>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
document.getElementById('back').addEventListener('click', () => location.reload());
|
document.getElementById("back").addEventListener("click", () => location.reload());
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
content.innerHTML = `<p class="error">⚠️ ${err.message}</p>`;
|
main.innerHTML = `<p class="error">⚠️ ${err.message}</p>`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue