smarana 0.8.4

An extensible note taking system for typst.
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use rusqlite::{params, Connection, Result};
use serde::Serialize;
use std::collections::{HashSet, HashMap};
use std::path::{Path, PathBuf};

use crate::frontparse::{parse_frontmatter, Frontmatter};

#[derive(Debug, Serialize)]
pub struct Note {
    pub filename: String,
    pub title: String,
    pub summary: Option<String>,
    pub date: Option<String>,
    pub time: Option<String>,
    pub tags: Option<Vec<String>>,
    pub note_type: Option<String>,
    pub uplink: Option<String>,
    pub content: String,
}

fn db_path(notebook_path: &Path) -> PathBuf {
    notebook_path.join(".smarana").join("index.db")
}

/// Initializes the SQLite database and its schema.
pub fn init_db(notebook_path: &Path) -> Result<()> {
    let path = db_path(notebook_path);
    let conn = Connection::open(&path)?;

    conn.execute(
        "CREATE TABLE IF NOT EXISTS notes (
            filename TEXT PRIMARY KEY,
            title TEXT NOT NULL,
            summary TEXT,
            date TEXT,
            time TEXT,
            tags TEXT,
            type TEXT DEFAULT 'fleeting',
            uplink TEXT,
            content TEXT NOT NULL DEFAULT ''
        )",
        [],
    )?;

    Ok(())
}

/// Drops and recreates the database schema.
pub fn init_db_force(notebook_path: &Path) -> Result<()> {
    let path = db_path(notebook_path);
    let conn = Connection::open(&path)?;
    conn.execute("DROP TABLE IF EXISTS notes", [])?;
    init_db(notebook_path)
}

/// Upserts a note using extracted frontmatter variables and full content
pub fn upsert_note(notebook_path: &Path, filename: &str, fm: &Frontmatter, content: &str) -> Result<()> {
    let path = db_path(notebook_path);
    let conn = Connection::open(&path)?;

    let title = fm.title.as_deref().unwrap_or("null");
    
    // Store tags as JSON array string
    let tags_json = if let Some(tags) = &fm.tags {
        serde_json::to_string(tags).ok()
    } else {
        None
    };

    let note_type = fm.note_type.as_deref().unwrap_or("fleeting");
    let uplink_str = fm.uplink.clone();

    conn.execute(
        "INSERT INTO notes (filename, title, summary, date, time, tags, type, uplink, content) 
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)
         ON CONFLICT(filename) DO UPDATE SET
         title=excluded.title,
         summary=excluded.summary,
         date=excluded.date,
         time=excluded.time,
         tags=excluded.tags,
         type=excluded.type,
         uplink=excluded.uplink,
         content=excluded.content",
        params![
            filename,
            title,
            fm.summary,
            fm.date,
            fm.time,
            tags_json,
            note_type,
            uplink_str,
            content
        ],
    )?;

    Ok(())
}



fn make_slug(title: &str) -> String {
    let re = regex::Regex::new(r"[^a-zA-Z0-9]+").unwrap();
    let slug = re.replace_all(title, "-").trim_matches('-').to_string();
    if slug.is_empty() { "untitled".to_string() } else { slug }
}

fn topological_sort_atomic(notes: &[(String, String, Option<String>, Option<String>, Option<String>)]) -> Vec<String> {
    // Map of slug -> filename to resolve up-links that are Typst labels (@Slug)
    let mut slug_to_filename: HashMap<String, String> = HashMap::new();
    let mut filename_to_note: HashMap<String, &(String, String, Option<String>, Option<String>, Option<String>)> = HashMap::new();
    
    for note in notes {
        slug_to_filename.insert(make_slug(&note.1), note.0.clone());
        filename_to_note.insert(note.0.clone(), note);
    }
    
    // Adjacency: uplink targets -> dependents
    let mut dependents: HashMap<String, Vec<String>> = HashMap::new();
    let mut has_parent: HashSet<String> = HashSet::new();
    
    for note in notes {
        if let Some(uplink_slug) = &note.4 {
            if let Some(parent_filename) = slug_to_filename.get(uplink_slug) {
                dependents.entry(parent_filename.clone()).or_default().push(note.0.clone());
                has_parent.insert(note.0.clone());
            }
        }
    }
    
    // Sort comparator: Recency (DESC = newest first)
    let cmp_date_desc = |a: &String, b: &String| {
        let na = filename_to_note.get(a).unwrap();
        let nb = filename_to_note.get(b).unwrap();
        let da = (na.2.as_deref().unwrap_or(""), na.3.as_deref().unwrap_or(""));
        let db = (nb.2.as_deref().unwrap_or(""), nb.3.as_deref().unwrap_or(""));
        db.cmp(&da) // descending
    };
    
    let mut roots: Vec<String> = notes.iter()
        .filter(|note| !has_parent.contains(&note.0))
        .map(|note| note.0.clone())
        .collect();
        
    roots.sort_by(&cmp_date_desc);
    
    let mut result = Vec::new();
    let mut visited = HashSet::new();
    let mut visiting = HashSet::new();
    
    fn dfs(
        node: String, 
        dependents: &HashMap<String, Vec<String>>, 
        visited: &mut HashSet<String>, 
        visiting: &mut HashSet<String>, 
        result: &mut Vec<String>,
        cmp: &impl Fn(&String, &String) -> std::cmp::Ordering
    ) {
        if visiting.contains(&node) || visited.contains(&node) {
            return;
        }
        visiting.insert(node.clone());
        result.push(node.clone());
        
        if let Some(mut children) = dependents.get(&node).cloned() {
            children.sort_by(cmp);
            for child in children {
                dfs(child, dependents, visited, visiting, result, cmp);
            }
        }
        
        visiting.remove(&node);
        visited.insert(node);
    }
    
    for root in roots {
        dfs(root, &dependents, &mut visited, &mut visiting, &mut result, &cmp_date_desc);
    }
    
    // Catch any detached cycle components
    let mut all_nodes: Vec<String> = notes.iter().map(|n| n.0.clone()).collect();
    all_nodes.sort_by(&cmp_date_desc);
    
    for node in all_nodes {
        if !visited.contains(&node) {
            dfs(node, &dependents, &mut visited, &mut visiting, &mut result, &cmp_date_desc);
        }
    }
    
    result
}

/// Generates `smarana.typ` at the notebook root with ordered `#include` directives
/// organized by compartment: fleeting → capture → atomic → appendix (tag index)
pub fn export_smarana_typ(notebook_path: &Path) {
    let path = db_path(notebook_path);
    let conn = match Connection::open(&path) {
        Ok(c) => c,
        Err(e) => {
            eprintln!("Failed to open database for smarana.typ export: {}", e);
            return;
        }
    };

    let mut content = String::from("// Auto-generated by smarana — do not edit manually\n\n#import \".smarana/document.typ\": conf\n#import \".smarana/dividers.typ\": section-divider\n#import \".smarana/appendix.typ\": tag-index-page\n#show: conf\n\n");

    // ── FLEETING Section ──
    content.push_str("// ═══ Fleeting Notes ═══\n");
    content.push_str("#section-divider(\"FLEETING\", \"fleeting\")\n\n");
    {
        let mut stmt = conn
            .prepare("SELECT filename FROM notes WHERE type = 'fleeting' ORDER BY date DESC, time DESC")
            .unwrap();
        let filenames: Vec<String> = stmt
            .query_map([], |row| row.get(0))
            .into_iter()
            .flatten()
            .filter_map(|r| r.ok())
            .collect();
        for f in &filenames {
            content.push_str(&format!("#include \"{}\"\n", f));
        }
    }

    content.push('\n');

    // ── CAPTURE Section ──
    content.push_str("// ═══ Capture Notes ═══\n");
    content.push_str("#section-divider(\"CAPTURE\", \"capture\")\n\n");
    {
        let mut stmt = conn
            .prepare("SELECT filename FROM notes WHERE type = 'capture' ORDER BY date DESC, time DESC")
            .unwrap();
        let filenames: Vec<String> = stmt
            .query_map([], |row| row.get(0))
            .into_iter()
            .flatten()
            .filter_map(|r| r.ok())
            .collect();
        for f in &filenames {
            content.push_str(&format!("#include \"{}\"\n", f));
        }
    }

    content.push('\n');

    // ── ATOMIC Section ──
    content.push_str("// ═══ Atomic Notes ═══\n");
    content.push_str("#section-divider(\"ATOMIC\", \"atomic\")\n\n");
    {
        let mut stmt = conn
            .prepare("SELECT filename, title, date, time, uplink FROM notes WHERE type = 'atomic' ORDER BY date ASC, time ASC")
            .unwrap();
        let atomic_notes: Vec<(String, String, Option<String>, Option<String>, Option<String>)> = stmt
            .query_map([], |row| {
                Ok((
                    row.get(0)?,
                    row.get(1)?,
                    row.get(2)?,
                    row.get(3)?,
                    row.get(4)?
                ))
            })
            .into_iter()
            .flatten()
            .filter_map(|r| r.ok())
            .collect();

        let sorted = topological_sort_atomic(&atomic_notes);
        for f in &sorted {
            content.push_str(&format!("#include \"{}\"\n", f));
        }
    }

    content.push('\n');

    // ── APPENDIX Section — Tag Index Pages ──
    content.push_str("// ═══ Appendix — Tag Index ═══\n");
    content.push_str("#section-divider(\"APPENDIX\", \"appendix\")\n\n");
    {
        // Collect all tags and their associated notes (title + slug)
        let mut stmt = conn
            .prepare("SELECT title, tags FROM notes WHERE tags IS NOT NULL AND tags != '[]' AND tags != '' ORDER BY title ASC")
            .unwrap();
        
        let mut tag_map: std::collections::BTreeMap<String, Vec<(String, String)>> = std::collections::BTreeMap::new();
        
        let rows: Vec<(String, String)> = stmt
            .query_map([], |row| {
                Ok((
                    row.get::<_, String>(0)?,
                    row.get::<_, String>(1)?,
                ))
            })
            .into_iter()
            .flatten()
            .filter_map(|r| r.ok())
            .collect();
        
        for (title, tags_json) in &rows {
            if let Ok(tags) = serde_json::from_str::<Vec<String>>(tags_json) {
                let slug = make_slug(title);
                for tag in tags {
                    tag_map.entry(tag).or_default().push((title.clone(), slug.clone()));
                }
            }
        }
        
        // Generate a tag-index-page call for each unique tag
        for (tag, notes) in &tag_map {
            content.push_str(&format!("#tag-index-page(\"{}\", (\n", escape_typst_string(tag)));
            for (title, slug) in notes {
                content.push_str(&format!("  (\"{}\", \"{}\"),\n", escape_typst_string(title), slug));
            }
            content.push_str("))\n\n");
        }
    }

    let smarana_path = notebook_path.join("smarana.typ");
    if let Err(e) = std::fs::write(&smarana_path, &content) {
        eprintln!("Failed to write smarana.typ: {}", e);
    }
}

/// Escape special characters for Typst string literals
fn escape_typst_string(s: &str) -> String {
    s.replace('\\', "\\\\").replace('"', "\\\"")
}

/// Reads a .typ file natively and upserts its explicit properties.
pub fn sync_file(notebook_path: &Path, file_path: &Path) {
    if let Ok(content) = std::fs::read_to_string(file_path) {
        let rel_path = file_path.strip_prefix(notebook_path).unwrap_or(file_path).to_string_lossy().to_string();
        let normalized_path = rel_path.replace("\\", "/");
        
        let fm = parse_frontmatter(&content, &normalized_path);
        
        if let Err(e) = upsert_note(notebook_path, &normalized_path, &fm, &content) {
            eprintln!("Failed to register note '{}': {}", normalized_path, e);
        }
    }

    export_smarana_typ(notebook_path);
}

/// Scans exclusively via `.typ` in the notebook root.
pub fn sync_all(notebook_path: &Path) {
    // Ensure we have the latest schema for a full sync
    if let Err(e) = init_db_force(notebook_path) {
        eprintln!("Failed to initialize database schema: {}", e);
        return;
    }

    if let Ok(entries) = std::fs::read_dir(notebook_path) {
        for entry in entries.filter_map(|e| e.ok()) {
            let path = entry.path();
            if path.is_file() {
                if let Some(ext) = path.extension() {
                    if ext == "typ" {
                        // Skip smarana.typ (the main document)
                        if let Some(name) = path.file_name() {
                            if name == "smarana.typ" {
                                continue;
                            }
                        }
                        // Ignore internal configurations beneath .smarana
                        if path.to_string_lossy().contains(".smarana") {
                            continue;
                        }
                        // Use internal sync without per-file export
                        if let Ok(content) = std::fs::read_to_string(&path) {
                            let rel_path = path.strip_prefix(notebook_path).unwrap_or(&path).to_string_lossy().to_string();
                            let normalized_path = rel_path.replace("\\", "/");
                            let fm = parse_frontmatter(&content, &normalized_path);
                            if let Err(e) = upsert_note(notebook_path, &normalized_path, &fm, &content) {
                                eprintln!("Failed to register note '{}': {}", normalized_path, e);
                            }
                        }
                    }
                }
            }
        }
    }

    // Export smarana.typ once after all notes are indexed
    export_smarana_typ(notebook_path);
}

/// Lists all notes structured explicitly for `-j` native JSON conversions!
pub fn list_notes(notebook_path: &Path) -> Result<Vec<Note>> {
    let path = db_path(notebook_path);
    let conn = Connection::open(&path)?;

    // We can order by date DESC
    let mut stmt = conn.prepare("SELECT filename, title, summary, date, time, tags, type, uplink, content FROM notes ORDER BY date DESC, time DESC")?;
    let note_iter = stmt.query_map([], |row| {
        let tags_str: Option<String> = row.get(5)?;
        let tags = if let Some(ts) = tags_str {
            serde_json::from_str(&ts).ok()
        } else {
            None
        };

        let uplink: Option<String> = row.get(7)?;

        Ok(Note {
            filename: row.get(0)?,
            title: row.get(1)?,
            summary: row.get(2)?,
            date: row.get(3)?,
            time: row.get(4)?,
            tags,
            note_type: row.get(6)?,
            uplink,
            content: row.get(8)?
        })
    })?;

    let mut notes = Vec::new();
    for note in note_iter {
        notes.push(note?);
    }

    Ok(notes)
}