Repository navigation
Expand file tree
/
Copy pathmigration.rs
More file actions
163 lines (137 loc) · 5.6 KB
/
Copy pathmigration.rs
File metadata and controls
163 lines (137 loc) · 5.6 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
use crate::types::{MemoryInput, MemoryKind};
use std::path::Path;
pub fn import_from_obsidian(vault_path: &Path) -> anyhow::Result<Vec<MemoryInput>> {
let mut memories = Vec::new();
for entry in walkdir::WalkDir::new(vault_path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.path().extension().is_some_and(|ext| ext == "md"))
{
let path = entry.path();
if let Ok(content) = std::fs::read_to_string(path) {
let file_stem = path.file_stem()
.and_then(|n| n.to_str())
.unwrap_or("untitled");
let (frontmatter, body) = parse_frontmatter(&content);
let tags: Vec<String> = frontmatter
.get("tags")
.map(|t| {
t.split(',')
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect()
})
.unwrap_or_default();
let kind = if frontmatter.get("type").is_some_and(|t| t == "convention") {
MemoryKind::Convention
} else if frontmatter.get("type").is_some_and(|t| t == "pattern") {
MemoryKind::Pattern
} else if frontmatter.get("type").is_some_and(|t| t == "decision") {
MemoryKind::Decision
} else {
MemoryKind::Context
};
memories.push(MemoryInput {
kind,
key: file_stem.to_string(),
content: body,
tags,
related_ids: Vec::new(),
});
}
}
Ok(memories)
}
fn parse_frontmatter(content: &str) -> (std::collections::HashMap<String, String>, String) {
let mut frontmatter = std::collections::HashMap::new();
let mut body = content.to_string();
if content.starts_with("---") {
if let Some(end) = content[3..].find("---") {
let fm_str = &content[3..end + 3];
body = content[end + 6..].trim().to_string();
for line in fm_str.lines() {
if let Some((key, value)) = line.split_once(':') {
frontmatter.insert(
key.trim().to_string(),
value.trim().to_string(),
);
}
}
}
}
(frontmatter, body)
}
pub fn import_from_json_file(json_path: &Path) -> anyhow::Result<Vec<MemoryInput>> {
let content = std::fs::read_to_string(json_path)?;
let memories: Vec<MemoryInput> = serde_json::from_str(&content)?;
Ok(memories)
}
pub fn import_from_markdown_dir(dir: &Path) -> anyhow::Result<Vec<MemoryInput>> {
let mut memories = Vec::new();
for entry in walkdir::WalkDir::new(dir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.path().extension().is_some_and(|ext| ext == "md"))
{
let path = entry.path();
if let Ok(content) = std::fs::read_to_string(path) {
let file_stem = path.file_stem()
.and_then(|n| n.to_str())
.unwrap_or("untitled");
let parsed = crate::markdown::parse_markdown(&content);
if !parsed.is_empty() {
memories.extend(parsed);
} else {
memories.push(MemoryInput {
kind: MemoryKind::Context,
key: file_stem.to_string(),
content,
tags: vec!["imported".to_string()],
related_ids: Vec::new(),
});
}
}
}
Ok(memories)
}
pub fn import_from_notion_csv(csv_path: &std::path::Path) -> anyhow::Result<Vec<MemoryInput>> {
let content = std::fs::read_to_string(csv_path)?;
let mut memories = Vec::new();
let mut lines = content.lines();
let header = lines.next().unwrap_or("");
let headers: Vec<&str> = header.split(',').collect();
let name_idx = headers.iter().position(|h| h.trim().to_lowercase() == "name");
let content_idx = headers.iter().position(|h| h.trim().to_lowercase() == "content");
let tags_idx = headers.iter().position(|h| h.trim().to_lowercase() == "tags");
let type_idx = headers.iter().position(|h| h.trim().to_lowercase() == "type");
for line in lines {
let fields: Vec<&str> = line.split(',').collect();
let name = name_idx.and_then(|i| fields.get(i)).unwrap_or(&"").trim();
let content_val = content_idx.and_then(|i| fields.get(i)).unwrap_or(&"").trim();
let tags_str = tags_idx.and_then(|i| fields.get(i)).unwrap_or(&"").trim();
let type_str = type_idx.and_then(|i| fields.get(i)).unwrap_or(&"").trim();
if name.is_empty() && content_val.is_empty() {
continue;
}
let kind = match type_str.to_lowercase().as_str() {
"convention" | "conv" => MemoryKind::Convention,
"pattern" | "pat" => MemoryKind::Pattern,
"decision" | "dec" => MemoryKind::Decision,
"preference" | "pref" => MemoryKind::Preference,
_ => MemoryKind::Context,
};
let tags: Vec<String> = tags_str
.split(';')
.map(|t| t.trim().to_string())
.filter(|t| !t.is_empty())
.collect();
memories.push(MemoryInput {
kind,
key: name.to_string(),
content: content_val.to_string(),
tags,
related_ids: Vec::new(),
});
}
Ok(memories)
}