document.addEventListener('DOMContentLoaded', () => { const themeToggle = document.getElementById('themeToggle'); const html = document.documentElement; // Icons const moonIcon = ''; const sunIcon = ''; // Load saved theme const savedTheme = localStorage.getItem('theme') || 'light'; setTheme(savedTheme); themeToggle.addEventListener('click', () => { const currentTheme = html.getAttribute('data-theme'); const newTheme = currentTheme === 'light' ? 'dark' : 'light'; setTheme(newTheme); }); function setTheme(theme) { html.setAttribute('data-theme', theme); localStorage.setItem('theme', theme); themeToggle.innerHTML = theme === 'light' ? moonIcon : sunIcon; } // Load Blog Posts loadPosts(); async function loadPosts() { const postGrid = document.getElementById('postGrid'); try { const response = await fetch('posts.json'); const posts = await response.json(); // Clear loading state if any postGrid.innerHTML = ''; posts.forEach((post, index) => { const article = document.createElement('article'); article.className = 'post-card'; article.style.animationDelay = `${(index + 1) * 0.1}s`; article.innerHTML = `
${post.excerpt}
Read Article → `; postGrid.appendChild(article); }); } catch (error) { console.error('Error loading posts:', error); postGrid.innerHTML = 'Failed to load posts.
'; } } });