journey-cli 0.9.11

A CLI-based journal application with automatic timestamping, vault management, and Obsidian integration
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
use crate::config::{VaultConfig, NoteFormat};
use crate::date_time::DateTimeHandler;
use crate::errors::JourneyError;
use chrono::{DateTime, Local, NaiveDate, Datelike, Weekday};
use std::fs;
use std::path::PathBuf;

pub struct Vault {
    pub config: VaultConfig,
    pub date_handler: DateTimeHandler,
}

impl Vault {
    pub fn new(mut config: VaultConfig) -> Self {
        // Backward-compat: map legacy note_format to list_type when not set
        if config.list_type.is_none() {
            if let Some(fmt) = config.note_format.clone() {
                config.list_type = Some(fmt);
            }
        }
        let date_handler = DateTimeHandler::new(config.locale.clone());
        Self {
            config,
            date_handler,
        }
    }

    /// Get locale-dependent table headers
    pub fn get_table_headers(&self) -> (String, String) {
        // Check for custom table headers first
        if let Some(ref custom_headers) = self.config.table_headers {
            return (custom_headers.time.clone(), custom_headers.content.clone());
        }
        
        let locale = &self.config.locale;
        
        // Check for Norwegian locale
        if locale.starts_with("no_") || locale.starts_with("nb_") || locale.starts_with("nn_") {
            ("Tid".to_string(), "Innhold".to_string())
        }
        // Check for Swedish locale
        else if locale.starts_with("sv_") {
            ("Tid".to_string(), "Innehåll".to_string())
        }
        // Check for Danish locale
        else if locale.starts_with("da_") {
            ("Tid".to_string(), "Indhold".to_string())
        }
        // Check for Finnish locale
        else if locale.starts_with("fi_") {
            ("Aika".to_string(), "Sisältö".to_string())
        }
        // Check for German locale
        else if locale.starts_with("de_") {
            ("Zeit".to_string(), "Inhalt".to_string())
        }
        // Check for French locale
        else if locale.starts_with("fr_") {
            ("Heure".to_string(), "Contenu".to_string())
        }
        // Check for Spanish locale
        else if locale.starts_with("es_") {
            ("Hora".to_string(), "Contenido".to_string())
        }
        // Check for Italian locale
        else if locale.starts_with("it_") {
            ("Ora".to_string(), "Contenuto".to_string())
        }
        // Check for Dutch locale
        else if locale.starts_with("nl_") {
            ("Tijd".to_string(), "Inhoud".to_string())
        }
        // Check for Portuguese locale
        else if locale.starts_with("pt_") {
            ("Hora".to_string(), "Conteúdo".to_string())
        }
        // Check for Russian locale
        else if locale.starts_with("ru_") {
            ("Время".to_string(), "Содержание".to_string())
        }
        // Check for Japanese locale
        else if locale.starts_with("ja_") {
            ("時間".to_string(), "内容".to_string())
        }
        // Check for Chinese locale
        else if locale.starts_with("zh_") {
            ("时间".to_string(), "内容".to_string())
        }
        // Default to English
        else {
            ("Time".to_string(), "Content".to_string())
        }
    }

    pub fn get_note_path(&self, date: NaiveDate) -> PathBuf {
        if let Some(ref format) = self.config.file_path_format {
            // Use custom file path format
            let formatted_path = self.format_custom_path(format, date);
            self.config.path.join(formatted_path)
        } else {
            // Use default format: YYYY-MM-DD.md
            let date_str = self.date_handler.format_date(date);
            self.config.path.join(format!("{}.md", date_str))
        }
    }

    /// Backwards-compatible API: add a note without category
    pub fn add_note(&self, content: &str, timestamp: Option<DateTime<Local>>) -> Result<(), JourneyError> {
        self.add_note_with_category(content, timestamp, None)
    }

    /// Category-aware API used by the application
    pub fn add_note_with_category(&self, content: &str, timestamp: Option<DateTime<Local>>, category: Option<&str>) -> Result<(), JourneyError> {
        let timestamp = timestamp.unwrap_or_else(|| self.date_handler.get_current_datetime());
        let date = timestamp.date_naive();
        let note_path = self.get_note_path(date);

        // Ensure the vault directory exists
        if let Some(parent) = note_path.parent() {
            fs::create_dir_all(parent)?;
        }

        // Expand phrases in the content
        let expanded_content = self.expand_phrases(content);
        let formatted_time = self.date_handler.format_datetime(timestamp);
        
        // Get the configured note format (default to bullet if not specified)
        let note_format = self.config.list_type.as_ref().unwrap_or(&NoteFormat::Bullet);
        let note_entry = self.format_note_entry(&formatted_time, &expanded_content, note_format);

        // Check if file exists and has content
        if note_path.exists() {
            let existing_content = fs::read_to_string(&note_path)?;
            
            // Check if we need to convert the existing format
            let converted_content = self.convert_note_format_if_needed(&existing_content, note_format)?;
            
            // If section_header is specified, find and insert into that section in chronological order
            if let Some(section_name) = self.config.get_section_header(category) {
                if let Some(section_start) = self.find_section(&converted_content, section_name) {
                    let mut lines: Vec<&str> = converted_content.lines().collect();
                    
                    // Find insertion index to keep chronological order within the section
                    let insertion_index = self.find_insertion_index(
                        &lines,
                        section_start,
                        &formatted_time,
                        self.config.list_type.as_ref().unwrap_or(&NoteFormat::Bullet)
                    );
                    
                    // Insert the note at the chosen index
                    lines.insert(insertion_index, &note_entry);
                    
                    fs::write(&note_path, lines.join("\n"))?;
                    return Ok(());
                } else {
                    // Section doesn't exist, create it at the end
                    let mut new_content = converted_content;
                    if !new_content.ends_with('\n') {
                        new_content.push('\n');
                    }
                    new_content.push_str(&format!("\n# {}\n", section_name));
                    new_content.push_str(&note_entry);
                    fs::write(&note_path, new_content)?;
                    return Ok(());
                }
            }
            
            // No section configured: insert in chronological order across the whole file
            let mut lines: Vec<&str> = converted_content.lines().collect();
            let insertion_index = self.find_insertion_index(
                &lines,
                0,
                &formatted_time,
                self.config.list_type.as_ref().unwrap_or(&NoteFormat::Bullet)
            );
            lines.insert(insertion_index, &note_entry);
            fs::write(&note_path, lines.join("\n"))?;
        } else {
            // Create new file
            let file_content = if let Some(template_file) = &self.config.template_file {
                // Use template file
                self.create_file_from_template(template_file, timestamp, &note_entry)?
            } else {
                // Use default template
                self.create_default_file_content(date, &note_entry, category)
            };
            
            fs::write(&note_path, file_content)?;
        }

        Ok(())
    }

    fn create_default_file_content(&self, date: NaiveDate, note_entry: &str, category: Option<&str>) -> String {
        let mut file_content = String::new();
        
        // Add frontmatter
        file_content.push_str("---\n");
        file_content.push_str(&format!("date: {}\n", self.date_handler.format_date(date)));
        file_content.push_str("---\n\n");
        
        // Add section if specified
        if let Some(section_name) = self.config.get_section_header(category) {
            file_content.push_str(&format!("# {}\n\n", section_name));
        }
        
        // Add table header if using table format
        let note_format = self.config.list_type.as_ref().unwrap_or(&NoteFormat::Bullet);
        if note_format == &NoteFormat::Table {
            let (time_header, content_header) = self.get_table_headers();
            file_content.push_str(&format!("| {} | {} |\n", time_header, content_header));
            file_content.push_str("|------|----------|\n");
        }
        
        file_content.push_str(note_entry);
        file_content
    }

    /// Determine if a line is a table header or separator line
    fn is_table_header_line(&self, line: &str) -> bool {
        let trimmed = line.trim();

        // Separator like |---|
        if trimmed.starts_with("|---") {
            return true;
        }

        // Match against localized/custom headers
        if trimmed.starts_with('|') {
            let (time_header, content_header) = self.get_table_headers();
            let expected = format!("| {} | {} |", time_header, content_header);
            if trimmed == expected {
                return true;
            }
        }

        false
    }

    fn create_file_from_template(&self, template_file: &str, timestamp: DateTime<Local>, note_entry: &str) -> Result<String, JourneyError> {
        // Read the template file
        let template_path = PathBuf::from(template_file);
        let template_content = fs::read_to_string(&template_path)
            .map_err(|e| JourneyError::Io(std::io::Error::new(std::io::ErrorKind::NotFound, format!("Failed to read template file '{}': {}", template_file, e))))?;
        
        // Process template variables
        let mut processed_content = template_content;
        
        // Replace template variables
        let date = timestamp.date_naive();
        processed_content = processed_content.replace("{{date}}", &self.date_handler.format_date(date));
        processed_content = processed_content.replace("{{time}}", &self.date_handler.format_time(timestamp.time()));
        processed_content = processed_content.replace("{{datetime}}", &self.date_handler.format_datetime(timestamp));
        
        // Additional template variables
        // Yesterday and tomorrow dates - use same format as note filenames
        let yesterday_date = date - chrono::Duration::days(1);
        let tomorrow_date = date + chrono::Duration::days(1);
        let yesterday = self.date_handler.format_date(yesterday_date);
        let tomorrow = self.date_handler.format_date(tomorrow_date);
        processed_content = processed_content.replace("{yesterday}", &yesterday);
        processed_content = processed_content.replace("{tomorrow}", &tomorrow);
        processed_content = processed_content.replace("{{yesterday}}", &yesterday);
        processed_content = processed_content.replace("{{tomorrow}}", &tomorrow);
        
        // Weekday (lowercase) - full weekday name
        let weekday = date.format("%A").to_string();
        processed_content = processed_content.replace("{weekday}", &weekday);
        processed_content = processed_content.replace("{{weekday}}", &weekday);
        
        // Weekday (uppercase) - abbreviated weekday name
        let weekday_short = date.format("%a").to_string();
        processed_content = processed_content.replace("{Weekday}", &weekday_short);
        processed_content = processed_content.replace("{{Weekday}}", &weekday_short);
        
        // Additional variables
        // Created timestamp
        let created = timestamp.format("%Y-%m-%d %H:%M:%S").to_string();
        processed_content = processed_content.replace("{created}", &created);
        processed_content = processed_content.replace("{{created}}", &created);
        
        // Today's date
        let today = self.date_handler.format_date(date);
        processed_content = processed_content.replace("{today}", &today);
        processed_content = processed_content.replace("{{today}}", &today);
        
        // Handle section name replacement (support legacy and new placeholders)
        if let Some(section_title) = self.config.section_header.as_ref().or(self.config.section_name.as_ref()) {
            processed_content = processed_content.replace("{{section_header}}", section_title);
            processed_content = processed_content.replace("{section_header}", section_title);
            processed_content = processed_content.replace("{{section_name}}", section_title);
            processed_content = processed_content.replace("{section_name}", section_title);
        }
        
        // If the template doesn't contain a placeholder for notes, append the note
        if !processed_content.contains("{{note}}") && !processed_content.contains("{note}") {
            processed_content.push_str(note_entry);
        } else {
            processed_content = processed_content.replace("{{note}}", note_entry);
            processed_content = processed_content.replace("{note}", note_entry);
        }
        
        Ok(processed_content)
    }

    pub fn find_section(&self, content: &str, section_name: &str) -> Option<usize> {
        let lines: Vec<&str> = content.lines().collect();
        for (i, line) in lines.iter().enumerate() {
            if line.trim().starts_with('#') && line.contains(section_name) {
                return Some(i);
            }
        }
        None
    }

    pub fn find_section_end(&self, lines: &[&str], section_start: usize) -> usize {
        // Look for the next section header or end of file
        for i in (section_start + 1)..lines.len() {
            if lines[i].trim().starts_with('#') {
                return i;
            }
        }
        // If no next section found, return end of file
        lines.len()
    }

    pub fn find_section_content_end(&self, lines: &[&str], section_start: usize) -> usize {
        let section_end = self.find_section_end(lines, section_start);
        
        // Find the actual end of content in the section (skip blank lines at the end)
        let mut content_end = section_start + 1;
        for i in section_start + 1..section_end {
            if !lines[i].trim().is_empty() {
                content_end = i + 1;
            }
        }
        
        content_end
    }

    /// Backwards-compatible API: list notes without category
    pub fn list_notes(&self, date: NaiveDate) -> Result<Vec<String>, JourneyError> {
        self.list_notes_with_category(date, None)
    }

    /// Category-aware API used by the application
    pub fn list_notes_with_category(&self, date: NaiveDate, category: Option<&str>) -> Result<Vec<String>, JourneyError> {
        let note_path = self.get_note_path(date);
        
        if !note_path.exists() {
            return Ok(vec![]);
        }

        let content = fs::read_to_string(&note_path)?;
        let lines: Vec<&str> = content.lines().collect();
        let mut notes = Vec::new();
        
        // If section_header is configured, only list notes within that section
        if let Some(section_name) = self.config.get_section_header(category) {
            if let Some(section_start) = self.find_section(&content, section_name) {
                let section_end = self.find_section_end(&lines, section_start);
                
                // Only process lines within the section
                for line in &lines[section_start..section_end] {
                    let trimmed = line.trim();
                    // Check for bullet format
                    if trimmed.starts_with("- ") {
                        notes.push(line.to_string());
                    }
                    // Check for table format (but not table headers or separators)
                    else if trimmed.starts_with("|") && !trimmed.starts_with("|---") && trimmed.contains("|") {
                        // Skip if it looks like a table header or separator
                        if !self.is_table_header_line(trimmed) {
                            notes.push(line.to_string());
                        }
                    }
                }
            }
            // If section doesn't exist, return empty list
        } else {
            // No section configured, list all notes in the file
            for line in &lines {
                let trimmed = line.trim();
                // Check for bullet format
                if trimmed.starts_with("- ") {
                    notes.push(line.to_string());
                }
                // Check for table format (but not table headers or separators)
                else if trimmed.starts_with("|") && !trimmed.starts_with("|---") && trimmed.contains("|") {
                    // Skip if it looks like a table header or separator
                    if !self.is_table_header_line(trimmed) {
                        notes.push(line.to_string());
                    }
                }
            }
        }

        // Sort notes by time extracted from the line (ascending chronological)
        let note_format = self.config.list_type.as_ref().unwrap_or(&NoteFormat::Bullet);
        notes.sort_by(|a, b| {
            let ta = self.extract_time_from_note_line(a, note_format);
            let tb = self.extract_time_from_note_line(b, note_format);
            
            // If both have valid times, compare them
            if let (Some(time_a), Some(time_b)) = (ta, tb) {
                time_a.cmp(&time_b)
            }
            // If only one has a valid time, put the invalid one at the end
            else if ta.is_some() {
                std::cmp::Ordering::Less
            }
            else if tb.is_some() {
                std::cmp::Ordering::Greater
            }
            // If neither has a valid time, maintain original order
            else {
                std::cmp::Ordering::Equal
            }
        });

        Ok(notes)
    }

    pub fn get_editor_path(&self, date: NaiveDate) -> PathBuf {
        self.get_note_path(date)
    }

    /// Expand phrases in the content using the vault's phrase mappings
    fn expand_phrases(&self, content: &str) -> String {
        let mut result = content.to_string();
        
        // Sort phrases by length (longest first) to avoid partial replacements
        let mut phrases: Vec<_> = self.config.phrases.iter().collect();
        phrases.sort_by(|a, b| b.0.len().cmp(&a.0.len()));
        
        for (phrase, replacement) in phrases {
            // Replace all occurrences of the phrase with its replacement
            result = result.replace(phrase, replacement);
        }
        
        result
    }

    /// Format a custom file path using date components
    fn format_custom_path(&self, format: &str, date: NaiveDate) -> String {
        let year = date.year();
        let month = date.month();
        let day = date.day();
        let weekday = date.weekday();
        
        let mut result = format.to_string();
        
        // Replace year
        result = result.replace("{year}", &year.to_string());
        
        // Replace month with zero-padding
        result = result.replace("{month:02}", &format!("{:02}", month));
        result = result.replace("{month}", &month.to_string());
        
        // Replace date:MM (zero-padded month) for Journals plugin compatibility
        // Process double braces first to avoid conflicts
        result = result.replace("{{date:MM}}", &format!("{:02}", month));
        result = result.replace("{date:MM}", &format!("{:02}", month));
        
        // Replace date:y (two-digit year) for Journals plugin compatibility
        // Process double braces first to avoid conflicts
        result = result.replace("{{date:y}}", &format!("{:02}", year % 100));
        result = result.replace("{date:y}", &format!("{:02}", year % 100));
        
        // Replace day/date with zero-padding (after date:MM and date:y to avoid conflicts)
        result = result.replace("{day:02}", &format!("{:02}", day));
        result = result.replace("{date:02}", &format!("{:02}", day));
        result = result.replace("{day}", &day.to_string());
        result = result.replace("{date}", &day.to_string());
        
        // Replace weekday names (case-sensitive)
        result = result.replace("{Weekday}", &self.format_weekday(weekday, false));
        result = result.replace("{weekday}", &self.format_weekday(weekday, false).to_lowercase());
        result = result.replace("{Weekday_short}", &self.format_weekday(weekday, true));
        result = result.replace("{weekday_short}", &self.format_weekday(weekday, true).to_lowercase());
        
        // Replace month names (case-sensitive)
        result = result.replace("{Month}", &self.format_month(month, false));
        result = result.replace("{month_name}", &self.format_month(month, false).to_lowercase());
        result = result.replace("{Month_short}", &self.format_month(month, true));
        result = result.replace("{month_short}", &self.format_month(month, true).to_lowercase());
        
        result
    }
    
    /// Format weekday name (full or short)
    fn format_weekday(&self, weekday: Weekday, short: bool) -> String {
        match weekday {
            Weekday::Mon => if short { "Mon".to_string() } else { "Monday".to_string() },
            Weekday::Tue => if short { "Tue".to_string() } else { "Tuesday".to_string() },
            Weekday::Wed => if short { "Wed".to_string() } else { "Wednesday".to_string() },
            Weekday::Thu => if short { "Thu".to_string() } else { "Thursday".to_string() },
            Weekday::Fri => if short { "Fri".to_string() } else { "Friday".to_string() },
            Weekday::Sat => if short { "Sat".to_string() } else { "Saturday".to_string() },
            Weekday::Sun => if short { "Sun".to_string() } else { "Sunday".to_string() },
        }
    }
    
    /// Format month name (full or short)
    fn format_month(&self, month: u32, short: bool) -> String {
        match month {
            1 => if short { "Jan".to_string() } else { "January".to_string() },
            2 => if short { "Feb".to_string() } else { "February".to_string() },
            3 => if short { "Mar".to_string() } else { "March".to_string() },
            4 => if short { "Apr".to_string() } else { "April".to_string() },
            5 => if short { "May".to_string() } else { "May".to_string() },
            6 => if short { "Jun".to_string() } else { "June".to_string() },
            7 => if short { "Jul".to_string() } else { "July".to_string() },
            8 => if short { "Aug".to_string() } else { "August".to_string() },
            9 => if short { "Sep".to_string() } else { "September".to_string() },
            10 => if short { "Oct".to_string() } else { "October".to_string() },
            11 => if short { "Nov".to_string() } else { "November".to_string() },
            12 => if short { "Dec".to_string() } else { "December".to_string() },
            _ => "Unknown".to_string(),
        }
    }

    /// Format a note entry according to the specified format
    fn format_note_entry(&self, timestamp: &str, content: &str, format: &NoteFormat) -> String {
        match format {
            NoteFormat::Bullet => format!("- [{}] {}\n", timestamp, content),
            NoteFormat::Table => format!("| {} | {} |\n", timestamp, content),
        }
    }

    /// Extract NaiveTime from a note line according to format
    fn extract_time_from_note_line(&self, line: &str, format: &NoteFormat) -> Option<chrono::NaiveTime> {
        match format {
            NoteFormat::Bullet => {
                // "- [HH:MM[:SS]] content"
                let trimmed = line.trim_start();
                if let Some(rest) = trimmed.strip_prefix("- ") {
                    if let Some(after_bracket) = rest.strip_prefix('[') {
                        if let Some(closing) = after_bracket.find(']') {
                            let time_str = &after_bracket[..closing];
                            chrono::NaiveTime::parse_from_str(time_str, "%H:%M:%S")
                                .or_else(|_| chrono::NaiveTime::parse_from_str(time_str, "%H:%M")).ok()
                        } else { None }
                    } else { None }
                } else { None }
            }
            NoteFormat::Table => {
                // "| HH:MM | content |"
                let trimmed = line.trim();
                if trimmed.starts_with('|') {
                    let cols: Vec<&str> = trimmed.trim_matches('|').split('|').map(|s| s.trim()).collect();
                    if let Some(time_str) = cols.get(0) {
                        chrono::NaiveTime::parse_from_str(time_str, "%H:%M:%S")
                            .or_else(|_| chrono::NaiveTime::parse_from_str(time_str, "%H:%M")).ok()
                    } else { None }
                } else { None }
            }
        }
    }

    /// Find insertion index within a section (or whole file when section_start=0) to keep chronological order
    fn find_insertion_index(&self, lines: &[&str], section_start: usize, new_time_str: &str, format: &NoteFormat) -> usize {
        let section_end = if section_start == 0 { lines.len() } else { self.find_section_end(lines, section_start) };
        let new_time = chrono::NaiveTime::parse_from_str(new_time_str, "%H:%M:%S")
            .or_else(|_| chrono::NaiveTime::parse_from_str(new_time_str, "%H:%M")).ok();

        // Scan content lines and find first with time greater than new_time
        let mut content_end = section_start + 1;
        let mut insert_at = None;
        for i in section_start + 1..section_end {
            let trimmed = lines[i].trim();
            if !trimmed.is_empty() { content_end = i + 1; }

            let existing_time_opt = match format {
                NoteFormat::Bullet => {
                    if trimmed.starts_with("- ") {
                        if let Some(rest) = trimmed.strip_prefix("- ") {
                            if let Some(after_bracket) = rest.strip_prefix('[') {
                                if let Some(closing) = after_bracket.find(']') {
                                    let ts = &after_bracket[..closing];
                                    chrono::NaiveTime::parse_from_str(ts, "%H:%M:%S")
                                        .or_else(|_| chrono::NaiveTime::parse_from_str(ts, "%H:%M")).ok()
                                } else { None }
                            } else { None }
                        } else { None }
                    } else { None }
                }
                NoteFormat::Table => {
                    if trimmed.starts_with('|') && !trimmed.starts_with("|---") {
                        let cols: Vec<&str> = trimmed.trim_matches('|').split('|').map(|s| s.trim()).collect();
                        cols.get(0).and_then(|ts| chrono::NaiveTime::parse_from_str(ts, "%H:%M:%S")
                            .or_else(|_| chrono::NaiveTime::parse_from_str(ts, "%H:%M")).ok())
                    } else { None }
                }
            };

            if let (Some(new_t), Some(old_t)) = (new_time, existing_time_opt) {
                if new_t < old_t {
                    insert_at = Some(i);
                    break;
                }
            }
        }

        insert_at.unwrap_or(content_end)
    }

    /// Detect the current note format in the content
    pub fn detect_note_format(&self, content: &str) -> Option<NoteFormat> {
        let lines: Vec<&str> = content.lines().collect();
        
        // Look for bullet format
        let has_bullet_notes = lines.iter().any(|line| line.trim().starts_with("- "));
        
        // Look for table format
        let has_table_notes = lines.iter().any(|line| {
            let trimmed = line.trim();
            trimmed.starts_with("|") && trimmed.contains("|") && !trimmed.starts_with("|---")
        });
        
        if has_bullet_notes && !has_table_notes {
            Some(NoteFormat::Bullet)
        } else if has_table_notes && !has_bullet_notes {
            Some(NoteFormat::Table)
        } else {
            None // Mixed or no notes found
        }
    }

    /// Convert note format if needed
    fn convert_note_format_if_needed(&self, content: &str, target_format: &NoteFormat) -> Result<String, JourneyError> {
        let current_format = self.detect_note_format(content);
        
        // If no format detected or already matches target, clean up blank lines if it's table format
        if current_format.is_none() || current_format.as_ref() == Some(target_format) {
            if target_format == &NoteFormat::Table {
                return Ok(self.clean_table_blank_lines(content));
            }
            return Ok(content.to_string());
        }
        
        let lines: Vec<&str> = content.lines().collect();
        let mut converted_lines = Vec::new();
        let mut first_note_found = false;
        
        for line in lines {
            let trimmed = line.trim();
            
            // Convert bullet to table
            if target_format == &NoteFormat::Table && trimmed.starts_with("- ") {
                // Add table header before the first note
                if !first_note_found {
                    let (time_header, content_header) = self.get_table_headers();
                    converted_lines.push(format!("| {} | {} |", time_header, content_header));
                    converted_lines.push("|------|----------|".to_string());
                    first_note_found = true;
                }
                
                if let Some((timestamp, note_content)) = self.parse_bullet_note(trimmed) {
                    converted_lines.push(format!("| {} | {} |", timestamp, note_content));
                } else {
                    converted_lines.push(line.to_string());
                }
            }
            // Skip blank lines when converting bullet to table
            else if target_format == &NoteFormat::Table && trimmed.is_empty() && first_note_found {
                // Skip blank lines between bullet points when converting to table
                continue;
            }
            // Convert table to bullet
            else if target_format == &NoteFormat::Bullet && trimmed.starts_with("|") && !trimmed.starts_with("|---") && !trimmed.contains("Time") && !trimmed.contains("Content") {
                if let Some((timestamp, note_content)) = self.parse_table_note(trimmed) {
                    converted_lines.push(format!("- [{}] {}", timestamp, note_content));
                } else {
                    converted_lines.push(line.to_string());
                }
            }
            // Skip table headers and separators when converting to bullet
            else if target_format == &NoteFormat::Bullet && trimmed.starts_with("|") && (trimmed.contains("Time") || trimmed.contains("Content") || trimmed.starts_with("|---")) {
                // Skip table headers and separators
                continue;
            }
            // Skip blank lines when converting table to bullet
            else if target_format == &NoteFormat::Bullet && trimmed.is_empty() {
                // Skip blank lines between table rows when converting to bullet
                continue;
            }
            else {
                converted_lines.push(line.to_string());
            }
        }
        
        Ok(converted_lines.join("\n"))
    }

    /// Clean up blank lines in table format
    fn clean_table_blank_lines(&self, content: &str) -> String {
        let lines: Vec<&str> = content.lines().collect();
        let mut cleaned_lines = Vec::new();
        let mut in_table = false;
        let mut last_was_table_row = false;
        
        for line in lines {
            let trimmed = line.trim();
            
            // Check if this is a table row
            if trimmed.starts_with("|") && !trimmed.starts_with("|---") && !trimmed.contains("Time") && !trimmed.contains("Content") {
                in_table = true;
                last_was_table_row = true;
                cleaned_lines.push(line.to_string());
            }
            // Check if this is a table header or separator
            else if trimmed.starts_with("|") && (trimmed.contains("Time") || trimmed.contains("Content") || trimmed.starts_with("|---")) {
                in_table = true;
                last_was_table_row = false;
                cleaned_lines.push(line.to_string());
            }
            // Check if this is a blank line
            else if trimmed.is_empty() {
                // Only keep blank lines if we're not in a table or if it's the first blank line after a table
                if !in_table || !last_was_table_row {
                    cleaned_lines.push(line.to_string());
                }
                last_was_table_row = false;
            }
            // Non-table content
            else {
                in_table = false;
                last_was_table_row = false;
                cleaned_lines.push(line.to_string());
            }
        }
        
        cleaned_lines.join("\n")
    }

    /// Parse a bullet note to extract timestamp and content
    fn parse_bullet_note(&self, line: &str) -> Option<(String, String)> {
        // Format: "- [timestamp] content"
        if let Some(after_dash) = line.trim_start().strip_prefix("- ") {
            if let Some(after_bracket) = after_dash.strip_prefix('[') {
                if let Some(closing) = after_bracket.find(']') {
                    let timestamp = after_bracket[..closing].to_string();
                    let content = after_bracket[closing + 1..].trim().to_string();
                    return Some((timestamp, content));
                }
            }
        }
        None
    }

    /// Parse a table note to extract timestamp and content
    fn parse_table_note(&self, line: &str) -> Option<(String, String)> {
        // Format: "| timestamp | content |"
        let parts: Vec<&str> = line.split('|').collect();
        if parts.len() >= 3 {
            let timestamp = parts[1].trim().to_string();
            let content = parts[2].trim().to_string();
            return Some((timestamp, content));
        }
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::collections::HashMap;
    use tempfile::TempDir;

    fn create_test_vault() -> (Vault, TempDir) {
        let temp_dir = TempDir::new().unwrap();
        let vault_path = temp_dir.path().join("test_vault");
        fs::create_dir_all(&vault_path).unwrap();

        let config = VaultConfig {
            name: "test".to_string(),
            path: vault_path,
            locale: "en_US.UTF-8".to_string(),
            phrases: HashMap::new(),
            section_header: None,
            section_header_work: None,
            section_header_personal: None,
            section_header_health: None,
            section_header_meetings: None,
            table_headers: None,
            date_format: None,
            template_file: None,
            file_path_format: None,
            list_type: Some(NoteFormat::Table),
            section_name: None,
            weekly_format: None,
            monthly_format: None,
            quarterly_format: None,
            yearly_format: None,
            note_format: None,
        };

        let vault = Vault::new(config);
        (vault, temp_dir)
    }

    #[test]
    fn test_table_format_no_blank_lines() {
        let (vault, _temp_dir) = create_test_vault();
        let date = chrono::Local::now().date_naive();

        // Add first note
        vault.add_note("Test1", None).unwrap();

        // Add second note
        vault.add_note("Test2", None).unwrap();

        // Read the file content
        let note_path = vault.get_note_path(date);
        let content = fs::read_to_string(&note_path).unwrap();
        let lines: Vec<&str> = content.lines().collect();
        

        // Find the table section
        let mut table_start = None;
        for (i, line) in lines.iter().enumerate() {
            if line.starts_with("| Time | Content |") {
                table_start = Some(i);
                break;
            }
        }

        assert!(table_start.is_some(), "Table header not found");
        let start = table_start.unwrap();

        // Check that there are no blank lines between table rows
        let table_lines = &lines[start..];
        
        // Find the first data row (after header and separator)
        let mut data_start = None;
        for (i, line) in table_lines.iter().enumerate() {
            if line.starts_with("|") && !line.starts_with("|---") && !line.contains("Time") && !line.contains("Content") {
                data_start = Some(i);
                break;
            }
        }

        assert!(data_start.is_some(), "No data rows found in table");
        let data_start = data_start.unwrap();

        // Check that there are no blank lines between data rows
        let mut in_data_section = false;
        let mut blank_line_found = false;
        
        for line in &table_lines[data_start..] {
            if line.starts_with("|") && !line.starts_with("|---") && !line.contains("Time") && !line.contains("Content") {
                if in_data_section && blank_line_found {
                    panic!("Blank line found between table rows");
                }
                in_data_section = true;
                blank_line_found = false;
            } else if line.trim().is_empty() {
                if in_data_section {
                    blank_line_found = true;
                }
            }
        }
    }

    #[test]
    fn test_table_format_existing_file_no_blank_lines() {
        let (vault, _temp_dir) = create_test_vault();
        let date = chrono::Local::now().date_naive();

        // Create a file with some existing content first that has blank lines
        let note_path = vault.get_note_path(date);
        let existing_content = "---
date: 2025-10-26
---

| Time | Content |
|------|----------|
| 00:04:43 | Test1 |

| 00:05:28 | Test2 |
";
        fs::write(&note_path, existing_content).unwrap();

        // Add third note to existing file
        vault.add_note("Test3", None).unwrap();

        // Add fourth note to existing file
        vault.add_note("Test4", None).unwrap();

        // Read the file content
        let note_path = vault.get_note_path(date);
        let content = fs::read_to_string(&note_path).unwrap();
        let lines: Vec<&str> = content.lines().collect();
        

        // Find the table section
        let mut table_start = None;
        for (i, line) in lines.iter().enumerate() {
            if line.starts_with("| Time | Content |") {
                table_start = Some(i);
                break;
            }
        }

        assert!(table_start.is_some(), "Table header not found");
        let start = table_start.unwrap();

        // Check that there are no blank lines between table rows
        let table_lines = &lines[start..];
        
        // Find the first data row (after header and separator)
        let mut data_start = None;
        for (i, line) in table_lines.iter().enumerate() {
            if line.starts_with("|") && !line.starts_with("|---") && !line.contains("Time") && !line.contains("Content") {
                data_start = Some(i);
                break;
            }
        }

        assert!(data_start.is_some(), "No data rows found in table");
        let data_start = data_start.unwrap();

        // Check that there are no blank lines between data rows
        let mut in_data_section = false;
        let mut blank_line_found = false;
        
        for line in &table_lines[data_start..] {
            if line.starts_with("|") && !line.starts_with("|---") && !line.contains("Time") && !line.contains("Content") {
                if in_data_section && blank_line_found {
                    panic!("Blank line found between table rows in existing file");
                }
                in_data_section = true;
                blank_line_found = false;
            } else if line.trim().is_empty() {
                if in_data_section {
                    blank_line_found = true;
                }
            }
        }
    }

    #[test]
    fn test_bullet_format_no_header() {
        let (mut vault, _temp_dir) = create_test_vault();
        
        // Set bullet format
        vault.config.list_type = Some(NoteFormat::Bullet);
        
        let date = chrono::Local::now().date_naive();

        // Add first note - should not create a header
        vault.add_note("Test1", None).unwrap();

        // Read the file content
        let note_path = vault.get_note_path(date);
        let content = fs::read_to_string(&note_path).unwrap();
        
        
        // Should not contain table headers
        assert!(!content.contains("| Time | Content |"), "Bullet format should not have table headers");
        assert!(!content.contains("|------|----------|"), "Bullet format should not have table separators");
        
        // Should contain bullet format
        assert!(content.contains("- "), "Bullet format should contain bullet points");
    }

    #[test]
    fn test_table_to_bullet_conversion_removes_header() {
        let (mut vault, _temp_dir) = create_test_vault();
        
        // Start with table format
        vault.config.list_type = Some(NoteFormat::Table);
        
        let date = chrono::Local::now().date_naive();

        // Add first note to create table format
        vault.add_note("Test1", None).unwrap();

        // Switch to bullet format
        vault.config.list_type = Some(NoteFormat::Bullet);

        // Add second note - should convert to bullet format without headers
        vault.add_note("Test2", None).unwrap();

        // Read the file content
        let note_path = vault.get_note_path(date);
        let content = fs::read_to_string(&note_path).unwrap();
        
        
        // Should not contain table headers
        assert!(!content.contains("| Time | Content |"), "Converted bullet format should not have table headers");
        assert!(!content.contains("|------|----------|"), "Converted bullet format should not have table separators");
        
        // Should contain bullet format
        assert!(content.contains("- "), "Converted bullet format should contain bullet points");
    }

    #[test]
    fn test_bullet_to_table_conversion_no_blank_lines() {
        let (mut vault, _temp_dir) = create_test_vault();
        
        // Start with bullet format
        vault.config.list_type = Some(NoteFormat::Bullet);
        
        let date = chrono::Local::now().date_naive();

        // Create a file with bullet points that have blank lines between them
        let note_path = vault.get_note_path(date);
        let existing_content = "---
date: 2025-10-26
---

- 00:04:43 Test1

- 00:05:28 Test2
";
        fs::write(&note_path, existing_content).unwrap();

        // Switch to table format
        vault.config.list_type = Some(NoteFormat::Table);

        // Add third note - should convert to table format without blank lines
        vault.add_note("Test3", None).unwrap();

        // Read the file content
        let note_path = vault.get_note_path(date);
        let content = fs::read_to_string(&note_path).unwrap();
        
        
        // Should not contain blank lines between table rows
        let lines: Vec<&str> = content.lines().collect();
        let mut in_table = false;
        let mut blank_line_found = false;
        
        for line in &lines {
            if line.starts_with("|") && !line.starts_with("|---") && !line.contains("Time") && !line.contains("Content") {
                if in_table && blank_line_found {
                    panic!("Blank line found between table rows in bullet-to-table conversion");
                }
                in_table = true;
                blank_line_found = false;
            } else if line.trim().is_empty() {
                if in_table {
                    blank_line_found = true;
                }
            }
        }
    }

    #[test]
    fn test_table_to_bullet_conversion_no_blank_lines() {
        let (mut vault, _temp_dir) = create_test_vault();
        
        // Start with table format
        vault.config.list_type = Some(NoteFormat::Table);
        
        let date = chrono::Local::now().date_naive();

        // Create a file with table rows that have blank lines between them
        let note_path = vault.get_note_path(date);
        let existing_content = "---
date: 2025-10-26
---

| Time | Content |
|------|----------|
| 00:04:43 | Test1 |

| 00:05:28 | Test2 |
";
        fs::write(&note_path, existing_content).unwrap();

        // Switch to bullet format
        vault.config.list_type = Some(NoteFormat::Bullet);

        // Add third note - should convert to bullet format without blank lines
        vault.add_note("Test3", None).unwrap();

        // Read the file content
        let note_path = vault.get_note_path(date);
        let content = fs::read_to_string(&note_path).unwrap();
        
        
        // Should not contain blank lines between bullet points
        let lines: Vec<&str> = content.lines().collect();
        let mut in_bullet_section = false;
        let mut blank_line_found = false;
        
        for line in &lines {
            if line.starts_with("- ") {
                if in_bullet_section && blank_line_found {
                    panic!("Blank line found between bullet points in table-to-bullet conversion");
                }
                in_bullet_section = true;
                blank_line_found = false;
            } else if line.trim().is_empty() {
                if in_bullet_section {
                    blank_line_found = true;
                }
            }
        }
    }

    #[test]
    fn test_adding_note_to_existing_file_no_blank_lines() {
        let (vault, _temp_dir) = create_test_vault();
        let date = chrono::Local::now().date_naive();

        // Create a file with some existing content that has blank lines between table rows
        let note_path = vault.get_note_path(date);
        let existing_content = "---
date: 2025-10-26
---

| Time | Content |
|------|----------|
| 00:04:43 | Test1 |

| 00:05:28 | Test2 |
";
        fs::write(&note_path, existing_content).unwrap();

        // Add third note to existing file - should not create blank lines
        vault.add_note("Test3", None).unwrap();

        // Add fourth note to existing file - should not create blank lines
        vault.add_note("Test4", None).unwrap();

        // Read the file content
        let note_path = vault.get_note_path(date);
        let content = fs::read_to_string(&note_path).unwrap();
        
        // Debug: print the actual content
        println!("Adding notes to existing file content:");
        println!("{}", content);
        
        // Should not contain blank lines between notes
        let lines: Vec<&str> = content.lines().collect();
        let mut in_notes_section = false;
        let mut blank_line_found = false;
        
        for line in &lines {
            if line.starts_with("|") && !line.starts_with("|---") && !line.contains("Time") && !line.contains("Content") {
                if in_notes_section && blank_line_found {
                    panic!("Blank line found between notes when adding to existing file");
                }
                in_notes_section = true;
                blank_line_found = false;
            } else if line.trim().is_empty() {
                if in_notes_section {
                    blank_line_found = true;
                }
            }
        }
    }

    #[test]
    fn test_append_note_without_conversion_no_blank_lines() {
        let (vault, _temp_dir) = create_test_vault();
        let date = chrono::Local::now().date_naive();

        // Create a file with some existing content
        let note_path = vault.get_note_path(date);
        let existing_content = "---
date: 2025-10-26
---

| Time | Content |
|------|----------|
| 00:04:43 | Test1 |
| 00:05:28 | Test2 |
";
        fs::write(&note_path, existing_content).unwrap();

        // Add third note to existing file - this should trigger the append logic
        vault.add_note("Test3", None).unwrap();

        // Read the file content
        let note_path = vault.get_note_path(date);
        let content = fs::read_to_string(&note_path).unwrap();
        
        
        // Should not contain blank lines between notes
        let lines: Vec<&str> = content.lines().collect();
        let mut in_notes_section = false;
        let mut blank_line_found = false;
        
        for line in &lines {
            if line.starts_with("|") && !line.starts_with("|---") && !line.contains("Time") && !line.contains("Content") {
                if in_notes_section && blank_line_found {
                    panic!("Blank line found between notes when appending to existing file");
                }
                in_notes_section = true;
                blank_line_found = false;
            } else if line.trim().is_empty() {
                if in_notes_section {
                    blank_line_found = true;
                }
            }
        }
    }

    #[test]
    fn test_section_insertion_no_blank_lines() {
        let (mut vault, _temp_dir) = create_test_vault();
        
        // Set up a section header
        vault.config.section_header = Some("Test Section".to_string());
        
        let date = chrono::Local::now().date_naive();

        // Create a file with a section that has some content
        let note_path = vault.get_note_path(date);
        let existing_content = "---
date: 2025-10-26
---

# Test Section

| Time | Content |
|------|----------|
| 00:04:43 | Test1 |
| 00:05:28 | Test2 |

# Another Section
Some other content
";
        fs::write(&note_path, existing_content).unwrap();

        // Add third note to the section - should not create blank lines
        vault.add_note("Test3", None).unwrap();

        // Read the file content
        let note_path = vault.get_note_path(date);
        let content = fs::read_to_string(&note_path).unwrap();
        
        // Debug: print the actual content
        println!("Section insertion content:");
        println!("{}", content);
        
        // Should not contain blank lines between notes in the section
        let lines: Vec<&str> = content.lines().collect();
        let mut in_section = false;
        let mut blank_line_found = false;
        
        for line in &lines {
            if *line == "# Test Section" {
                in_section = true;
                blank_line_found = false;
                continue;
            }
            if *line == "# Another Section" {
                in_section = false;
                break;
            }
            if in_section && line.starts_with("|") && !line.starts_with("|---") && !line.contains("Time") && !line.contains("Content") {
                if blank_line_found {
                    panic!("Blank line found between notes in section");
                }
                blank_line_found = false;
            } else if in_section && (line.starts_with("|---") || line.contains("Time") || line.contains("Content")) {
                // Reset on table header/separator lines
                blank_line_found = false;
            } else if in_section && line.trim().is_empty() {
                blank_line_found = true;
            }
        }
    }

    #[test]
    fn test_header_flag_functionality() {
        let (vault, _temp_dir) = create_test_vault();
        let date = chrono::Local::now().date_naive();

        // Add some notes in table format
        vault.add_note("Test1", None).unwrap();
        vault.add_note("Test2", None).unwrap();

        // Test listing without header flag
        let notes = vault.list_notes(date).unwrap();
        assert!(!notes.is_empty(), "Should have notes");
        
        // Verify that table headers are not included in the notes
        for note in &notes {
            assert!(!note.contains("Time") && !note.contains("Content"), 
                "Table headers should not be included in notes: {}", note);
        }
    }

    #[test]
    fn test_table_format_with_section_no_blank_lines() {
        let (mut vault, _temp_dir) = create_test_vault();
        
        // Set up a section header
        vault.config.section_header = Some("Test Section".to_string());
        
        let date = chrono::Local::now().date_naive();

        // Add first note
        vault.add_note("Test1", None).unwrap();

        // Add second note
        vault.add_note("Test2", None).unwrap();

        // Read the file content
        let note_path = vault.get_note_path(date);
        let content = fs::read_to_string(&note_path).unwrap();
        let lines: Vec<&str> = content.lines().collect();

        // Find the section
        let mut section_start = None;
        for (i, line) in lines.iter().enumerate() {
            if *line == "# Test Section" {
                section_start = Some(i);
                break;
            }
        }

        assert!(section_start.is_some(), "Section header not found");
        let start = section_start.unwrap();

        // Check that there are no blank lines between the section header and table
        let section_lines = &lines[start..];
        
        // The line after the section header should be the table header
        assert!(section_lines.len() > 1, "No content after section header");
        
        // Find the table header after the section
        let mut table_header_found = false;
        for line in section_lines {
            if line.starts_with("| Time | Content |") {
                table_header_found = true;
                break;
            }
        }
        assert!(table_header_found, "Table header not found after section");

        // Check for blank lines between table rows
        let mut in_table = false;
        let mut blank_line_found = false;
        
        for line in section_lines {
            if line.starts_with("|") && !line.starts_with("|---") && !line.contains("Time") && !line.contains("Content") {
                if in_table && blank_line_found {
                    panic!("Blank line found between table rows in section");
                }
                in_table = true;
                blank_line_found = false;
            } else if line.trim().is_empty() {
                if in_table {
                    blank_line_found = true;
                }
            }
        }
    }
}