const fs = require('fs'); const path = require('path'); const matter = require('gray-matter'); const postsDir = path.join(__dirname, '_posts'); const outputDir = path.join(__dirname, 'blog'); const GIT_URL = process.env.GIT_URL || 'https://git.kropa.tech/lotherk/deardiary'; const APP_URL = process.env.APP_URL || 'https://deardiary.hiddenbox.org'; // Ensure output directory exists if (!fs.existsSync(outputDir)) { fs.mkdirSync(outputDir, { recursive: true }); } // Get all markdown files const files = fs.readdirSync(postsDir).filter(f => f.endsWith('.md')); // Parse and generate HTML for each post const posts = files.map(file => { const content = fs.readFileSync(path.join(postsDir, file), 'utf-8'); const { data: frontmatter, content: markdown } = matter(content); // Simple markdown to HTML conversion let html = markdown // Headers .replace(/^### (.*$)/gim, '

$1

') .replace(/^## (.*$)/gim, '

$1

') .replace(/^# (.*$)/gim, '

$1

') // Bold .replace(/\*\*(.*?)\*\*/g, '$1') // Italic .replace(/\*(.*?)\*/g, '$1') // Code blocks .replace(/```(\w*)\n([\s\S]*?)```/g, '
$2
') // Inline code .replace(/`(.*?)`/g, '$1') // Links .replace(/\[(.*?)\]\((.*?)\)/g, '$1') // Images .replace(/!\[(.*?)\]\((.*?)\)/g, '$1') // Line breaks .replace(/\n\n/g, '

') // Lists .replace(/^\- (.*$)/gim, '

  • $1
  • ') .replace(/(
  • .*<\/li>)/s, ''); // Wrap in paragraph html = `

    ${html}

    `; // Clean up empty paragraphs html = html.replace(/

    <\/p>/g, ''); const slug = file.replace('.md', ''); const url = `/blog/${slug}/`; return { ...frontmatter, slug, url, html, date: frontmatter.date ? frontmatter.date.toISOString().split('T')[0] : '' }; }); // Sort posts by date (newest first) posts.sort((a, b) => new Date(b.date) - new Date(a.date)); const relativePath = '../'; // Generate index page const indexHtml = ` Blog - DearDiary

    Blog

    Updates, tutorials, and thoughts on AI-powered journaling.

    `; fs.writeFileSync(path.join(outputDir, 'index.html'), indexHtml); // Generate individual post pages posts.forEach(post => { const postDir = path.join(outputDir, post.slug); if (!fs.existsSync(postDir)) { fs.mkdirSync(postDir, { recursive: true }); } const postRelativePath = '../../'; const postHtml = ` ${post.title} - DearDiary Blog
    ← Back to Blog

    ${post.title}

    ${post.date}${post.author ? ` · ${post.author}` : ''}
    ${post.html}
    `; fs.writeFileSync(path.join(postDir, 'index.html'), postHtml); }); console.log(`Generated ${posts.length} blog posts.`);