parseMarkdown 解析 Markdown 内容为 ProjectMemory
(projectID, content string)
| 179 | |
| 180 | // parseMarkdown 解析 Markdown 内容为 ProjectMemory |
| 181 | func (s *FileStore) parseMarkdown(projectID, content string) (*ProjectMemory, error) { |
| 182 | memory := NewProjectMemory(projectID, "", "") |
| 183 | |
| 184 | lines := strings.Split(content, "\n") |
| 185 | var currentSection MemorySection |
| 186 | var inSection bool |
| 187 | |
| 188 | for _, line := range lines { |
| 189 | line = strings.TrimSpace(line) |
| 190 | |
| 191 | // 检测章节标题 |
| 192 | if strings.HasPrefix(line, "## 用户偏好") { |
| 193 | currentSection = SectionPreferences |
| 194 | inSection = true |
| 195 | continue |
| 196 | } else if strings.HasPrefix(line, "## 关键选择") { |
| 197 | currentSection = SectionChoices |
| 198 | inSection = true |
| 199 | continue |
| 200 | } else if strings.HasPrefix(line, "## 工作流状态") { |
| 201 | currentSection = SectionWorkflow |
| 202 | inSection = true |
| 203 | continue |
| 204 | } else if strings.HasPrefix(line, "## 生成参数") { |
| 205 | currentSection = SectionParams |
| 206 | inSection = true |
| 207 | continue |
| 208 | } else if strings.HasPrefix(line, "## 项目历史") { |
| 209 | currentSection = SectionHistory |
| 210 | inSection = true |
| 211 | continue |
| 212 | } else if strings.HasPrefix(line, "## ") { |
| 213 | inSection = false |
| 214 | continue |
| 215 | } |
| 216 | |
| 217 | // 解析条目(以 - 开头的行) |
| 218 | if inSection && strings.HasPrefix(line, "- ") { |
| 219 | entryContent := strings.TrimPrefix(line, "- ") |
| 220 | if entryContent != "" && !strings.Contains(entryContent, "*暂无记录*") { |
| 221 | entry := &Entry{ |
| 222 | ID: fmt.Sprintf("entry-%d", time.Now().UnixNano()), |
| 223 | Content: entryContent, |
| 224 | Source: "file", |
| 225 | CreatedAt: time.Now(), |
| 226 | } |
| 227 | |
| 228 | // 尝试提取分类(格式: [category] content) |
| 229 | if strings.HasPrefix(entryContent, "[") { |
| 230 | if idx := strings.Index(entryContent, "]"); idx > 0 { |
| 231 | entry.Category = entryContent[1:idx] |
| 232 | entry.Content = strings.TrimSpace(entryContent[idx+1:]) |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | memory.AddEntry(currentSection, entry) |
| 237 | } |
| 238 | } |
no test coverage detected