mdvault-core 0.7.2

Core library for mdvault - markdown vault management
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
use mdvault_core::markdown_ast::{
    InsertPosition, MarkdownAstError, MarkdownEditor, SectionMatch,
};

// === Basic insertion tests ===

#[test]
fn insert_at_begin_of_section() {
    let input = r#"# Changelog

## Unreleased

### Added

- Feature A

## 1.0.0
"#;

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("Added"),
        "- Feature B\n",
        InsertPosition::Begin,
    )
    .unwrap();

    // Feature B should appear before Feature A
    let feature_b_pos = result.content.find("Feature B").unwrap();
    let feature_a_pos = result.content.find("Feature A").unwrap();
    assert!(feature_b_pos < feature_a_pos);
}

#[test]
fn insert_at_end_of_section() {
    let input = r#"# Inbox

- Task 1
- Task 2

# Done
"#;

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("Inbox"),
        "- Task 3\n",
        InsertPosition::End,
    )
    .unwrap();

    // Task 3 should appear after Task 2 but before Done
    let task2_pos = result.content.find("Task 2").unwrap();
    let task3_pos = result.content.find("Task 3").unwrap();
    let done_pos = result.content.find("# Done").unwrap();
    assert!(task2_pos < task3_pos);
    assert!(task3_pos < done_pos);
}

// === Section matching tests ===

#[test]
fn case_insensitive_match_default() {
    let input = "# INBOX\n\nContent\n";

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("inbox"),
        "New item\n",
        InsertPosition::Begin,
    );

    assert!(result.is_ok());
}

#[test]
fn case_sensitive_match_fails_when_case_differs() {
    let input = "# INBOX\n\nContent\n";

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("inbox").case_sensitive(true),
        "New item\n",
        InsertPosition::Begin,
    );

    assert!(matches!(result, Err(MarkdownAstError::SectionNotFound(_))));
}

#[test]
fn case_sensitive_match_succeeds_when_case_matches() {
    let input = "# INBOX\n\nContent\n";

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("INBOX").case_sensitive(true),
        "New item\n",
        InsertPosition::Begin,
    );

    assert!(result.is_ok());
}

#[test]
fn trimmed_title_matching() {
    let input = "# Inbox   \n\nContent\n"; // Trailing spaces in heading

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("Inbox"),
        "New\n",
        InsertPosition::Begin,
    );

    assert!(result.is_ok());
}

// === Edge cases ===

#[test]
fn section_not_found_error() {
    let input = "# Existing\n\nContent\n";

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("NonExistent"),
        "Fragment\n",
        InsertPosition::Begin,
    );

    match result {
        Err(MarkdownAstError::SectionNotFound(s)) => assert_eq!(s, "NonExistent"),
        _ => panic!("Expected SectionNotFound error"),
    }
}

#[test]
fn empty_document_error() {
    let result = MarkdownEditor::insert_into_section(
        "",
        &SectionMatch::new("Any"),
        "Fragment\n",
        InsertPosition::Begin,
    );

    assert!(matches!(result, Err(MarkdownAstError::EmptyDocument)));
}

#[test]
fn whitespace_only_document_error() {
    let result = MarkdownEditor::insert_into_section(
        "   \n\n   ",
        &SectionMatch::new("Any"),
        "Fragment\n",
        InsertPosition::Begin,
    );

    assert!(matches!(result, Err(MarkdownAstError::EmptyDocument)));
}

#[test]
fn empty_fragment_is_noop() {
    let input = "# Section\n\nOriginal\n";

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("Section"),
        "",
        InsertPosition::Begin,
    )
    .unwrap();

    // Content should be unchanged
    assert!(result.content.contains("Original"));
}

#[test]
fn code_block_with_hash_not_matched_as_heading() {
    let input = r#"# Real Heading

```bash
# This is a comment, not a heading
echo "hello"
```

# Another Heading
"#;

    let headings = MarkdownEditor::find_headings(input);
    assert_eq!(headings.len(), 2);
    assert_eq!(headings[0].title, "Real Heading");
    assert_eq!(headings[1].title, "Another Heading");
}

#[test]
fn last_section_extends_to_eof() {
    let input = "# Only Section\n\nLine 1\n\nLine 2\n";

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("Only Section"),
        "New line\n",
        InsertPosition::End,
    )
    .unwrap();

    // New line should appear after Line 2
    let line2_pos = result.content.find("Line 2").unwrap();
    let newline_pos = result.content.find("New line").unwrap();
    assert!(newline_pos > line2_pos);
}

#[test]
fn insert_into_empty_section() {
    let input = "# Empty Section\n\n# Next Section\n\nContent\n";

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("Empty Section"),
        "New content\n",
        InsertPosition::Begin,
    )
    .unwrap();

    // New content should appear after Empty Section but before Next Section
    let empty_section_pos = result.content.find("# Empty Section").unwrap();
    let new_content_pos = result.content.find("New content").unwrap();
    let next_section_pos = result.content.find("# Next Section").unwrap();
    assert!(new_content_pos > empty_section_pos);
    assert!(new_content_pos < next_section_pos);
}

#[test]
fn nested_sections_respects_level() {
    let input = r#"# Level 1

## Level 2 A

Content A

### Level 3

Deep content

## Level 2 B

Content B
"#;

    // Insert into Level 2 A - should only affect until Level 2 B
    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("Level 2 A"),
        "New A content\n",
        InsertPosition::End,
    )
    .unwrap();

    // Verify insertion is before "## Level 2 B"
    let level2b_pos = result.content.find("## Level 2 B").unwrap();
    let new_content_pos = result.content.find("New A content").unwrap();
    assert!(new_content_pos < level2b_pos);
}

#[test]
fn multiple_same_name_sections_matches_first() {
    let input = r#"# Inbox

First inbox content

# Other

# Inbox

Second inbox content
"#;

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("Inbox"),
        "New item\n",
        InsertPosition::Begin,
    )
    .unwrap();

    // Should insert after FIRST "# Inbox"
    let first_inbox = result.content.find("# Inbox").unwrap();
    let new_item = result.content.find("New item").unwrap();

    // Find second inbox after the first
    let after_first = &result.content[first_inbox + 7..];
    let second_inbox = after_first.find("# Inbox").unwrap() + first_inbox + 7;

    assert!(new_item > first_inbox && new_item < second_inbox);
}

// === find_headings tests ===

#[test]
fn find_headings_returns_all_levels() {
    let input = r#"# H1

## H2

### H3

#### H4

##### H5

###### H6
"#;

    let headings = MarkdownEditor::find_headings(input);
    assert_eq!(headings.len(), 6);
    assert_eq!(headings[0].level, 1);
    assert_eq!(headings[1].level, 2);
    assert_eq!(headings[2].level, 3);
    assert_eq!(headings[3].level, 4);
    assert_eq!(headings[4].level, 5);
    assert_eq!(headings[5].level, 6);
}

#[test]
fn find_headings_preserves_order() {
    let input = r#"# First

## Second

# Third
"#;

    let headings = MarkdownEditor::find_headings(input);
    assert_eq!(headings.len(), 3);
    assert_eq!(headings[0].title, "First");
    assert_eq!(headings[1].title, "Second");
    assert_eq!(headings[2].title, "Third");
}

// === section_exists tests ===

#[test]
fn section_exists_returns_true_for_existing() {
    let input = "# Existing\n\nContent\n";
    assert!(MarkdownEditor::section_exists(input, &SectionMatch::new("Existing")));
}

#[test]
fn section_exists_returns_false_for_missing() {
    let input = "# Existing\n\nContent\n";
    assert!(!MarkdownEditor::section_exists(input, &SectionMatch::new("Missing")));
}

// === Setext headings ===

#[test]
fn setext_headings_level_1() {
    let input = r#"Main Title
===========

Content under main.
"#;

    let headings = MarkdownEditor::find_headings(input);
    assert_eq!(headings.len(), 1);
    assert_eq!(headings[0].title, "Main Title");
    assert_eq!(headings[0].level, 1);
}

#[test]
fn setext_headings_level_2() {
    let input = r#"Subtitle
--------

Subtitle content.
"#;

    let headings = MarkdownEditor::find_headings(input);
    assert_eq!(headings.len(), 1);
    assert_eq!(headings[0].title, "Subtitle");
    assert_eq!(headings[0].level, 2);
}

#[test]
fn insert_into_setext_heading() {
    let input = r#"My Section
===========

Original content.
"#;

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("My Section"),
        "New content\n",
        InsertPosition::Begin,
    )
    .unwrap();

    assert!(result.content.contains("New content"));
}

// === Wikilinks and special character preservation ===

#[test]
fn preserves_wikilinks_in_existing_content() {
    let input = r#"# Notes

- [[wikilink]]
- [[page#section]]
- Regular text

# Other
"#;

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("Notes"),
        "- New item\n",
        InsertPosition::End,
    )
    .unwrap();

    // Wikilinks should NOT be escaped
    assert!(
        result.content.contains("[[wikilink]]"),
        "Wikilinks should be preserved, got: {}",
        result.content
    );
    assert!(
        result.content.contains("[[page#section]]"),
        "Wikilinks with sections should be preserved, got: {}",
        result.content
    );
}

#[test]
fn preserves_wikilinks_in_inserted_content() {
    let input = r#"# Notes

- Existing item

# Other
"#;

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("Notes"),
        "- [[new wikilink]]\n",
        InsertPosition::End,
    )
    .unwrap();

    // Inserted wikilinks should NOT be escaped
    assert!(
        result.content.contains("[[new wikilink]]"),
        "Inserted wikilinks should be preserved, got: {}",
        result.content
    );
}

// === Blank line handling tests ===

#[test]
fn insert_at_end_preserves_section_separator() {
    // This is the exact case from the user report:
    // When inserting at end of section 1, the new text should appear
    // after existing content, with the blank line separator maintained
    // between sections.
    let input = r#"# section 1
- content1
- other content

# another section
- more text
"#;

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("section 1"),
        "- new text\n",
        InsertPosition::End,
    )
    .unwrap();

    // Expected output:
    // # section 1
    // - content1
    // - other content
    // - new text
    //
    // # another section
    // - more text

    let expected = r#"# section 1
- content1
- other content
- new text

# another section
- more text
"#;

    assert_eq!(
        result.content, expected,
        "\nExpected:\n{}\n\nGot:\n{}\n",
        expected, result.content
    );
}

#[test]
fn insert_at_end_with_multiple_blank_lines() {
    // Multiple blank lines should be normalized to one
    let input = "# Section\n- item1\n\n\n\n# Next\n";

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("Section"),
        "- item2\n",
        InsertPosition::End,
    )
    .unwrap();

    // New item should be right after item1, with one blank line before Next
    assert!(result.content.contains("- item1\n- item2\n"));
    assert!(result.content.contains("\n\n# Next"));
}

#[test]
fn insert_at_end_of_last_section_no_trailing_blank() {
    // Last section shouldn't add extra blank lines at EOF
    let input = "# Only Section\n- item1\n";

    let result = MarkdownEditor::insert_into_section(
        input,
        &SectionMatch::new("Only Section"),
        "- item2\n",
        InsertPosition::End,
    )
    .unwrap();

    assert_eq!(result.content, "# Only Section\n- item1\n- item2\n");
}