|
| 1 | +import { readFileSync, writeFileSync } from 'fs' |
| 2 | +import { fileURLToPath } from 'url' |
| 3 | +import { dirname, join } from 'path' |
| 4 | + |
| 5 | +const __filename = fileURLToPath(import.meta.url) |
| 6 | +const __dirname = dirname(__filename) |
| 7 | +const rootDir = join(__dirname, '..') |
| 8 | + |
| 9 | +const changelogPath = join(rootDir, 'CHANGELOG.md') |
| 10 | +const outputPath = join(rootDir, 'public', 'changelog.json') |
| 11 | + |
| 12 | +const markdown = readFileSync(changelogPath, 'utf-8') |
| 13 | + |
| 14 | +const entries = [] |
| 15 | +const versionRegex = /^## \[([^\]]+)\](?: - (\d{4}-\d{2}-\d{2}))?/ |
| 16 | +const sectionRegex = /^### (.+)/ |
| 17 | +const itemRegex = /^- (.+)/ |
| 18 | + |
| 19 | +let currentEntry = null |
| 20 | +let currentSection = null |
| 21 | + |
| 22 | +for (const line of markdown.split('\n')) { |
| 23 | + const versionMatch = line.match(versionRegex) |
| 24 | + if (versionMatch) { |
| 25 | + if (currentEntry) { |
| 26 | + entries.push(currentEntry) |
| 27 | + } |
| 28 | + currentEntry = { |
| 29 | + version: versionMatch[1], |
| 30 | + date: versionMatch[2] || '', |
| 31 | + sections: [] |
| 32 | + } |
| 33 | + currentSection = null |
| 34 | + continue |
| 35 | + } |
| 36 | + |
| 37 | + if (!currentEntry) continue |
| 38 | + |
| 39 | + const sectionMatch = line.match(sectionRegex) |
| 40 | + if (sectionMatch) { |
| 41 | + currentSection = { |
| 42 | + title: sectionMatch[1], |
| 43 | + items: [] |
| 44 | + } |
| 45 | + currentEntry.sections.push(currentSection) |
| 46 | + continue |
| 47 | + } |
| 48 | + |
| 49 | + const itemMatch = line.match(itemRegex) |
| 50 | + if (itemMatch && currentSection) { |
| 51 | + currentSection.items.push(itemMatch[1]) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +if (currentEntry) { |
| 56 | + entries.push(currentEntry) |
| 57 | +} |
| 58 | + |
| 59 | +writeFileSync(outputPath, JSON.stringify(entries, null, 2)) |
| 60 | +console.log(`Generated ${outputPath} with ${entries.length} entries`) |
0 commit comments