Repository navigation
Expand file tree
/
Copy pathmarkdown.rs
More file actions
executable file
·170 lines (154 loc) · 5.92 KB
/
Copy pathmarkdown.rs
File metadata and controls
executable file
·170 lines (154 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::types::{MemoryInput, MemoryKind};
pub fn parse_markdown(content: &str) -> Vec<MemoryInput> {
let mut memories = Vec::new();
let mut current_section: Option<String> = None;
let mut current_content = String::new();
let mut in_code_block = false;
for line in content.lines() {
// Track code blocks
if line.trim_start().starts_with("```") {
in_code_block = !in_code_block;
continue;
}
// Skip code blocks
if in_code_block {
continue;
}
// Detect headers as section names
if let Some(stripped) = line.strip_prefix("# ") {
// Save previous section
if let Some(section) = current_section.take() {
let content = current_content.trim().to_string();
if !content.is_empty() && content.len() > 10 {
memories.push(MemoryInput {
kind: guess_kind(§ion, &content),
key: section,
content,
tags: vec!["imported".to_string(), "markdown".to_string()],
related_ids: Vec::new(),
});
}
}
current_section = Some(stripped.trim().to_string());
current_content.clear();
} else if let Some(stripped) = line.strip_prefix("## ") {
// Save previous section
if let Some(section) = current_section.take() {
let content = current_content.trim().to_string();
if !content.is_empty() && content.len() > 10 {
memories.push(MemoryInput {
kind: guess_kind(§ion, &content),
key: section,
content,
tags: vec!["imported".to_string(), "markdown".to_string()],
related_ids: Vec::new(),
});
}
}
current_section = Some(stripped.trim().to_string());
current_content.clear();
} else if !line.trim().is_empty() {
current_content.push_str(line);
current_content.push('\n');
}
}
// Save last section
if let Some(section) = current_section {
let content = current_content.trim().to_string();
if !content.is_empty() && content.len() > 10 {
memories.push(MemoryInput {
kind: guess_kind(§ion, &content),
key: section,
content,
tags: vec!["imported".to_string(), "markdown".to_string()],
related_ids: Vec::new(),
});
}
}
memories
}
pub fn parse_bullet_list(content: &str) -> Vec<MemoryInput> {
let mut memories = Vec::new();
let mut current_key = String::new();
let mut current_content = String::new();
for line in content.lines() {
let trimmed = line.trim();
if trimmed.starts_with("- ") || trimmed.starts_with("* ") {
// Save previous
if !current_key.is_empty() {
let content = current_content.trim().to_string();
if !content.is_empty() {
memories.push(MemoryInput {
kind: guess_kind(¤t_key, &content),
key: current_key.clone(),
content,
tags: vec!["imported".to_string()],
related_ids: Vec::new(),
});
}
}
let bullet = &trimmed[2..];
if let Some((key, rest)) = bullet.split_once(':') {
current_key = key.trim().to_string();
current_content = rest.trim().to_string();
} else {
current_key = bullet.to_string();
current_content.clear();
}
} else if !trimmed.is_empty() {
if !current_content.is_empty() {
current_content.push(' ');
}
current_content.push_str(trimmed);
}
}
// Save last
if !current_key.is_empty() {
let content = current_content.trim().to_string();
if !content.is_empty() {
memories.push(MemoryInput {
kind: guess_kind(¤t_key, &content),
key: current_key,
content,
tags: vec!["imported".to_string()],
related_ids: Vec::new(),
});
}
}
memories
}
fn guess_kind(key: &str, content: &str) -> MemoryKind {
let lower_key = key.to_lowercase();
let lower_content = content.to_lowercase();
// Convention indicators
if lower_key.contains("naming") || lower_key.contains("style")
|| lower_key.contains("format") || lower_key.contains("convention")
|| lower_content.contains("always use") || lower_content.contains("never use")
|| lower_content.contains("should be")
{
return MemoryKind::Convention;
}
// Pattern indicators
if lower_key.contains("pattern") || lower_key.contains("architecture")
|| lower_key.contains("structure") || lower_key.contains("approach")
|| lower_content.contains("use the") || lower_content.contains("implement")
{
return MemoryKind::Pattern;
}
// Decision indicators
if lower_key.contains("decision") || lower_key.contains("chose")
|| lower_key.contains("selected") || lower_key.contains("why")
|| lower_content.contains("we decided") || lower_content.contains("we chose")
|| lower_content.contains("rationale")
{
return MemoryKind::Decision;
}
// Preference indicators
if lower_key.contains("prefer") || lower_key.contains("favorite")
|| lower_content.contains("we prefer") || lower_content.contains("preferred")
{
return MemoryKind::Preference;
}
// Default to context
MemoryKind::Context
}