diaryx_core 1.4.4

Core library for Diaryx - a tool to manage markdown files with YAML frontmatter
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
//! Entry operations module.
//!
//! This module provides functionality for working with individual entries:
//! - Frontmatter manipulation (get, set, remove properties)
//! - Content operations (get, set, append, prepend)
//! - Attachment management
//!
//! # Migration Note
//!
//! `DiaryxAppSync` is deprecated. Use [`DiaryxApp`] with `AsyncFileSystem`
//! for all new code. The sync implementation will be removed in a future version.
//!
//! ```ignore
//! use diaryx_core::entry::DiaryxApp;
//! use diaryx_core::fs::{RealFileSystem, SyncToAsyncFs};
//! use futures_lite::future::block_on;
//!
//! // Use async-first API even in sync contexts
//! let fs = SyncToAsyncFs::new(RealFileSystem);
//! let app = DiaryxApp::new(fs);
//!
//! block_on(app.get_content("note.md"))?;
//! ```

mod helpers;

// Re-export helper functions
pub use helpers::{
    apply_filename_style, extract_first_line_h1, has_non_portable_chars, prettify_filename,
    sanitize_filename, slugify, slugify_title, sync_h1_in_body,
};

use crate::date;
use crate::error::{DiaryxError, Result};
use crate::fs::{AsyncFileSystem, FileSystem};
use crate::link_parser;
use crate::yaml_value::YamlValue;
use indexmap::IndexMap;
use std::path::{Path, PathBuf};

/// Async-first Diaryx entry operations.
///
/// This is the main entry API going forward.
pub struct DiaryxApp<FS: AsyncFileSystem> {
    fs: FS,
}

/// Legacy synchronous Diaryx entry operations.
///
/// This preserves the prior `FileSystem`-based implementation during the async refactor.
/// Prefer [`DiaryxApp`].
pub struct DiaryxAppSync<FS: FileSystem> {
    fs: FS,
}

impl<FS: AsyncFileSystem> DiaryxApp<FS> {
    /// DiaryxApp constructor
    pub fn new(fs: FS) -> Self {
        Self { fs }
    }

    /// Access the underlying filesystem.
    pub fn fs(&self) -> &FS {
        &self.fs
    }

    /// Create a new entry.
    pub async fn create_entry(&self, path: &str) -> Result<()> {
        let content = format!("---\ntitle: {}\n---\n\n# {}\n\n", path, path);
        self.fs
            .create_new(std::path::Path::new(path), &content)
            .await
            .map_err(|e| DiaryxError::FileWrite {
                path: PathBuf::from(path),
                source: e,
            })?;
        Ok(())
    }

    /// Parses a markdown file and extracts frontmatter and body.
    /// Returns an error if no frontmatter is found.
    async fn parse_file(&self, path: &str) -> Result<(IndexMap<String, YamlValue>, String)> {
        let path_buf = PathBuf::from(path);
        let content = self
            .fs
            .read_to_string(std::path::Path::new(path))
            .await
            .map_err(|e| DiaryxError::FileRead {
                path: path_buf.clone(),
                source: e,
            })?;

        // Check if content starts with frontmatter delimiter
        if !content.starts_with("---\n") && !content.starts_with("---\r\n") {
            return Err(DiaryxError::NoFrontmatter(path_buf));
        }

        // Find the closing delimiter
        let rest = &content[4..]; // Skip first "---\n"
        let end_idx = rest
            .find("\n---\n")
            .or_else(|| rest.find("\n---\r\n"))
            .ok_or_else(|| DiaryxError::NoFrontmatter(path_buf.clone()))?;

        let frontmatter_str = &rest[..end_idx];
        let body = &rest[end_idx + 5..]; // Skip "\n---\n"

        // Parse YAML frontmatter into IndexMap to preserve order
        let frontmatter: IndexMap<String, YamlValue> = serde_yaml::from_str(frontmatter_str)?;

        Ok((frontmatter, body.to_string()))
    }

    /// Parses a markdown file, creating empty frontmatter if none exists.
    /// Creates the file if it doesn't exist.
    /// Use this for operations that should create frontmatter when missing (like set).
    async fn parse_file_or_create_frontmatter(
        &self,
        path: &str,
    ) -> Result<(IndexMap<String, YamlValue>, String)> {
        let path_buf = PathBuf::from(path);

        // Try to read the file, if it doesn't exist, return empty frontmatter and body
        let content = match self.fs.read_to_string(std::path::Path::new(path)).await {
            Ok(c) => c,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                // File doesn't exist - return empty frontmatter and body
                // The file will be created when reconstruct_file is called
                return Ok((IndexMap::new(), String::new()));
            }
            Err(e) => {
                return Err(DiaryxError::FileRead {
                    path: path_buf,
                    source: e,
                });
            }
        };

        // Check if content starts with frontmatter delimiter
        if !content.starts_with("---\n") && !content.starts_with("---\r\n") {
            // No frontmatter - return empty frontmatter and entire content as body
            return Ok((IndexMap::new(), content));
        }

        // Find the closing delimiter
        let rest = &content[4..]; // Skip first "---\n"
        let end_idx = rest.find("\n---\n").or_else(|| rest.find("\n---\r\n"));

        match end_idx {
            Some(idx) => {
                let frontmatter_str = &rest[..idx];
                let body = &rest[idx + 5..]; // Skip "\n---\n"

                // Parse YAML frontmatter into IndexMap to preserve order
                let frontmatter: IndexMap<String, YamlValue> =
                    serde_yaml::from_str(frontmatter_str)?;

                Ok((frontmatter, body.to_string()))
            }
            None => {
                // Malformed frontmatter (no closing delimiter) - treat as no frontmatter
                Ok((IndexMap::new(), content))
            }
        }
    }

    /// Reconstructs a markdown file with updated frontmatter.
    async fn reconstruct_file(
        &self,
        path: &str,
        frontmatter: &IndexMap<String, YamlValue>,
        body: &str,
    ) -> Result<()> {
        let yaml_str = serde_yaml::to_string(frontmatter)?;
        let content = format!("---\n{}---\n{}", yaml_str, body);
        self.fs
            .write_file(std::path::Path::new(path), &content)
            .await
            .map_err(|e| DiaryxError::FileWrite {
                path: PathBuf::from(path),
                source: e,
            })?;
        Ok(())
    }

    // ==================== Frontmatter Methods ====================

    /// Adds or updates a frontmatter property.
    /// Creates frontmatter if none exists.
    pub async fn set_frontmatter_property(
        &self,
        path: &str,
        key: &str,
        value: YamlValue,
    ) -> Result<()> {
        let (mut frontmatter, body) = self.parse_file_or_create_frontmatter(path).await?;
        frontmatter.insert(key.to_string(), value);
        self.reconstruct_file(path, &frontmatter, &body).await
    }

    /// Removes a frontmatter property.
    /// Does nothing if no frontmatter exists or key is not found.
    pub async fn remove_frontmatter_property(&self, path: &str, key: &str) -> Result<()> {
        match self.parse_file(path).await {
            Ok((mut frontmatter, body)) => {
                frontmatter.shift_remove(key);
                self.reconstruct_file(path, &frontmatter, &body).await
            }
            Err(DiaryxError::NoFrontmatter(_)) => Ok(()), // No frontmatter, nothing to remove
            Err(e) => Err(e),
        }
    }

    /// Renames a frontmatter property key.
    /// Returns Ok(true) if the key was found and renamed, Ok(false) if key was not found or no frontmatter.
    pub async fn rename_frontmatter_property(
        &self,
        path: &str,
        old_key: &str,
        new_key: &str,
    ) -> Result<bool> {
        let (frontmatter, body) = match self.parse_file(path).await {
            Ok(result) => result,
            Err(DiaryxError::NoFrontmatter(_)) => return Ok(false), // No frontmatter, key not found
            Err(e) => return Err(e),
        };

        if !frontmatter.contains_key(old_key) {
            return Ok(false);
        }

        // Rebuild the map, replacing old_key with new_key at the same position
        let mut result: IndexMap<String, YamlValue> = IndexMap::new();
        for (k, v) in frontmatter {
            if k == old_key {
                result.insert(new_key.to_string(), v);
            } else {
                result.insert(k, v);
            }
        }

        self.reconstruct_file(path, &result, &body).await?;
        Ok(true)
    }

    /// Gets a frontmatter property value.
    /// Returns Ok(None) if no frontmatter exists or key is not found.
    pub async fn get_frontmatter_property(
        &self,
        path: &str,
        key: &str,
    ) -> Result<Option<YamlValue>> {
        match self.parse_file(path).await {
            Ok((frontmatter, _)) => Ok(frontmatter.get(key).cloned()),
            Err(DiaryxError::NoFrontmatter(_)) => Ok(None), // No frontmatter, key not found
            Err(e) => Err(e),
        }
    }

    /// Gets all frontmatter properties.
    /// Returns empty map if no frontmatter exists.
    pub async fn get_all_frontmatter(&self, path: &str) -> Result<IndexMap<String, YamlValue>> {
        match self.parse_file(path).await {
            Ok((frontmatter, _)) => Ok(frontmatter),
            Err(DiaryxError::NoFrontmatter(_)) => Ok(IndexMap::new()), // No frontmatter, return empty
            Err(e) => Err(e),
        }
    }

    // ==================== Content Methods ====================

    /// Get the content (body) of a file, excluding frontmatter.
    pub async fn get_content(&self, path: &str) -> Result<String> {
        let (_, body) = self.parse_file_or_create_frontmatter(path).await?;
        Ok(body)
    }

    /// Set the content (body) of a file, preserving frontmatter.
    /// Creates frontmatter if none exists.
    pub async fn set_content(&self, path: &str, content: &str) -> Result<()> {
        let (frontmatter, _) = self.parse_file_or_create_frontmatter(path).await?;
        self.reconstruct_file(path, &frontmatter, content).await
    }

    /// Clear the content (body) of a file, preserving frontmatter.
    pub async fn clear_content(&self, path: &str) -> Result<()> {
        self.set_content(path, "").await
    }

    /// Update the 'updated' frontmatter property with the current timestamp (RFC 3339 format).
    /// Creates frontmatter if none exists.
    pub async fn touch_updated(&self, path: &str) -> Result<()> {
        let timestamp = date::current_local_timestamp_rfc3339();
        self.set_frontmatter_property(path, "updated", YamlValue::String(timestamp))
            .await
    }

    /// Save content and optionally update the 'updated' timestamp.
    /// When `auto_update_timestamp` is true (the default), this combines set_content and touch_updated.
    pub async fn save_content(&self, path: &str, content: &str) -> Result<()> {
        self.save_content_with_options(path, content, true).await
    }

    /// Save content with explicit control over timestamp updating.
    pub async fn save_content_with_options(
        &self,
        path: &str,
        content: &str,
        auto_update_timestamp: bool,
    ) -> Result<()> {
        self.set_content(path, content).await?;
        if auto_update_timestamp {
            self.touch_updated(path).await?;
        }
        Ok(())
    }

    /// Append content to the end of a file's body.
    pub async fn append_content(&self, path: &str, content: &str) -> Result<()> {
        let (frontmatter, body) = self.parse_file_or_create_frontmatter(path).await?;
        let new_body = if body.is_empty() {
            content.to_string()
        } else if body.ends_with('\n') {
            format!("{}{}", body, content)
        } else {
            format!("{}\n{}", body, content)
        };
        self.reconstruct_file(path, &frontmatter, &new_body).await
    }

    /// Prepend content to the beginning of a file's body.
    pub async fn prepend_content(&self, path: &str, content: &str) -> Result<()> {
        let (frontmatter, body) = self.parse_file_or_create_frontmatter(path).await?;
        let new_body = if body.is_empty() {
            content.to_string()
        } else if content.ends_with('\n') {
            format!("{}{}", content, body)
        } else {
            format!("{}\n{}", content, body)
        };
        self.reconstruct_file(path, &frontmatter, &new_body).await
    }

    // ==================== Attachment Methods ====================

    /// Add an attachment path to the entry's attachments list.
    /// Creates the attachments property if it doesn't exist.
    pub async fn add_attachment(&self, path: &str, attachment_path: &str) -> Result<()> {
        let (mut frontmatter, body) = self.parse_file_or_create_frontmatter(path).await?;
        let parsed_target = link_parser::parse_link(attachment_path);
        let target_canonical = link_parser::to_canonical(&parsed_target, Path::new(path));

        let attachments = frontmatter
            .entry("attachments".to_string())
            .or_insert(YamlValue::Sequence(vec![]));

        if let YamlValue::Sequence(list) = attachments {
            let exists = list.iter().any(|item| {
                if let YamlValue::String(existing) = item {
                    let parsed_existing = link_parser::parse_link(existing);
                    return link_parser::to_canonical(&parsed_existing, Path::new(path))
                        == target_canonical;
                }
                false
            });

            if !exists {
                list.push(YamlValue::String(attachment_path.to_string()));
            }
        }

        self.reconstruct_file(path, &frontmatter, &body).await
    }

    /// Remove an attachment path from the entry's attachments list.
    /// Does nothing if the attachment isn't found.
    pub async fn remove_attachment(&self, path: &str, attachment_path: &str) -> Result<()> {
        let (mut frontmatter, body) = match self.parse_file(path).await {
            Ok(result) => result,
            Err(DiaryxError::NoFrontmatter(_)) => return Ok(()),
            Err(e) => return Err(e),
        };
        let parsed_target = link_parser::parse_link(attachment_path);
        let target_canonical = link_parser::to_canonical(&parsed_target, Path::new(path));

        if let Some(YamlValue::Sequence(list)) = frontmatter.get_mut("attachments") {
            list.retain(|item| {
                if let YamlValue::String(s) = item {
                    let parsed_existing = link_parser::parse_link(s);
                    link_parser::to_canonical(&parsed_existing, Path::new(path)) != target_canonical
                } else {
                    true
                }
            });

            // Remove empty attachments array
            if list.is_empty() {
                frontmatter.shift_remove("attachments");
            }
        }

        self.reconstruct_file(path, &frontmatter, &body).await
    }

    /// Get the list of attachments directly declared in this entry.
    pub async fn get_attachments(&self, path: &str) -> Result<Vec<String>> {
        let (frontmatter, _) = match self.parse_file(path).await {
            Ok(result) => result,
            Err(DiaryxError::NoFrontmatter(_)) => return Ok(vec![]),
            Err(e) => return Err(e),
        };

        match frontmatter.get("attachments") {
            Some(YamlValue::Sequence(list)) => Ok(list
                .iter()
                .filter_map(|v| {
                    if let YamlValue::String(s) = v {
                        Some(s.clone())
                    } else {
                        None
                    }
                })
                .collect()),
            _ => Ok(vec![]),
        }
    }
}

impl<FS: FileSystem> DiaryxAppSync<FS> {
    /// DiaryxAppSync constructor
    pub fn new(fs: FS) -> Self {
        Self { fs }
    }

    /// Access the underlying filesystem.
    pub fn fs(&self) -> &FS {
        &self.fs
    }

    /// Create a new entry
    pub fn create_entry(&self, path: &str) -> Result<()> {
        let content = format!("---\ntitle: {}\n---\n\n# {}\n\n", path, path);
        self.fs.create_new(std::path::Path::new(path), &content)?;
        Ok(())
    }

    /// Parses a markdown file and extracts frontmatter and body
    /// Returns an error if no frontmatter is found
    fn parse_file(&self, path: &str) -> Result<(IndexMap<String, YamlValue>, String)> {
        let path_buf = PathBuf::from(path);
        let content = self
            .fs
            .read_to_string(std::path::Path::new(path))
            .map_err(|e| DiaryxError::FileRead {
                path: path_buf.clone(),
                source: e,
            })?;

        // Check if content starts with frontmatter delimiter
        if !content.starts_with("---\n") && !content.starts_with("---\r\n") {
            return Err(DiaryxError::NoFrontmatter(path_buf));
        }

        // Find the closing delimiter
        let rest = &content[4..]; // Skip first "---\n"
        let end_idx = rest
            .find("\n---\n")
            .or_else(|| rest.find("\n---\r\n"))
            .ok_or_else(|| DiaryxError::NoFrontmatter(path_buf.clone()))?;

        let frontmatter_str = &rest[..end_idx];
        let body = &rest[end_idx + 5..]; // Skip "\n---\n"

        // Parse YAML frontmatter into IndexMap to preserve order
        let frontmatter: IndexMap<String, YamlValue> = serde_yaml::from_str(frontmatter_str)?;

        Ok((frontmatter, body.to_string()))
    }

    /// Parses a markdown file, creating empty frontmatter if none exists.
    /// Creates the file if it doesn't exist.
    /// Use this for operations that should create frontmatter when missing (like set).
    fn parse_file_or_create_frontmatter(
        &self,
        path: &str,
    ) -> Result<(IndexMap<String, YamlValue>, String)> {
        let path_buf = PathBuf::from(path);

        // Try to read the file, if it doesn't exist, return empty frontmatter and body
        let content = match self.fs.read_to_string(std::path::Path::new(path)) {
            Ok(c) => c,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                // File doesn't exist - return empty frontmatter and body
                // The file will be created when reconstruct_file is called
                return Ok((IndexMap::new(), String::new()));
            }
            Err(e) => {
                return Err(DiaryxError::FileRead {
                    path: path_buf,
                    source: e,
                });
            }
        };

        // Check if content starts with frontmatter delimiter
        if !content.starts_with("---\n") && !content.starts_with("---\r\n") {
            // No frontmatter - return empty frontmatter and entire content as body
            return Ok((IndexMap::new(), content));
        }

        // Find the closing delimiter
        let rest = &content[4..]; // Skip first "---\n"
        let end_idx = rest.find("\n---\n").or_else(|| rest.find("\n---\r\n"));

        match end_idx {
            Some(idx) => {
                let frontmatter_str = &rest[..idx];
                let body = &rest[idx + 5..]; // Skip "\n---\n"

                // Parse YAML frontmatter into IndexMap to preserve order
                let frontmatter: IndexMap<String, YamlValue> =
                    serde_yaml::from_str(frontmatter_str)?;

                Ok((frontmatter, body.to_string()))
            }
            None => {
                // Malformed frontmatter (no closing delimiter) - treat as no frontmatter
                Ok((IndexMap::new(), content))
            }
        }
    }

    /// Reconstructs a markdown file with updated frontmatter.
    fn reconstruct_file(
        &self,
        path: &str,
        frontmatter: &IndexMap<String, YamlValue>,
        body: &str,
    ) -> Result<()> {
        let yaml_str = serde_yaml::to_string(frontmatter)?;
        let content = format!("---\n{}---\n{}", yaml_str, body);
        self.fs
            .write_file(std::path::Path::new(path), &content)
            .map_err(|e| DiaryxError::FileWrite {
                path: PathBuf::from(path),
                source: e,
            })?;
        Ok(())
    }

    /// Adds or updates a frontmatter property.
    /// Creates frontmatter if none exists.
    pub fn set_frontmatter_property(&self, path: &str, key: &str, value: YamlValue) -> Result<()> {
        let (mut frontmatter, body) = self.parse_file_or_create_frontmatter(path)?;
        frontmatter.insert(key.to_string(), value);
        self.reconstruct_file(path, &frontmatter, &body)
    }

    /// Removes a frontmatter property.
    /// Does nothing if no frontmatter exists or key is not found.
    pub fn remove_frontmatter_property(&self, path: &str, key: &str) -> Result<()> {
        match self.parse_file(path) {
            Ok((mut frontmatter, body)) => {
                frontmatter.shift_remove(key);
                self.reconstruct_file(path, &frontmatter, &body)
            }
            Err(DiaryxError::NoFrontmatter(_)) => Ok(()), // No frontmatter, nothing to remove
            Err(e) => Err(e),
        }
    }

    /// Renames a frontmatter property key.
    /// Returns Ok(true) if the key was found and renamed, Ok(false) if key was not found or no frontmatter.
    pub fn rename_frontmatter_property(
        &self,
        path: &str,
        old_key: &str,
        new_key: &str,
    ) -> Result<bool> {
        let (frontmatter, body) = match self.parse_file(path) {
            Ok(result) => result,
            Err(DiaryxError::NoFrontmatter(_)) => return Ok(false), // No frontmatter, key not found
            Err(e) => return Err(e),
        };

        if !frontmatter.contains_key(old_key) {
            return Ok(false);
        }

        // Rebuild the map, replacing old_key with new_key at the same position
        let mut result: IndexMap<String, YamlValue> = IndexMap::new();
        for (k, v) in frontmatter {
            if k == old_key {
                result.insert(new_key.to_string(), v);
            } else {
                result.insert(k, v);
            }
        }

        self.reconstruct_file(path, &result, &body)?;
        Ok(true)
    }

    /// Get body content (excluding frontmatter). If no frontmatter exists, returns entire file.
    pub fn get_content(&self, path: &str) -> Result<String> {
        match self.parse_file_or_create_frontmatter(path) {
            Ok((_frontmatter, body)) => Ok(body),
            Err(e) => Err(e),
        }
    }

    /// Set body content, preserving (or creating) frontmatter.
    pub fn set_content(&self, path: &str, content: &str) -> Result<()> {
        let (frontmatter, _old_body) = self.parse_file_or_create_frontmatter(path)?;
        self.reconstruct_file(path, &frontmatter, content)
    }

    /// Clear file body content.
    pub fn clear_content(&self, path: &str) -> Result<()> {
        self.set_content(path, "")
    }

    /// Append content to end of body.
    pub fn append_content(&self, path: &str, content: &str) -> Result<()> {
        let (frontmatter, mut body) = self.parse_file_or_create_frontmatter(path)?;

        if body.is_empty() {
            body = content.to_string();
        } else if body.ends_with('\n') {
            body.push_str(content);
        } else {
            body.push('\n');
            body.push_str(content);
        }

        self.reconstruct_file(path, &frontmatter, &body)
    }

    /// Prepend content to start of body.
    pub fn prepend_content(&self, path: &str, content: &str) -> Result<()> {
        let (frontmatter, body) = self.parse_file_or_create_frontmatter(path)?;
        let new_body = if body.is_empty() {
            content.to_string()
        } else if content.ends_with('\n') {
            format!("{}{}", content, body)
        } else {
            format!("{}\n{}", content, body)
        };
        self.reconstruct_file(path, &frontmatter, &new_body)
    }

    /// Gets a frontmatter property value.
    /// Returns Ok(None) if no frontmatter exists or key is not found.
    pub fn get_frontmatter_property(&self, path: &str, key: &str) -> Result<Option<YamlValue>> {
        match self.parse_file(path) {
            Ok((frontmatter, _)) => Ok(frontmatter.get(key).cloned()),
            Err(DiaryxError::NoFrontmatter(_)) => Ok(None), // No frontmatter, key not found
            Err(e) => Err(e),
        }
    }

    /// Gets all frontmatter properties.
    /// Returns empty map if no frontmatter exists.
    pub fn get_all_frontmatter(&self, path: &str) -> Result<IndexMap<String, YamlValue>> {
        match self.parse_file(path) {
            Ok((frontmatter, _)) => Ok(frontmatter),
            Err(DiaryxError::NoFrontmatter(_)) => Ok(IndexMap::new()), // No frontmatter, return empty
            Err(e) => Err(e),
        }
    }

    // ==================== Attachment Methods ====================

    /// Add an attachment path to the entry's attachments list.
    /// Creates the attachments property if it doesn't exist.
    pub fn add_attachment(&self, path: &str, attachment_path: &str) -> Result<()> {
        let (mut frontmatter, body) = self.parse_file_or_create_frontmatter(path)?;
        let parsed_target = link_parser::parse_link(attachment_path);
        let target_canonical = link_parser::to_canonical(&parsed_target, Path::new(path));

        let attachments = frontmatter
            .entry("attachments".to_string())
            .or_insert(YamlValue::Sequence(vec![]));

        if let YamlValue::Sequence(list) = attachments {
            let exists = list.iter().any(|item| {
                if let YamlValue::String(existing) = item {
                    let parsed_existing = link_parser::parse_link(existing);
                    return link_parser::to_canonical(&parsed_existing, Path::new(path))
                        == target_canonical;
                }
                false
            });

            if !exists {
                list.push(YamlValue::String(attachment_path.to_string()));
            }
        }

        self.reconstruct_file(path, &frontmatter, &body)
    }

    /// Remove an attachment path from the entry's attachments list.
    /// Does nothing if the attachment isn't found.
    pub fn remove_attachment(&self, path: &str, attachment_path: &str) -> Result<()> {
        let (mut frontmatter, body) = match self.parse_file(path) {
            Ok(result) => result,
            Err(DiaryxError::NoFrontmatter(_)) => return Ok(()),
            Err(e) => return Err(e),
        };
        let parsed_target = link_parser::parse_link(attachment_path);
        let target_canonical = link_parser::to_canonical(&parsed_target, Path::new(path));

        if let Some(YamlValue::Sequence(list)) = frontmatter.get_mut("attachments") {
            list.retain(|item| {
                if let YamlValue::String(s) = item {
                    let parsed_existing = link_parser::parse_link(s);
                    link_parser::to_canonical(&parsed_existing, Path::new(path)) != target_canonical
                } else {
                    true
                }
            });

            // Remove empty attachments array
            if list.is_empty() {
                frontmatter.shift_remove("attachments");
            }
        }

        self.reconstruct_file(path, &frontmatter, &body)
    }

    /// Get the list of attachments directly declared in this entry.
    pub fn get_attachments(&self, path: &str) -> Result<Vec<String>> {
        let (frontmatter, _) = match self.parse_file(path) {
            Ok(result) => result,
            Err(DiaryxError::NoFrontmatter(_)) => return Ok(vec![]),
            Err(e) => return Err(e),
        };

        match frontmatter.get("attachments") {
            Some(YamlValue::Sequence(list)) => Ok(list
                .iter()
                .filter_map(|v| {
                    if let YamlValue::String(s) = v {
                        Some(s.clone())
                    } else {
                        None
                    }
                })
                .collect()),
            _ => Ok(vec![]),
        }
    }

    /// Resolve an attachment by traversing up the index hierarchy via part_of.
    /// Returns the absolute path to the attachment if found, or None.
    pub fn resolve_attachment(
        &self,
        entry_path: &str,
        attachment_name: &str,
    ) -> Result<Option<PathBuf>> {
        use crate::workspace::IndexFrontmatter;

        let entry_path = Path::new(entry_path);
        let entry_dir = entry_path.parent().unwrap_or(Path::new("."));

        // Parse the file's frontmatter inline (to avoid async Workspace dependency)
        let content = match self.fs.read_to_string(entry_path) {
            Ok(c) => c,
            Err(_) => return Ok(None),
        };

        // Parse frontmatter
        if !content.starts_with("---\n") && !content.starts_with("---\r\n") {
            return Ok(None);
        }

        let rest = &content[4..];
        let end_idx = match rest.find("\n---\n").or_else(|| rest.find("\n---\r\n")) {
            Some(idx) => idx,
            None => return Ok(None),
        };

        let frontmatter_str = &rest[..end_idx];
        let frontmatter: IndexFrontmatter = match serde_yaml::from_str(frontmatter_str) {
            Ok(fm) => fm,
            Err(_) => return Ok(None),
        };

        // First, check attachments directly on this entry
        for att_path in frontmatter.attachments_list() {
            // Parse through link_parser to handle markdown links and different path formats
            let parsed = link_parser::parse_link(att_path);
            let resolved = entry_dir.join(&parsed.path);
            if resolved.file_name().map(|n| n.to_string_lossy()) == Some(attachment_name.into())
                && self.fs.exists(&resolved)
            {
                return Ok(Some(resolved));
            }
            // Also check if the path itself matches
            if parsed.path == attachment_name
                || parsed.path.ends_with(&format!("/{}", attachment_name))
            {
                let resolved = entry_dir.join(&parsed.path);
                if self.fs.exists(&resolved) {
                    return Ok(Some(resolved));
                }
            }
        }

        // Traverse up via part_of
        if let Some(ref parent_rel) = frontmatter.part_of {
            let parsed_parent = link_parser::parse_link(parent_rel);
            let canonical_parent = link_parser::to_canonical(&parsed_parent, entry_path);
            let parent_path = PathBuf::from(canonical_parent);
            if self.fs.exists(&parent_path) {
                return self.resolve_attachment(&parent_path.to_string_lossy(), attachment_name);
            }
        }

        Ok(None)
    }

    // ==================== Frontmatter Sorting ====================

    /// Sort frontmatter keys according to a pattern.
    /// Pattern is comma-separated keys, with "*" meaning "rest alphabetically".
    /// Example: "title,description,*" puts title first, description second, rest alphabetically
    /// Does nothing if no frontmatter exists (won't add empty frontmatter).
    pub fn sort_frontmatter(&self, path: &str, pattern: Option<&str>) -> Result<()> {
        let (frontmatter, body) = match self.parse_file(path) {
            Ok(result) => result,
            Err(DiaryxError::NoFrontmatter(_)) => return Ok(()), // No frontmatter, nothing to sort
            Err(e) => return Err(e),
        };

        let sorted = match pattern {
            Some(p) => self.sort_by_pattern(frontmatter, p),
            None => self.sort_alphabetically(frontmatter),
        };

        self.reconstruct_file(path, &sorted, &body)
    }

    fn sort_alphabetically(
        &self,
        frontmatter: IndexMap<String, YamlValue>,
    ) -> IndexMap<String, YamlValue> {
        let mut pairs: Vec<_> = frontmatter.into_iter().collect();
        pairs.sort_by(|a, b| a.0.cmp(&b.0));
        pairs.into_iter().collect()
    }

    fn sort_by_pattern(
        &self,
        frontmatter: IndexMap<String, YamlValue>,
        pattern: &str,
    ) -> IndexMap<String, YamlValue> {
        let priority_keys: Vec<&str> = pattern.split(',').map(|s| s.trim()).collect();

        let mut result = IndexMap::new();
        let mut remaining: IndexMap<String, YamlValue> = frontmatter;

        for key in &priority_keys {
            if *key == "*" {
                // Insert remaining keys alphabetically
                let mut rest: Vec<_> = remaining.drain(..).collect();
                rest.sort_by(|a, b| a.0.cmp(&b.0));
                for (k, v) in rest {
                    result.insert(k, v);
                }
                break;
            } else if let Some(value) = remaining.shift_remove(*key) {
                result.insert(key.to_string(), value);
            }
        }

        // If no "*" was in pattern, append any remaining keys alphabetically.
        if !remaining.is_empty() {
            let mut rest: Vec<_> = remaining.drain(..).collect();
            rest.sort_by(|a, b| a.0.cmp(&b.0));
            for (k, v) in rest {
                result.insert(k, v);
            }
        }

        result
    }
}

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

    #[test]
    fn test_get_content() {
        let fs = MockFileSystem::new().with_file("test.md", "---\ntitle: Test\n---\nHello, world!");
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs));

        let content = crate::fs::block_on_test(app.get_content("test.md")).unwrap();
        assert_eq!(content, "Hello, world!");
    }

    #[test]
    fn test_get_content_empty_body() {
        let fs = MockFileSystem::new().with_file("test.md", "---\ntitle: Test\n---\n");
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs));

        let content = crate::fs::block_on_test(app.get_content("test.md")).unwrap();
        assert_eq!(content, "");
    }

    #[test]
    fn test_get_content_no_frontmatter() {
        let fs = MockFileSystem::new().with_file("test.md", "Just plain content");
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs));

        let content = crate::fs::block_on_test(app.get_content("test.md")).unwrap();
        assert_eq!(content, "Just plain content");
    }

    #[test]
    fn test_set_content() {
        let fs = MockFileSystem::new().with_file("test.md", "---\ntitle: Test\n---\nOld content");
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs.clone()));

        crate::fs::block_on_test(app.set_content("test.md", "New content")).unwrap();

        let result = fs.get_content("test.md").unwrap();
        assert!(result.contains("title: Test"));
        assert!(result.contains("New content"));
        assert!(!result.contains("Old content"));
    }

    #[test]
    fn test_set_content_preserves_frontmatter() {
        let fs = MockFileSystem::new().with_file(
            "test.md",
            "---\ntitle: My Title\ndescription: A description\n---\nOld",
        );
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs.clone()));

        crate::fs::block_on_test(app.set_content("test.md", "New body")).unwrap();

        let result = fs.get_content("test.md").unwrap();
        assert!(result.contains("title: My Title"));
        assert!(result.contains("description: A description"));
        assert!(result.contains("New body"));
    }

    #[test]
    fn test_clear_content() {
        let fs =
            MockFileSystem::new().with_file("test.md", "---\ntitle: Test\n---\nSome content here");
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs.clone()));

        crate::fs::block_on_test(app.clear_content("test.md")).unwrap();

        let result = fs.get_content("test.md").unwrap();
        assert!(result.contains("title: Test"));
        // Content should be empty after the frontmatter closing
        assert!(result.ends_with("---\n"));
    }

    #[test]
    fn test_append_content() {
        let fs = MockFileSystem::new().with_file("test.md", "---\ntitle: Test\n---\nFirst line");
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs.clone()));

        crate::fs::block_on_test(app.append_content("test.md", "Second line")).unwrap();

        let result = fs.get_content("test.md").unwrap();
        assert!(result.contains("First line"));
        assert!(result.contains("Second line"));
        // Second line should come after first
        let first_pos = result.find("First line").unwrap();
        let second_pos = result.find("Second line").unwrap();
        assert!(second_pos > first_pos);
    }

    #[test]
    fn test_append_content_adds_newline() {
        let fs = MockFileSystem::new()
            .with_file("test.md", "---\ntitle: Test\n---\nNo trailing newline");
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs.clone()));

        crate::fs::block_on_test(app.append_content("test.md", "Appended")).unwrap();

        let content = crate::fs::block_on_test(app.get_content("test.md")).unwrap();
        assert!(content.contains("No trailing newline\nAppended"));
    }

    #[test]
    fn test_append_content_to_empty_body() {
        let fs = MockFileSystem::new().with_file("test.md", "---\ntitle: Test\n---\n");
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs.clone()));

        crate::fs::block_on_test(app.append_content("test.md", "New content")).unwrap();

        let content = crate::fs::block_on_test(app.get_content("test.md")).unwrap();
        assert_eq!(content, "New content");
    }

    #[test]
    fn test_prepend_content() {
        let fs =
            MockFileSystem::new().with_file("test.md", "---\ntitle: Test\n---\nExisting content");
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs.clone()));

        crate::fs::block_on_test(app.prepend_content("test.md", "# Header")).unwrap();

        let result = fs.get_content("test.md").unwrap();
        assert!(result.contains("# Header"));
        assert!(result.contains("Existing content"));
        // Header should come before existing content
        let header_pos = result.find("# Header").unwrap();
        let existing_pos = result.find("Existing content").unwrap();
        assert!(header_pos < existing_pos);
    }

    #[test]
    fn test_prepend_content_adds_newline() {
        let fs = MockFileSystem::new().with_file("test.md", "---\ntitle: Test\n---\nExisting");
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs.clone()));

        crate::fs::block_on_test(app.prepend_content("test.md", "Prepended")).unwrap();

        let content = crate::fs::block_on_test(app.get_content("test.md")).unwrap();
        assert!(content.contains("Prepended\nExisting"));
    }

    #[test]
    fn test_prepend_content_to_empty_body() {
        let fs = MockFileSystem::new().with_file("test.md", "---\ntitle: Test\n---\n");
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs.clone()));

        crate::fs::block_on_test(app.prepend_content("test.md", "New content")).unwrap();

        let content = crate::fs::block_on_test(app.get_content("test.md")).unwrap();
        assert_eq!(content, "New content");
    }

    #[test]
    fn test_content_operations_on_nonexistent_file() {
        let fs = MockFileSystem::new();
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs.clone()));

        // set_content should create the file
        crate::fs::block_on_test(app.set_content("new.md", "Content")).unwrap();

        let result = fs.get_content("new.md").unwrap();
        assert!(result.contains("Content"));
    }

    #[test]
    fn test_multiple_content_operations() {
        let fs = MockFileSystem::new().with_file("test.md", "---\ntitle: Test\n---\n");
        let app = DiaryxApp::new(SyncToAsyncFs::new(fs.clone()));

        crate::fs::block_on_test(app.append_content("test.md", "Line 1")).unwrap();
        crate::fs::block_on_test(app.append_content("test.md", "Line 2")).unwrap();
        crate::fs::block_on_test(app.prepend_content("test.md", "# Title")).unwrap();

        let content = crate::fs::block_on_test(app.get_content("test.md")).unwrap();
        assert!(content.starts_with("# Title"));
        assert!(content.contains("Line 1"));
        assert!(content.contains("Line 2"));
    }
}