|
| 1 | +// Parse YAML frontmatter from markdown |
| 2 | +function parseFrontmatter(content) { |
| 3 | + const frontmatterRegex = /^---\n([\s\S]*?)\n---/; |
| 4 | + const match = content.match(frontmatterRegex); |
| 5 | + |
| 6 | + if (!match) { |
| 7 | + return { metadata: {}, content: content }; |
| 8 | + } |
| 9 | + |
| 10 | + const frontmatterStr = match[1]; |
| 11 | + const metadata = {}; |
| 12 | + |
| 13 | + // Simple YAML parser |
| 14 | + frontmatterStr.split('\n').forEach(line => { |
| 15 | + const [key, ...valueParts] = line.split(':'); |
| 16 | + if (key && valueParts.length > 0) { |
| 17 | + let value = valueParts.join(':').trim(); |
| 18 | + value = value.replace(/^["']|["']$/g, ''); |
| 19 | + metadata[key.trim()] = value; |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + const bodyContent = content.replace(frontmatterRegex, '').trim(); |
| 24 | + return { metadata, content: bodyContent }; |
| 25 | +} |
| 26 | + |
| 27 | +// Get the post filename from URL |
| 28 | +function getPostFilename() { |
| 29 | + const path = window.location.pathname; |
| 30 | + const parts = path.split('/'); |
| 31 | + // Should be something like /aigc/posts/post-name/ |
| 32 | + for (let i = 0; i < parts.length; i++) { |
| 33 | + if (parts[i] === 'posts' && i + 1 < parts.length) { |
| 34 | + return parts[i + 1]; |
| 35 | + } |
| 36 | + } |
| 37 | + return null; |
| 38 | +} |
| 39 | + |
| 40 | +// Format date |
| 41 | +function formatDate(dateStr) { |
| 42 | + try { |
| 43 | + const [year, month, day] = dateStr.split('-'); |
| 44 | + const date = new Date(year, month - 1, day); |
| 45 | + return date.toLocaleDateString('en-US', { day: 'numeric', month: 'short', year: 'numeric' }); |
| 46 | + } catch (e) { |
| 47 | + return dateStr; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// Load and render the post |
| 52 | +async function loadPost() { |
| 53 | + const filename = getPostFilename(); |
| 54 | + if (!filename) { |
| 55 | + document.getElementById('post-container').innerHTML = '<p>Post not found</p>'; |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + console.log('Loading post:', filename); |
| 60 | + |
| 61 | + try { |
| 62 | + // Fetch the markdown file from the posts directory (one level up from current post dir) |
| 63 | + const response = await fetch(`/aigc/posts/${filename}.md`); |
| 64 | + |
| 65 | + if (!response.ok) { |
| 66 | + console.error('Failed to fetch markdown:', response.status, response.statusText); |
| 67 | + document.getElementById('post-container').innerHTML = `<p>Could not load post (${response.status})</p>`; |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const markdownContent = await response.text(); |
| 72 | + console.log('Markdown loaded, length:', markdownContent.length); |
| 73 | + |
| 74 | + const { metadata, content } = parseFrontmatter(markdownContent); |
| 75 | + console.log('Metadata:', metadata); |
| 76 | + |
| 77 | + // Update page title |
| 78 | + if (metadata.title) { |
| 79 | + document.title = `ritchie@singapore~$ ${metadata.title}`; |
| 80 | + } |
| 81 | + |
| 82 | + // Convert markdown to HTML - ensure marked is available |
| 83 | + let htmlContent; |
| 84 | + console.log('marked available:', typeof marked !== 'undefined'); |
| 85 | + |
| 86 | + if (typeof marked !== 'undefined' && marked.parse) { |
| 87 | + try { |
| 88 | + htmlContent = marked.parse(content); |
| 89 | + console.log('Markdown parsed successfully, HTML length:', htmlContent.length); |
| 90 | + } catch (e) { |
| 91 | + console.error('Error parsing markdown:', e); |
| 92 | + htmlContent = `<pre>${content}</pre>`; |
| 93 | + } |
| 94 | + } else { |
| 95 | + console.warn('marked.js not loaded, showing raw markdown'); |
| 96 | + htmlContent = `<pre>${content}</pre>`; |
| 97 | + } |
| 98 | + |
| 99 | + // Build the post header |
| 100 | + const headerHTML = ` |
| 101 | + <div class="post-header"> |
| 102 | + <h1 class="post-title">${metadata.title || 'Untitled'}</h1> |
| 103 | + <div class="post-meta"> |
| 104 | + <div class="post-meta-item"> |
| 105 | + <span class="post-meta-label">Published:</span> ${formatDate(metadata.date || 'Unknown')} |
| 106 | + </div> |
| 107 | + <div class="post-meta-item"> |
| 108 | + <span class="post-meta-label">Category:</span> <span style="color: #00ff88;">${metadata.category || 'AIGC'}</span> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + `; |
| 113 | + |
| 114 | + // Insert the header and content |
| 115 | + const container = document.getElementById('post-container'); |
| 116 | + container.innerHTML = headerHTML + htmlContent; |
| 117 | + |
| 118 | + // Re-render MathJax if it's loaded |
| 119 | + if (typeof MathJax !== 'undefined' && MathJax.typesetPromise) { |
| 120 | + MathJax.typesetPromise([container]).catch(err => console.log('MathJax error:', err)); |
| 121 | + } |
| 122 | + |
| 123 | + } catch (error) { |
| 124 | + console.error('Error loading post:', error); |
| 125 | + document.getElementById('post-container').innerHTML = `<p>Error loading post: ${error.message}</p>`; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +document.addEventListener('DOMContentLoaded', loadPost); |
0 commit comments