oxios-markdown 0.2.0

Markdown knowledge management — ported from files.md by Artem Zakirullin
Documentation
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Checklist engine for markdown content.
//!
//! Ported from files.md (`server/pkg/txt/md.go` lines 95–261) by Artem Zakirullin.
//! Provides functions to parse, add, complete, and remove `- [ ]` / `- [x]` checklist items.

use std::collections::HashMap;

use crate::fs::hash_filename;
use crate::parser::norm_new_lines;

/// Parse checklist items from markdown.
///
/// Returns an ordered list of item texts and a map indicating which items are completed.
///
/// ```
/// use oxios_markdown::checklist::checklist_items;
/// let md = "- [ ] Buy milk\n- [x] Write code\n";
/// let (items, completed) = checklist_items(md);
/// assert_eq!(items, vec!["Buy milk", "Write code"]);
/// assert!(!completed["Buy milk"]);
/// assert!(completed["Write code"]);
/// ```
pub fn checklist_items(md: &str) -> (Vec<String>, HashMap<String, bool>) {
    let mut items = Vec::new();
    let mut is_completed = HashMap::new();

    for line in md.lines() {
        let line = line.trim();
        if let Some(item) = line.strip_prefix("- [ ] ") {
            items.push(item.to_string());
            is_completed.insert(item.to_string(), false);
        } else if let Some(item) = line.strip_prefix("- [x] ") {
            items.push(item.to_string());
            is_completed.insert(item.to_string(), true);
        }
    }

    (items, is_completed)
}

/// Get incomplete checklist items only.
///
/// ```
/// use oxios_markdown::checklist::incomplete_checklist_items;
/// let md = "- [ ] Task A\n- [x] Task B\n- [ ] Task C\n";
/// let incomplete = incomplete_checklist_items(md);
/// assert_eq!(incomplete, vec!["Task A", "Task C"]);
/// ```
pub fn incomplete_checklist_items(md: &str) -> Vec<String> {
    let (items, is_completed) = checklist_items(md);
    items
        .into_iter()
        .filter(|item| !is_completed[item])
        .collect()
}

/// Add a checklist item. Removes duplicate if exists.
///
/// When `checked` is false the item is inserted *before* the first existing
/// incomplete item, keeping all incomplete items grouped at the bottom.
/// Newlines in `item` are converted to spaces.
///
/// ```
/// use oxios_markdown::checklist::add_checklist_item;
/// let md = "- [ ] Existing task\n";
/// let result = add_checklist_item(md, "New task", false);
/// assert!(result.contains("- [ ] New task"));
/// assert!(result.contains("- [ ] Existing task"));
/// ```
pub fn add_checklist_item(md: &str, item: &str, checked: bool) -> String {
    // Normalise newlines → spaces
    let item = norm_new_lines(item).replace('\n', " ");
    let item = item.trim();

    // Remove existing copy (if any)
    let (md, _) = remove_checklist_item(md, item);
    let mut lines: Vec<String> = md.lines().map(|l| l.to_string()).collect();

    if checked {
        lines.push(format!("- [x] {}", item));
    } else {
        // Find the first incomplete item and insert before it
        let mut insert_index = lines.len();
        for (i, line) in lines.iter().enumerate() {
            let trimmed = line.trim();
            if trimmed.starts_with("- [ ] ") {
                insert_index = i;
                break;
            }
        }

        if insert_index == lines.len() {
            lines.push(format!("- [ ] {}", item));
        } else {
            lines.insert(insert_index, format!("- [ ] {}", item));
        }
    }

    lines.join("\n").trim().to_string()
}

/// Complete a checklist item by hash. Returns `(new_md, completed_item_text)`.
///
/// The marker stays in place (does not relocate to the bottom) to avoid
/// breaking multi-line records.
///
/// ```
/// use oxios_markdown::checklist::complete_checklist_item;
/// use oxios_markdown::fs::hash_filename;
/// let md = "- [ ] Buy milk\n- [ ] Write code\n";
/// let hash = hash_filename("Buy milk");
/// let (new_md, completed) = complete_checklist_item(md, &hash);
/// assert_eq!(completed, "Buy milk");
/// assert!(new_md.contains("- [x] Buy milk"));
/// ```
pub fn complete_checklist_item(md: &str, item_hash: &str) -> (String, String) {
    let mut found_item = String::new();
    let mut lines: Vec<String> = md.lines().map(|l| l.to_string()).collect();
    let mut found_index: Option<usize> = None;

    for (i, line) in lines.iter().enumerate() {
        let trimmed = line.trim();
        if trimmed.len() < 6 {
            continue;
        }
        if let Some(rest) = trimmed.strip_prefix("- [ ] ") {
            if hash_filename(rest) == item_hash {
                found_item = rest.to_string();
                found_index = Some(i);
                break;
            }
        }
    }

    if let Some(idx) = found_index {
        lines[idx] = format!("- [x] {}", found_item);
    }

    (lines.join("\n"), found_item)
}

/// Remove a checklist item by item text or hash. Returns `(new_md, removed_item)`.
///
/// ```
/// use oxios_markdown::checklist::remove_checklist_item;
/// let md = "- [ ] Task A\n- [x] Task B\n";
/// let (new_md, removed) = remove_checklist_item(md, "Task A");
/// assert_eq!(removed, "Task A");
/// assert!(!new_md.contains("Task A"));
/// assert!(new_md.contains("Task B"));
/// ```
pub fn remove_checklist_item(md: &str, item_or_hash: &str) -> (String, String) {
    let mut removed_item = String::new();
    let mut new_lines: Vec<String> = Vec::new();

    for line in md.lines() {
        let trimmed = line.trim();
        // Preserve lines that are too short to be checklist items
        if trimmed.len() < 6 {
            new_lines.push(line.to_string());
            continue;
        }

        // Both "- [ ] X" and "- [x] X" have a 6-char prefix before the item text
        let rest = trimmed
            .strip_prefix("- [ ] ")
            .or_else(|| trimmed.strip_prefix("- [x] "));

        if let Some(rest) = rest {
            if hash_filename(rest) == item_or_hash || rest == item_or_hash {
                removed_item = rest.to_string();
                continue; // skip this line
            }
        }

        new_lines.push(line.to_string());
    }

    (new_lines.join("\n"), removed_item)
}

/// Remove all completed checklist items. Returns `(new_md, removed_md)`.
///
/// ```
/// use oxios_markdown::checklist::remove_completed_checklist_items;
/// let md = "- [ ] Task A\n- [x] Task B\n- [x] Task C\n";
/// let (new_md, removed) = remove_completed_checklist_items(md);
/// assert!(new_md.contains("Task A"));
/// assert!(!new_md.contains("Task B"));
/// assert!(removed.contains("Task B"));
/// ```
pub fn remove_completed_checklist_items(md: &str) -> (String, String) {
    let mut removed_lines: Vec<String> = Vec::new();
    let mut new_lines: Vec<String> = Vec::new();

    for line in md.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("- [x] ") {
            removed_lines.push(trimmed.to_string());
        } else {
            new_lines.push(line.to_string());
        }
    }

    let removed_md = if removed_lines.is_empty() {
        String::new()
    } else {
        format!("{}\n", removed_lines.join("\n"))
    };

    (new_lines.join("\n"), removed_md)
}

/// Find a single checklist item by text or hash.
///
/// Returns the item text if found, empty string otherwise.
///
/// ```
/// use oxios_markdown::checklist::checklist_item;
/// let md = "- [ ] Task A\n- [x] Task B\n";
/// assert_eq!(checklist_item(md, "Task B"), "Task B");
/// assert_eq!(checklist_item(md, "nonexistent"), "");
/// ```
pub fn checklist_item(md: &str, item_or_hash: &str) -> String {
    for line in md.lines() {
        let trimmed = line.trim();
        if trimmed.len() < 6 {
            continue;
        }

        let rest = trimmed
            .strip_prefix("- [ ] ")
            .or_else(|| trimmed.strip_prefix("- [x] "));

        if let Some(rest) = rest {
            if hash_filename(rest) == item_or_hash || rest == item_or_hash {
                return rest.to_string();
            }
        }
    }

    String::new()
}

/// Insert text under a header in markdown content.
///
/// If the header doesn't exist yet it is created at the top.
/// If the header exists, `new_content` is inserted right after it
/// (before the next `###` sub-header).
///
/// ```
/// use oxios_markdown::checklist::add_header_and_text;
/// let content = "### Notes\nSome notes\n";
/// let result = add_header_and_text(content, "### Notes", "Extra line");
/// assert!(result.contains("Extra line"));
/// ```
pub fn add_header_and_text(content: &str, header: &str, new_content: &str) -> String {
    if !content.contains(header) {
        if content.is_empty() {
            return format!("{}\n{}", header, new_content);
        } else {
            return format!("{}\n{}\n\n{}", header, new_content, content);
        }
    }

    let lines: Vec<&str> = content.lines().collect();
    let mut header_index: Option<usize> = None;

    for (i, line) in lines.iter().enumerate() {
        if *line == header {
            header_index = Some(i);
            break;
        }
    }

    let header_index = match header_index {
        Some(idx) => idx,
        None => return format!("{}\n{}\n\n{}", header, new_content, content),
    };

    // Find where to insert (after the last line belonging to this header,
    // stopping at the next ### sub-header).
    let mut insert_index = header_index + 1;

    for (i, line) in lines.iter().enumerate().skip(header_index + 1) {
        if line.starts_with("###") {
            insert_index = i;
            break;
        }
        insert_index = i + 1;
    }

    // Build the new content
    let mut new_lines: Vec<String> = Vec::with_capacity(lines.len() + 2);
    for line in &lines[..insert_index] {
        new_lines.push(line.to_string());
    }
    new_lines.push(new_content.to_string());

    // Add empty line after new content if there's content following and it's not empty
    if insert_index < lines.len() && !lines[insert_index].trim().is_empty() {
        new_lines.push(String::new());
    }

    for line in &lines[insert_index..] {
        new_lines.push(line.to_string());
    }

    new_lines.join("\n")
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fs::hash_filename;

    #[test]
    fn test_checklist_items() {
        let md = "- [ ] Buy milk\n- [x] Write code\n- [ ] Read book\n";
        let (items, completed) = checklist_items(md);
        assert_eq!(items, vec!["Buy milk", "Write code", "Read book"]);
        assert!(!completed["Buy milk"]);
        assert!(completed["Write code"]);
        assert!(!completed["Read book"]);
    }

    #[test]
    fn test_checklist_items_empty() {
        let md = "No checklist here\nJust text\n";
        let (items, completed) = checklist_items(md);
        assert!(items.is_empty());
        assert!(completed.is_empty());
    }

    #[test]
    fn test_incomplete_checklist_items() {
        let md = "- [ ] Task A\n- [x] Task B\n- [ ] Task C\n";
        let incomplete = incomplete_checklist_items(md);
        assert_eq!(incomplete, vec!["Task A", "Task C"]);
    }

    #[test]
    fn test_add_checklist_item_unchecked() {
        let md = "- [ ] Existing task\n";
        let result = add_checklist_item(md, "New task", false);
        // New unchecked item should be inserted before existing unchecked
        let lines: Vec<&str> = result.lines().collect();
        let new_pos = lines.iter().position(|l| l.contains("New task")).unwrap();
        let existing_pos = lines
            .iter()
            .position(|l| l.contains("Existing task"))
            .unwrap();
        assert!(new_pos < existing_pos);
    }

    #[test]
    fn test_add_checklist_item_checked() {
        let md = "- [ ] Task A\n";
        let result = add_checklist_item(md, "Done task", true);
        assert!(result.contains("- [x] Done task"));
        assert!(result.contains("- [ ] Task A"));
    }

    #[test]
    fn test_add_checklist_item_removes_duplicate() {
        let md = "- [ ] Task A\n- [ ] Task B\n";
        let result = add_checklist_item(md, "Task A", false);
        // Should only appear once
        assert_eq!(result.matches("Task A").count(), 1);
    }

    #[test]
    fn test_add_checklist_item_newlines_to_spaces() {
        let md = "- [ ] Existing\n";
        let result = add_checklist_item(md, "Multi\nline\nitem", false);
        assert!(result.contains("- [ ] Multi line item"));
    }

    #[test]
    fn test_complete_checklist_item() {
        let md = "- [ ] Buy milk\n- [ ] Write code\n";
        let hash = hash_filename("Buy milk");
        let (new_md, completed) = complete_checklist_item(md, &hash);
        assert_eq!(completed, "Buy milk");
        assert!(new_md.contains("- [x] Buy milk"));
        assert!(new_md.contains("- [ ] Write code"));
    }

    #[test]
    fn test_complete_checklist_item_not_found() {
        let md = "- [ ] Buy milk\n";
        let (new_md, completed) = complete_checklist_item(md, "nonexistent_hash");
        assert_eq!(completed, "");
        assert_eq!(new_md, md.trim_end());
    }

    #[test]
    fn test_remove_checklist_item_by_text() {
        let md = "- [ ] Task A\n- [x] Task B\n";
        let (new_md, removed) = remove_checklist_item(md, "Task A");
        assert_eq!(removed, "Task A");
        assert!(!new_md.contains("Task A"));
        assert!(new_md.contains("Task B"));
    }

    #[test]
    fn test_remove_checklist_item_by_hash() {
        let md = "- [ ] Task A\n- [x] Task B\n";
        let hash = hash_filename("Task A");
        let (new_md, removed) = remove_checklist_item(md, &hash);
        assert_eq!(removed, "Task A");
        assert!(!new_md.contains("Task A"));
    }

    #[test]
    fn test_remove_checklist_item_not_found() {
        let md = "- [ ] Task A\n";
        let (new_md, removed) = remove_checklist_item(md, "nonexistent");
        assert_eq!(removed, "");
        assert!(new_md.contains("Task A"));
    }

    #[test]
    fn test_remove_completed_checklist_items() {
        let md = "- [ ] Task A\n- [x] Task B\n- [x] Task C\nSome text\n";
        let (new_md, removed) = remove_completed_checklist_items(md);
        assert!(new_md.contains("Task A"));
        assert!(!new_md.contains("Task B"));
        assert!(!new_md.contains("Task C"));
        assert!(new_md.contains("Some text"));
        assert!(removed.contains("- [x] Task B"));
        assert!(removed.contains("- [x] Task C"));
    }

    #[test]
    fn test_checklist_item_by_text() {
        let md = "- [ ] Task A\n- [x] Task B\n";
        assert_eq!(checklist_item(md, "Task B"), "Task B");
    }

    #[test]
    fn test_checklist_item_by_hash() {
        let md = "- [ ] Task A\n";
        let hash = hash_filename("Task A");
        assert_eq!(checklist_item(md, &hash), "Task A");
    }

    #[test]
    fn test_checklist_item_not_found() {
        let md = "- [ ] Task A\n";
        assert_eq!(checklist_item(md, "nonexistent"), "");
    }

    #[test]
    fn test_add_header_and_text_new_header() {
        let content = "Existing content\n";
        let result = add_header_and_text(content, "## New Header", "New text");
        assert!(result.starts_with("## New Header\nNew text\n\nExisting content"));
    }

    #[test]
    fn test_add_header_and_text_existing_header() {
        let content = "### Notes\nSome notes\n### Other\nOther content";
        let result = add_header_and_text(content, "### Notes", "Extra line");
        // Go behavior: inserts new content after existing content under the header, before next ### header
        assert!(result.contains("Some notes\nExtra line"));
        assert!(result.contains("### Other"));
    }

    #[test]
    fn test_add_header_and_text_empty_content() {
        let result = add_header_and_text("", "## Header", "Text");
        assert_eq!(result, "## Header\nText");
    }

    #[test]
    fn test_add_checklist_item_to_empty() {
        let result = add_checklist_item("", "First task", false);
        assert_eq!(result, "- [ ] First task");
    }
}