/** * Generates changelog data file for the frontend. * Run with: node scripts/generate-changelog-data.js */ const fs = require('fs'); const path = require('path'); const CHANGELOG_DIR = path.join(__dirname, '..', 'changelog'); const OUTPUT_FILE = path.join(__dirname, '..', 'src', 'data', 'changelog-data.ts'); const LANGUAGES = ['en', 'cn', 'de', 'fr']; function readFile(filePath) { try { return fs.readFileSync(filePath, 'utf-8'); } catch { return null; } } function parseMdx(content) { const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/); if (!frontmatterMatch) { return { title: '', body: content }; } const body = frontmatterMatch[2].trim(); const frontmatter = frontmatterMatch[1]; const titleMatch = frontmatter.match(/title:\s*["']?([^"'\n]+)["']?/); const title = titleMatch ? titleMatch[1].trim() : ''; return { title, body }; } // Get all unique dates const dates = []; const files = fs.readdirSync(CHANGELOG_DIR); files.forEach(file => { const match = file.match(/^(\d{4}-\d{2}-\d{2})-(\w+)\.mdx$/); if (match) { const date = match[1]; const lang = match[2]; if (!dates.includes(date)) { dates.push(date); } } }); // Sort dates descending dates.sort((a, b) => new Date(b) - new Date(a)); // Generate data for each language const data = {}; LANGUAGES.forEach(lang => { data[lang] = dates.map(date => { const filePath = path.join(CHANGELOG_DIR, `${date}-${lang}.mdx`); const content = readFile(filePath); if (!content) { return null; } const { title, body } = parseMdx(content); return { date, title, lang, author: 'ZhenYi', body, }; }).filter(Boolean); }); // Generate TypeScript file const tsContent = `// Auto-generated from changelog/*.mdx files // Run: node scripts/generate-changelog-data.js export type ChangelogEntry = { date: string; title: string; lang: string; author: string; body: string; }; export const CHANGELOG_DATA: Record = ${JSON.stringify(data, null, 2)}; export const CHANGELOG_LANGUAGES = ${JSON.stringify(LANGUAGES)}; `; fs.writeFileSync(OUTPUT_FILE, tsContent); console.log(`Generated ${OUTPUT_FILE}`);