${post.title}
${post.summary}
Continue reading →document.addEventListener('DOMContentLoaded', async function () { const contentWrapper = document.querySelector('.blog-feed'); // Correct target for the posts container const tagCloud = document.querySelector('.tag-cloud'); const recentPostsSection = document.querySelector('.recent-posts ul'); const posts = []; let currentPostCount = 3; // Initial number of posts to display let recentPostsCount = 0; // To track how many recent posts have been loaded // Function to load posts data from Markdown files async function loadPosts() { const markdownFiles = await getMarkdownFiles(); for (const file of markdownFiles) { const post = await fetchPost(file); if (post) { posts.push(post); } } updateTagCloud(posts); displayPosts(); } // Function to get all Markdown files by checking their existence async function getMarkdownFiles() { const files = []; let index = 1; let fileExists = true; while (fileExists) { const fileName = `post${index}.md`; const response = await fetch(`/posts/markdown/${fileName}`); if (response.ok) { files.push(fileName); } else { fileExists = false; } index++; } return files; } // Function to fetch a single post and parse its content async function fetchPost(file) { const response = await fetch(`/posts/markdown/${file}`); if (!response.ok) { console.error(`Failed to fetch ${file}: ${response.status} ${response.statusText}`); return null; } const content = await response.text(); const post = parseMarkdown(content); // Construct link to the HTML file post.link = `/posts/html/${file.replace('.md', '.html')}`; return post; } // Function to parse Markdown content and extract front matter and content function parseMarkdown(content) { const lines = content.split('\n'); const frontMatter = {}; let contentStartIndex = -1; for (let i = 0; i < lines.length; i++) { const line = lines[i].trim(); if (line.startsWith('### content:')) { contentStartIndex = i + 1; break; } const match = line.match(/^### (\w+): (.+)$/); if (match) { const key = match[1]; const value = match[2]; frontMatter[key] = value.split(',').map(item => item.trim()); } } if (contentStartIndex === -1) { console.error('Invalid front matter'); return null; } const contentLines = lines.slice(contentStartIndex); const contentText = contentLines.join('\n').trim(); return { title: frontMatter.title || 'Untitled Post', date: frontMatter.date || 'Unknown Date', tags: frontMatter.tags || [], summary: frontMatter.summary || contentText.split('\n')[0], content: contentText }; } // Function to update tag cloud function updateTagCloud(postsData) { const tagSet = new Set(); postsData.forEach(post => post.tags.forEach(tag => tagSet.add(tag))); tagCloud.innerHTML = ''; tagSet.forEach(tag => { const tagElement = document.createElement('span'); tagElement.className = 'tag'; const tagLink = tag.toLowerCase().replace(/\s+/g, ''); tagElement.innerHTML = `${tag}`; tagElement.style.fontSize = `14px`; tagCloud.appendChild(tagElement); }); } // Function to display posts based on currentPostCount function displayPosts() { contentWrapper.innerHTML = ''; // Clear previous posts posts.slice(0, currentPostCount).forEach(post => { const newPost = document.createElement('section'); newPost.className = 'blog-post-wrapper'; newPost.innerHTML = `
${post.summary}
Continue reading →