|
| 1 | +// Parse CHANGELOG.zh-CN.md structure |
| 2 | +// Format: |
| 3 | +// ## [version] - date |
| 4 | +// ### Update Title |
| 5 | +// Description |
| 6 | +// #### Section Name |
| 7 | +// - item |
| 8 | +// - item |
| 9 | + |
| 10 | +export interface ChangelogItem { |
| 11 | + text: string |
| 12 | +} |
| 13 | + |
| 14 | +export interface ChangelogSection { |
| 15 | + name: string // e.g., "新增功能", "改进优化" |
| 16 | + items: ChangelogItem[] |
| 17 | +} |
| 18 | + |
| 19 | +export interface ProductUpdate { |
| 20 | + version: string |
| 21 | + date: string |
| 22 | + releasedAt: number // Unix timestamp |
| 23 | + title: string // e.g., "九键输入法、语音输入法正式上线" |
| 24 | + description: string |
| 25 | + sections: ChangelogSection[] |
| 26 | +} |
| 27 | + |
| 28 | +export function parseChangelog(markdown: string): ProductUpdate[] { |
| 29 | + const lines = markdown.split('\n') |
| 30 | + const updates: ProductUpdate[] = [] |
| 31 | + let currentUpdate: Partial<ProductUpdate> | null = null |
| 32 | + let currentSection: Partial<ChangelogSection> | null = null |
| 33 | + let expectingTitle = false |
| 34 | + let expectingDescription = false |
| 35 | + |
| 36 | + for (let i = 0; i < lines.length; i++) { |
| 37 | + const line = lines[i].trim() |
| 38 | + |
| 39 | + // Skip empty lines and main title |
| 40 | + if (!line || line.startsWith('# 更新日志') || line.startsWith('本文档记录')) { |
| 41 | + continue |
| 42 | + } |
| 43 | + |
| 44 | + // Match version line: ## [2.3.1] - 2025-10-27 |
| 45 | + const versionMatch = line.match(/^##\s+\[([^\]]+)\]\s+-\s+(.+)/) |
| 46 | + if (versionMatch) { |
| 47 | + // Save previous update if exists |
| 48 | + if (currentUpdate && currentUpdate.version) { |
| 49 | + if (currentSection && currentSection.name) { |
| 50 | + if (!currentUpdate.sections) currentUpdate.sections = [] |
| 51 | + currentUpdate.sections.push(currentSection as ChangelogSection) |
| 52 | + } |
| 53 | + updates.push(currentUpdate as ProductUpdate) |
| 54 | + } |
| 55 | + |
| 56 | + // Start new update |
| 57 | + const [, version, dateStr] = versionMatch |
| 58 | + currentUpdate = { |
| 59 | + version, |
| 60 | + date: dateStr, |
| 61 | + releasedAt: Date.parse(dateStr), |
| 62 | + title: '', |
| 63 | + description: '', |
| 64 | + sections: [] |
| 65 | + } |
| 66 | + currentSection = null |
| 67 | + expectingTitle = true |
| 68 | + expectingDescription = false |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + // Match title: ### Update Title |
| 73 | + const titleMatch = line.match(/^###\s+(.+)/) |
| 74 | + if (titleMatch && expectingTitle) { |
| 75 | + currentUpdate!.title = titleMatch[1] |
| 76 | + expectingTitle = false |
| 77 | + expectingDescription = true |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + // Match section: #### 新增功能 |
| 82 | + const sectionMatch = line.match(/^####\s+(.+)/) |
| 83 | + if (sectionMatch) { |
| 84 | + // Save previous section |
| 85 | + if (currentSection && currentSection.name) { |
| 86 | + currentUpdate!.sections!.push(currentSection as ChangelogSection) |
| 87 | + } |
| 88 | + |
| 89 | + // Start new section |
| 90 | + currentSection = { |
| 91 | + name: sectionMatch[1], |
| 92 | + items: [] |
| 93 | + } |
| 94 | + expectingDescription = false |
| 95 | + continue |
| 96 | + } |
| 97 | + |
| 98 | + // Match item: - 九宫格拼音键盘 |
| 99 | + const itemMatch = line.match(/^-\s+(.+)/) |
| 100 | + if (itemMatch && currentSection) { |
| 101 | + currentSection.items!.push({ text: itemMatch[1] }) |
| 102 | + continue |
| 103 | + } |
| 104 | + |
| 105 | + // Description line (plain text after title) |
| 106 | + if (expectingDescription && line && !line.startsWith('#')) { |
| 107 | + currentUpdate!.description = line |
| 108 | + expectingDescription = false |
| 109 | + continue |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Save last update |
| 114 | + if (currentUpdate && currentUpdate.version) { |
| 115 | + if (currentSection && currentSection.name) { |
| 116 | + currentUpdate.sections!.push(currentSection as ChangelogSection) |
| 117 | + } |
| 118 | + updates.push(currentUpdate as ProductUpdate) |
| 119 | + } |
| 120 | + |
| 121 | + return updates |
| 122 | +} |
| 123 | + |
| 124 | +// Storage key for last viewed timestamp |
| 125 | +export const LAST_VIEWED_KEY = 'vrime_changelog_last_viewed' |
| 126 | + |
| 127 | +// Get last viewed timestamp from localStorage |
| 128 | +export function getLastViewedTimestamp(): number { |
| 129 | + const stored = localStorage.getItem(LAST_VIEWED_KEY) |
| 130 | + return stored ? parseInt(stored, 10) : 0 |
| 131 | +} |
| 132 | + |
| 133 | +// Update last viewed timestamp |
| 134 | +export function markChangelogAsViewed(): void { |
| 135 | + localStorage.setItem(LAST_VIEWED_KEY, Date.now().toString()) |
| 136 | +} |
| 137 | + |
| 138 | +// Get updates released after a certain timestamp |
| 139 | +export function getUnseenUpdates(updates: ProductUpdate[]): ProductUpdate[] { |
| 140 | + const lastViewed = getLastViewedTimestamp() |
| 141 | + return updates.filter(update => update.releasedAt > lastViewed) |
| 142 | +} |
| 143 | + |
| 144 | +// Get count of unseen updates |
| 145 | +export function getUnseenCount(updates: ProductUpdate[]): number { |
| 146 | + return getUnseenUpdates(updates).length |
| 147 | +} |
| 148 | + |
| 149 | +// Check if update is new (released after last viewed) |
| 150 | +export function isNewUpdate(update: ProductUpdate): boolean { |
| 151 | + return update.releasedAt > getLastViewedTimestamp() |
| 152 | +} |
0 commit comments