obsidian-rs-core 0.2.0

A library for interacting with Obsidian vaults
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
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;

use regex::Regex;

use crate::{LocatedTag, Location, NoteError, common};

use gray_matter::{Matter, Pod, engine::YAML};
use indexmap::IndexMap;

#[derive(Clone)]
pub struct Note {
    pub path: PathBuf,
    pub id: String,
    pub title: Option<String>,
    pub aliases: Vec<String>,
    /// All tags: frontmatter tags have `location: Location::Frontmatter`; inline tags have
    /// `location: Location::Inline(...)`. Always populated, even when content is not loaded.
    pub tags: Vec<LocatedTag>,
    /// Body text stripped of frontmatter. `None` when the note was loaded without body
    /// (the default). Use [`Note::from_path_with_body`] or [`Note::load_body`] to
    /// populate this field. Required for [`Note::write`].
    pub body: Option<String>,
    /// Links extracted from the body at load time (always populated).
    pub links: Vec<crate::LocatedLink>,
    pub frontmatter: Option<IndexMap<String, Pod>>,
    /// Number of lines occupied by the frontmatter block (including delimiters).
    /// Used to offset link locations so they reflect positions in the original file.
    pub frontmatter_line_count: usize,
}

#[derive(Clone)]
pub struct NoteBuilder {
    pub path: PathBuf,
    pub id: String,
    pub title: Option<String>,
    pub aliases: Vec<String>,
    pub tags: Vec<LocatedTag>,
    pub body: Option<String>,
}

impl NoteBuilder {
    pub fn new(path: impl AsRef<Path>) -> Result<Self, NoteError> {
        Ok(Self {
            path: path.as_ref().to_path_buf(),
            id: path
                .as_ref()
                .file_stem()
                .ok_or(NoteError::InvalidPath(path.as_ref().to_path_buf()))?
                .to_string_lossy()
                .to_string(),
            title: None,
            aliases: Vec::new(),
            tags: Vec::new(),
            body: None,
        })
    }

    pub fn id(mut self, id: &str) -> Self {
        self.id = id.to_string();
        self
    }

    pub fn title(mut self, title: &str) -> Self {
        self.title = Some(title.to_string());
        self
    }

    pub fn alias(mut self, alias: &str) -> Self {
        self.aliases.push(alias.to_string());
        self
    }

    pub fn aliases(mut self, aliases: &[String]) -> Self {
        for alias in aliases {
            self = self.alias(alias);
        }
        self
    }

    pub fn tag(mut self, tag: &str) -> Self {
        self.tags.push(LocatedTag {
            tag: tag.to_string(),
            location: Location::Frontmatter,
        });
        self
    }

    pub fn tags(mut self, tags: &[&str]) -> Self {
        for tag in tags {
            self = self.tag(tag);
        }
        self
    }

    pub fn located_tag(mut self, tag: &LocatedTag) -> Self {
        self.tags.push(tag.clone());
        self
    }

    pub fn located_tags(mut self, tags: &[LocatedTag]) -> Self {
        for tag in tags {
            self = self.located_tag(tag);
        }
        self
    }

    pub fn body(mut self, body: &str) -> Self {
        self.body = Some(body.to_string());
        self
    }

    pub fn build(self) -> Result<Note, NoteError> {
        let Self {
            path,
            id,
            title,
            aliases,
            tags,
            body,
        } = self;

        let mut note = Note {
            path,
            id,
            title,
            aliases,
            tags,
            body: None,
            links: Vec::new(),
            frontmatter: None,
            frontmatter_line_count: 0,
        };
        note.update_content(body.as_deref(), None)?;
        Ok(note)
    }
}

impl Note {
    pub fn builder(path: impl AsRef<Path>) -> Result<NoteBuilder, NoteError> {
        NoteBuilder::new(path)
    }

    /// Parses a note from a raw file string, always retaining the body content.
    ///
    /// Useful for constructing notes from in-memory strings (e.g. in tests).
    /// For file-backed notes prefer [`Note::from_path`] (no content) or
    /// [`Note::from_path_with_body`] (with content).
    pub fn parse(path: impl AsRef<Path>, content: &str) -> Self {
        Self::parse_impl(path, content, true)
    }

    /// Loads a note from disk without retaining the body content.
    ///
    /// Links and inline tags are still extracted and stored. This is the
    /// memory-efficient default for bulk operations. Use
    /// [`from_path_with_body`](Self::from_path_with_body) when the body
    /// text is needed (e.g. for content search or writing).
    pub fn from_path(path: impl AsRef<Path>) -> Result<Self, NoteError> {
        let path = common::normalize_path(path.as_ref(), None);
        let raw = std::fs::read_to_string(&path)?;
        Ok(Self::parse_impl(&path, &raw, false))
    }

    /// Loads a note from disk, retaining the full body content in [`Note::body`].
    pub fn from_path_with_body(path: impl AsRef<Path>) -> Result<Self, NoteError> {
        let path = common::normalize_path(path.as_ref(), None);
        let raw = std::fs::read_to_string(&path)?;
        Ok(Self::parse_impl(&path, &raw, true))
    }

    fn parse_impl(path: impl AsRef<Path>, content: &str, keep_body: bool) -> Self {
        let matter = Matter::<YAML>::new();
        let (body, frontmatter) = match matter.parse(content) {
            Ok(parsed) => {
                let fm = parsed.data.and_then(|pod: Pod| pod.as_hashmap().ok()).map(|hm| {
                    let mut entries: Vec<_> = hm.into_iter().collect();
                    entries.sort_by(|a, b| a.0.cmp(&b.0));
                    entries.into_iter().collect::<IndexMap<_, _>>()
                });
                (parsed.content, fm)
            }
            Err(_) => (content.to_string(), None),
        };
        let frontmatter_line_count = content.lines().count().saturating_sub(body.lines().count());
        let id = frontmatter
            .as_ref()
            .and_then(|fm| fm.get("id"))
            .and_then(|p| p.as_string().ok())
            .or_else(|| {
                path.as_ref()
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .map(|s| s.to_string())
            })
            .unwrap_or_default();
        let mut title = frontmatter
            .as_ref()
            .and_then(|fm| fm.get("title"))
            .and_then(|p| p.as_string().ok())
            .or_else(|| find_h1(&body));
        let aliases = {
            let mut v: Vec<String> = frontmatter
                .as_ref()
                .and_then(|fm| fm.get("aliases"))
                .and_then(|p| p.as_vec().ok())
                .unwrap_or_default()
                .into_iter()
                .filter_map(|p| p.as_string().ok())
                .collect();

            // If there's a title, it should be an alias too, and if there's not a title we should
            // infer it from the first alias
            if let Some(ref t) = title {
                let clean = strip_title_md(t);
                if !v.contains(&clean) {
                    v.push(clean);
                }
            } else if !v.is_empty() {
                title = Some(v[0].clone());
            }
            v
        };
        let fm_tags: Vec<LocatedTag> = frontmatter
            .as_ref()
            .and_then(|fm| fm.get("tags"))
            .and_then(|p| p.as_vec().ok())
            .unwrap_or_default()
            .into_iter()
            .filter_map(|p| p.as_string().ok())
            .map(|tag| LocatedTag {
                tag,
                location: Location::Frontmatter,
            })
            .collect();
        let offset = frontmatter_line_count;
        let links = crate::link::parse_links(&body)
            .into_iter()
            .map(|mut ll| {
                ll.location.line += offset;
                ll
            })
            .collect();
        let inline_tags = crate::tag::parse_inline_tags(&body)
            .into_iter()
            .map(|mut lt| {
                if let Location::Inline(ref mut loc) = lt.location {
                    loc.line += offset;
                }
                lt
            })
            .collect::<Vec<_>>();
        let mut tags = fm_tags;
        tags.extend(inline_tags);

        Note {
            path: path.as_ref().to_path_buf(),
            id,
            title,
            aliases,
            tags,
            body: if keep_body { Some(body) } else { None },
            links,
            frontmatter,
            frontmatter_line_count,
        }
    }

    pub fn update_content(
        &mut self,
        body: Option<&str>,
        frontmatter: Option<IndexMap<String, Pod>>,
    ) -> Result<(), NoteError> {
        if body.is_none() && frontmatter.is_none() {
            return Ok(());
        }

        if let Some(body) = body {
            if let Some(frontmatter) = frontmatter {
                self.frontmatter = Some(frontmatter);
            }
            let file_content = self.to_file_content(body)?;
            let parsed = Self::parse_impl(&self.path, &file_content, true);
            self.body = Some(body.to_string());
            self.tags = parsed.tags;
            self.links = parsed.links;
        } else if let Some(frontmatter) = frontmatter {
            // Update tags from frontmatter.
            let mut tags: Vec<LocatedTag> = frontmatter
                .get("tags")
                .and_then(|p| p.as_vec().ok())
                .unwrap_or_default()
                .into_iter()
                .filter_map(|p| p.as_string().ok())
                .map(|tag| LocatedTag {
                    tag,
                    location: Location::Frontmatter,
                })
                .collect();

            for tag in &self.tags {
                match tag.location {
                    Location::Frontmatter => {}
                    Location::Inline(_) => tags.push(tag.clone()),
                }
            }

            self.frontmatter = Some(frontmatter);
            self.tags = tags;
        }

        Ok(())
    }

    /// Reloads the note from its path without retaining body content.
    pub fn reload(self) -> Result<Self, NoteError> {
        Self::from_path(&self.path)
    }

    /// Reloads the note from its path while retaining body content.
    pub fn reload_with_body(self) -> Result<Self, NoteError> {
        Self::from_path_with_body(&self.path)
    }

    /// Populates [`Note::body`] by reading the note's body from disk.
    /// Does nothing if the body is already loaded.
    pub fn load_body(&mut self) -> Result<(), NoteError> {
        if self.body.is_none() {
            let raw = std::fs::read_to_string(&self.path)?;
            let matter = Matter::<YAML>::new();
            let body = match matter.parse::<Pod>(&raw) {
                Ok(parsed) => parsed.content,
                Err(_) => raw,
            };
            self.body = Some(body);
        }
        Ok(())
    }

    /// Add an alias.
    pub fn add_alias(&mut self, alias: String) {
        if !self.aliases.contains(&alias) {
            self.aliases.push(alias);
        }
    }

    /// Add a frontmatter tag.
    pub fn add_tag(&mut self, tag: impl Into<String>) {
        let tag = crate::tag::clean_tag(&tag.into());
        let already_present = self
            .tags
            .iter()
            .any(|t| t.tag.eq_ignore_ascii_case(&tag) && matches!(t.location, Location::Frontmatter));
        if !already_present {
            self.tags.push(LocatedTag {
                tag,
                location: Location::Frontmatter,
            });
        }
    }

    /// Remove a frontmatter tag.
    pub fn remove_tag(&mut self, tag: &str) {
        let tag = crate::tag::clean_tag(tag);
        self.tags
            .retain(|t| !(t.tag.eq_ignore_ascii_case(&tag) && matches!(t.location, Location::Frontmatter)));
    }

    /// Set an arbitrary frontmatter field to a value (which can be any YAML type).
    /// A null value removes the field from the frontmatter.
    pub fn set_field(&mut self, key: &str, value: &serde_yaml::Value) -> Result<(), NoteError> {
        // Guard against invalid field names that would cause YAML serialization to fail (e.g. containing newlines),
        // or that would be confusing to users (e.g. "id", "aliases", "tags" which are derived from other fields and would be ignored).
        if key.contains('\n') {
            return Err(NoteError::InvalidFieldName(
                "field names cannot contain newlines".to_string(),
            ));
        }
        if ["id", "title", "aliases", "tags"].contains(&key) {
            return Err(NoteError::InvalidFieldName(format!(
                "'{}' is a reserved field name and cannot be set this way",
                key
            )));
        }

        if self.frontmatter.is_none() {
            self.frontmatter = Some(IndexMap::new());
        }

        if value.is_null() {
            // Remove the field if value is null.
            self.frontmatter.as_mut().unwrap().shift_remove(key);
        } else {
            self.frontmatter
                .as_mut()
                .unwrap()
                .insert(key.to_string(), yaml_to_pod_value(value));
        }

        Ok(())
    }

    /// Atomically writes the note to `self.path`, including serialized frontmatter.
    ///
    /// Requires [`Note::body`] to be populated. Returns
    /// [`NoteError::BodyNotLoaded`] if the body is `None`.
    ///
    /// Frontmatter keys are serialized in a deterministic order: `id` first, then
    /// `title` (if present), then `aliases`, then `tags`, then all remaining keys
    /// sorted alphabetically.
    pub fn write(&self) -> Result<(), NoteError> {
        let content = self.read(true)?;
        let parent = self.path.parent().unwrap_or_else(|| Path::new("."));
        let mut tmp = tempfile::NamedTempFile::new_in(parent)?;
        tmp.write_all(content.as_bytes())?;
        tmp.persist(&self.path).map_err(|e| e.error)?;
        Ok(())
    }

    /// Atomically writes updated frontmatter to `self.path`, reading the current body
    /// from disk. Use this when only frontmatter has changed but not the body.
    pub fn write_frontmatter(&self) -> Result<(), NoteError> {
        let raw = std::fs::read_to_string(&self.path)?;
        let matter = Matter::<YAML>::new();
        let body = match matter.parse::<Pod>(&raw) {
            Ok(parsed) => parsed.content,
            Err(_) => raw.clone(),
        };
        let file_content = self.to_file_content(&body)?;
        let parent = self.path.parent().unwrap_or_else(|| Path::new("."));
        let mut tmp = tempfile::NamedTempFile::new_in(parent)?;
        tmp.write_all(file_content.as_bytes())?;
        tmp.persist(&self.path).map_err(|e| e.error)?;
        Ok(())
    }

    /// Read the contents of the note as a string, optionally including frontmatter.
    /// Requires [`Note::body`] to be populated. Returns
    /// [`NoteError::BodyNotLoaded`] if the body is `None`.
    pub fn read(&self, include_frontmatter: bool) -> Result<String, NoteError> {
        let body = self.body.as_deref().ok_or(NoteError::BodyNotLoaded)?;
        if include_frontmatter {
            let file_content = self.to_file_content(body)?;
            Ok(file_content)
        } else {
            Ok(body.to_string())
        }
    }

    /// Get the note's frontmatter map.
    pub fn frontmatter_map(&self) -> IndexMap<String, Pod> {
        let mut fm = if let Some(fm) = &self.frontmatter {
            fm.clone()
        } else {
            // No frontmatter; create it.
            IndexMap::new()
        };

        // Make sure fields are up-to-date.
        fm.insert("id".to_string(), Pod::String(self.id.clone()));
        if self.aliases.is_empty() {
            // No aliases; remove the field to avoid emitting an empty array.
            fm.shift_remove("aliases");
        } else {
            fm.insert(
                "aliases".to_string(),
                Pod::Array(self.aliases.iter().cloned().map(Pod::String).collect()),
            );
        }
        let fm_tags: Vec<String> = self
            .tags
            .iter()
            .filter(|t| matches!(t.location, Location::Frontmatter))
            .map(|t| t.tag.clone())
            .collect();
        if fm_tags.is_empty() {
            // No tags; remove the field to avoid emitting an empty array.
            fm.shift_remove("tags");
        } else {
            fm.insert(
                "tags".to_string(),
                Pod::Array(fm_tags.into_iter().map(Pod::String).collect()),
            );
        }
        fm
    }

    /// Get the note's frontmatter map in a form suitable for YAML serialization.
    pub fn frontmatter_yaml(&self) -> Result<serde_yaml::Mapping, serde_yaml::Error> {
        let fm = self.frontmatter_map();

        const PRIORITY_KEYS: &[&str] = &["id", "title", "aliases", "tags"];
        let mut mapping = serde_yaml::Mapping::new();
        // Emit priority keys in fixed order, only if present.
        for key in PRIORITY_KEYS {
            if let Some(v) = fm.get(*key) {
                mapping.insert(serde_yaml::Value::String((*key).to_string()), pod_to_yaml_value(v));
            }
        }
        // Emit remaining keys in alphabetical order.
        let mut rest: Vec<_> = fm
            .iter()
            .filter(|(k, _)| !PRIORITY_KEYS.contains(&k.as_str()))
            .collect();
        rest.sort_by(|a, b| a.0.cmp(b.0));
        for (k, v) in rest {
            mapping.insert(serde_yaml::Value::String(k.clone()), pod_to_yaml_value(v));
        }
        Ok(mapping)
    }

    /// Get the note's frontmatter map in a form suitable for JSON serialization.
    pub fn frontmatter_json(&self) -> Result<serde_json::Map<String, serde_json::Value>, NoteError> {
        let fm = self.frontmatter_map();
        let mut mapping = serde_json::Map::new();
        for (k, v) in fm {
            mapping.insert(k, pod_to_json_value(&v)?);
        }
        Ok(mapping)
    }

    /// Get the note's frontmatter as a YAML string (without delimiters).
    pub fn frontmatter_string(&self) -> Result<String, serde_yaml::Error> {
        let fm = self.frontmatter_yaml()?;
        let yaml = serde_yaml::to_string(&fm)?;
        // Strip leading "---\n" if emitted by serde_yaml, since we'll add our own delimiters.
        Ok(yaml.strip_prefix("---\n").unwrap_or(&yaml).to_string())
    }

    /// Get the last modified time of the note's file on disk.
    pub fn last_modified_time(&self) -> std::time::SystemTime {
        std::fs::metadata(&self.path)
            .and_then(|m| m.modified())
            .unwrap_or(std::time::SystemTime::UNIX_EPOCH)
    }

    /// Get the creation time of the note.
    pub fn creation_time(&self) -> std::time::SystemTime {
        std::fs::metadata(&self.path)
            .and_then(|m| m.created())
            .unwrap_or(std::time::SystemTime::UNIX_EPOCH)
    }

    fn to_file_content(&self, body: &str) -> Result<String, serde_yaml::Error> {
        let fm = self.frontmatter_string()?;
        Ok(format!("---\n{}---\n\n{}", fm, body))
    }
}

fn pod_to_yaml_value(pod: &Pod) -> serde_yaml::Value {
    match pod {
        Pod::Null => serde_yaml::Value::Null,
        Pod::String(s) => serde_yaml::Value::String(s.clone()),
        Pod::Integer(i) => serde_yaml::Value::Number((*i).into()),
        Pod::Float(f) => serde_yaml::Value::Number(serde_yaml::Number::from(*f)),
        Pod::Boolean(b) => serde_yaml::Value::Bool(*b),
        Pod::Array(arr) => serde_yaml::Value::Sequence(arr.iter().map(pod_to_yaml_value).collect()),
        Pod::Hash(map) => serde_yaml::Value::Mapping(
            map.iter()
                .map(|(k, v)| (serde_yaml::Value::String(k.clone()), pod_to_yaml_value(v)))
                .collect(),
        ),
    }
}

fn yaml_to_pod_value(yaml: &serde_yaml::Value) -> Pod {
    match yaml {
        serde_yaml::Value::Null => Pod::Null,
        serde_yaml::Value::String(s) => Pod::String(s.clone()),
        serde_yaml::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                Pod::Integer(i)
            } else if let Some(f) = n.as_f64() {
                Pod::Float(f)
            } else {
                // This should never happen since serde_yaml::Number can only be i64 or f64.
                Pod::Null
            }
        }
        serde_yaml::Value::Bool(b) => Pod::Boolean(*b),
        serde_yaml::Value::Sequence(seq) => Pod::Array(seq.iter().map(yaml_to_pod_value).collect()),
        serde_yaml::Value::Mapping(map) => Pod::Hash(
            map.iter()
                .filter_map(|(k, v)| k.as_str().map(|ks| (ks.to_string(), yaml_to_pod_value(v))))
                .collect(),
        ),
        serde_yaml::Value::Tagged(_) => {
            // YAML tags are not supported in our frontmatter; treat them as null.
            Pod::Null
        }
    }
}

fn pod_to_json_value(pod: &Pod) -> Result<serde_json::Value, NoteError> {
    match pod {
        Pod::Null => Ok(serde_json::Value::Null),
        Pod::String(s) => Ok(serde_json::Value::String(s.clone())),
        Pod::Integer(i) => Ok(serde_json::Value::Number((*i).into())),
        Pod::Float(f) => Ok(serde_json::Value::Number(
            serde_json::Number::from_f64(*f).ok_or_else(|| NoteError::Json(format!("invalid float value: {}", f)))?,
        )),
        Pod::Boolean(b) => Ok(serde_json::Value::Bool(*b)),
        Pod::Array(arr) => {
            let result: Result<Vec<serde_json::Value>, NoteError> = arr.iter().map(pod_to_json_value).collect();
            Ok(serde_json::Value::Array(result?))
        }
        Pod::Hash(map) => {
            let result: Result<serde_json::Map<String, serde_json::Value>, NoteError> = map
                .iter()
                .map(|(k, v)| pod_to_json_value(v).map(|json_v| (k.clone(), json_v)))
                .collect();
            result.map(serde_json::Value::Object)
        }
    }
}

fn find_h1(content: &str) -> Option<String> {
    content
        .lines()
        .find_map(|line| line.strip_prefix("# ").map(|t| t.trim_end().to_string()))
}

fn strip_title_md(s: &str) -> String {
    // [[target|alias]] → alias, [[target]] or [[target#heading]] → target
    static WIKI_RE: LazyLock<Regex> =
        LazyLock::new(|| Regex::new(r"!?\[\[([^\]#|]*?)(?:#[^\]|]*?)?(?:\|([^\]]*?))?\]\]").unwrap());
    // [text](url) → text
    static MD_LINK_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\[([^\]]+?)\]\([^)]*?\)").unwrap());
    // `code` → code
    static INLINE_CODE_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"`([^`\n]+)`").unwrap());

    let s = WIKI_RE.replace_all(s, |caps: &regex::Captures| {
        caps.get(2)
            .or_else(|| caps.get(1))
            .map_or("", |m| m.as_str())
            .to_string()
    });
    let s = MD_LINK_RE.replace_all(&s, "$1");
    let s = INLINE_CODE_RE.replace_all(&s, "$1");
    s.into_owned()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    #[test]
    fn parse_with_frontmatter() {
        let input = "---\ntitle: My Note\ntags: [rust, obsidian]\n---\n\nHello, world!";
        let note = Note::parse("/vault/my-note.md", input);

        assert_eq!(note.path, PathBuf::from("/vault/my-note.md"));
        assert_eq!(note.body.as_deref().unwrap().trim(), "Hello, world!");

        let fm = note.frontmatter.expect("should have frontmatter");
        assert!(fm.contains_key("title"));
        assert!(fm.contains_key("tags"));
    }

    #[test]
    fn parse_without_frontmatter() {
        let input = "Just some plain markdown content.";
        let note = Note::parse("/vault/plain.md", input);

        assert!(note.frontmatter.is_none());
        assert_eq!(note.body.as_deref().unwrap(), input);
    }

    #[test]
    fn from_path_reads_file() {
        let mut tmp = tempfile::NamedTempFile::new().unwrap();
        write!(tmp, "---\nauthor: Pete\n---\n\nBody text.").unwrap();

        let note = Note::from_path_with_body(tmp.path()).expect("should read file");
        let fm = note.frontmatter.expect("should have frontmatter");
        assert!(fm.contains_key("author"));
        assert!(note.body.unwrap().contains("Body text."));
    }

    #[test]
    fn id_from_frontmatter() {
        let input = "---\nid: custom-id\n---\n\nContent.";
        let note = Note::parse("/vault/my-note.md", input);
        assert_eq!(note.id, "custom-id");
    }

    #[test]
    fn id_falls_back_to_filename_stem() {
        let input = "---\nauthor: Pete\n---\n\nContent.";
        let note = Note::parse("/vault/my-note.md", input);
        assert_eq!(note.id, "my-note");
    }

    #[test]
    fn id_from_stem_when_no_frontmatter() {
        let note = Note::parse("/vault/another-note.md", "Just content.");
        assert_eq!(note.id, "another-note");
    }

    #[test]
    fn title_from_frontmatter() {
        let input = "---\ntitle: FM Title\n---\n\n# H1 Title\n\nContent.";
        let note = Note::parse("/vault/note.md", input);
        // frontmatter takes precedence over H1
        assert_eq!(note.title.as_deref(), Some("FM Title"));
    }

    #[test]
    fn title_from_h1() {
        let input = "# My Heading\n\nSome content.";
        let note = Note::parse("/vault/note.md", input);
        assert_eq!(note.title.as_deref(), Some("My Heading"));
    }

    #[test]
    fn title_none_when_absent() {
        let note = Note::parse("/vault/note.md", "No heading here.");
        assert!(note.title.is_none());
    }

    #[test]
    fn aliases_from_frontmatter_include_title() {
        let input = "---\ntitle: My Note\naliases: [alias-one, alias-two]\n---\n\nContent.";
        let note = Note::parse("/vault/note.md", input);
        assert!(note.aliases.contains(&"alias-one".to_string()));
        assert!(note.aliases.contains(&"alias-two".to_string()));
        assert!(note.aliases.contains(&"My Note".to_string()));
    }

    #[test]
    fn aliases_title_not_duplicated_when_already_present() {
        let input = "---\ntitle: My Note\naliases: [My Note, other-alias]\n---\n\nContent.";
        let note = Note::parse("/vault/note.md", input);
        assert_eq!(note.aliases.iter().filter(|a| *a == "My Note").count(), 1);
    }

    #[test]
    fn aliases_just_title_when_no_frontmatter_aliases() {
        let input = "---\ntitle: My Note\n---\n\nContent.";
        let note = Note::parse("/vault/note.md", input);
        assert_eq!(note.aliases, vec!["My Note".to_string()]);
    }

    #[test]
    fn aliases_empty_when_no_title_and_no_frontmatter_aliases() {
        let note = Note::parse("/vault/note.md", "No heading here.");
        assert!(note.aliases.is_empty());
    }

    #[test]
    fn aliases_includes_h1_title_when_no_frontmatter() {
        let input = "# H1 Title\n\nSome content.";
        let note = Note::parse("/vault/note.md", input);
        assert_eq!(note.aliases, vec!["H1 Title".to_string()]);
    }

    #[test]
    fn tags_from_frontmatter() {
        let input = "---\ntags: [rust, obsidian]\n---\n\nContent.";
        let note = Note::parse("/vault/note.md", input);
        let fm_tags: Vec<&str> = note
            .tags
            .iter()
            .filter(|t| matches!(t.location, crate::Location::Frontmatter))
            .map(|t| t.tag.as_str())
            .collect();
        assert_eq!(fm_tags, vec!["rust", "obsidian"]);
    }

    #[test]
    fn tags_empty_when_absent() {
        let note = Note::parse("/vault/note.md", "No frontmatter here.");
        assert!(
            !note
                .tags
                .iter()
                .any(|t| matches!(t.location, crate::Location::Frontmatter))
        );
    }

    #[test]
    fn write_frontmatter_key_ordering() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        // Provide keys out of order; verify they are written in the canonical order.
        std::fs::write(
            tmp.path(),
            "---\nzebra: last\ntags: [t]\naliases: [a]\ntitle: T\nid: my-id\nauthor: Pete\n---\n\nContent.",
        )
        .unwrap();

        let note = Note::from_path_with_body(tmp.path()).unwrap();
        note.write().unwrap();

        let on_disk = std::fs::read_to_string(tmp.path()).unwrap();
        // Extract only key lines (not list item lines that start with '-').
        let keys: Vec<&str> = on_disk
            .lines()
            .skip(1) // skip opening "---"
            .take_while(|l| *l != "---")
            .filter(|l| !l.starts_with('-'))
            .map(|l| l.split(':').next().unwrap())
            .collect();
        assert_eq!(keys, vec!["id", "title", "aliases", "tags", "author", "zebra"]);
    }

    #[test]
    fn write_frontmatter_key_ordering_no_title() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        std::fs::write(tmp.path(), "---\ntags: [t]\nid: my-id\nzebra: last\n---\n\nContent.").unwrap();

        let note = Note::from_path_with_body(tmp.path()).unwrap();
        note.write().unwrap();

        let on_disk = std::fs::read_to_string(tmp.path()).unwrap();
        let keys: Vec<&str> = on_disk
            .lines()
            .skip(1)
            .take_while(|l| *l != "---")
            .filter(|l| !l.starts_with('-'))
            .map(|l| l.split(':').next().unwrap())
            .collect();
        assert_eq!(keys, vec!["id", "tags", "zebra"]);
    }

    #[test]
    fn write_round_trips_note_without_frontmatter() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        let original = "Just some plain content.";
        std::fs::write(tmp.path(), original).unwrap();

        let note = Note::from_path_with_body(tmp.path()).unwrap();
        note.write().unwrap();

        let on_disk = std::fs::read_to_string(tmp.path()).unwrap();
        assert_eq!(
            on_disk,
            format!(
                "---\nid: {}\n---\n\n{}",
                tmp.path().file_stem().unwrap().display().to_string(),
                original
            )
        );
    }

    #[test]
    fn write_round_trips_note_with_frontmatter() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        let original = "---\ntitle: My Note\n---\n\nBody text.";
        std::fs::write(tmp.path(), original).unwrap();

        let note = Note::from_path_with_body(tmp.path()).unwrap();
        note.write().unwrap();

        // Re-parse to verify the on-disk content is valid and retains key fields.
        let reparsed = Note::from_path_with_body(tmp.path()).unwrap();
        assert_eq!(reparsed.title.as_deref(), Some("My Note"));
        assert_eq!(reparsed.body.as_deref().unwrap().trim(), "Body text.");
    }

    #[test]
    fn write_reflects_frontmatter_mutation() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        std::fs::write(tmp.path(), "---\ntitle: Old Title\n---\n\nContent.").unwrap();

        let mut note = Note::from_path_with_body(tmp.path()).unwrap();
        note.frontmatter
            .as_mut()
            .unwrap()
            .insert("title".to_string(), Pod::String("New Title".to_string()));
        note.write().unwrap();

        let reparsed = Note::from_path(tmp.path()).unwrap();
        assert_eq!(reparsed.title.as_deref(), Some("New Title"));
    }

    // strip_title_md unit tests

    #[test]
    fn strip_title_md_plain_is_unchanged() {
        assert_eq!(strip_title_md("My Note"), "My Note");
    }

    #[test]
    fn strip_title_md_wiki_link_no_alias() {
        assert_eq!(strip_title_md("[[linked note]]"), "linked note");
    }

    #[test]
    fn strip_title_md_wiki_link_with_alias() {
        assert_eq!(strip_title_md("[[note|display text]]"), "display text");
    }

    #[test]
    fn strip_title_md_wiki_link_with_heading() {
        assert_eq!(strip_title_md("[[note#heading]]"), "note");
    }

    #[test]
    fn strip_title_md_markdown_link() {
        assert_eq!(strip_title_md("[text](https://example.com)"), "text");
    }

    #[test]
    fn strip_title_md_inline_code() {
        assert_eq!(strip_title_md("`code` stuff"), "code stuff");
    }

    #[test]
    fn strip_title_md_mixed() {
        assert_eq!(strip_title_md("My [[note|ref]] and `stuff`"), "My ref and stuff");
    }

    // Integration tests: aliases use cleaned title

    #[test]
    fn alias_from_h1_with_wiki_link_no_alias() {
        let input = "# [[linked note]]\n\nContent.";
        let note = Note::parse("/vault/note.md", input);
        assert_eq!(note.title.as_deref(), Some("[[linked note]]"));
        assert!(note.aliases.contains(&"linked note".to_string()));
    }

    #[test]
    fn alias_from_h1_with_wiki_link_with_alias() {
        let input = "# [[note|display text]]\n\nContent.";
        let note = Note::parse("/vault/note.md", input);
        assert!(note.aliases.contains(&"display text".to_string()));
    }

    #[test]
    fn alias_from_h1_with_markdown_link() {
        let input = "# [text](https://example.com)\n\nContent.";
        let note = Note::parse("/vault/note.md", input);
        assert!(note.aliases.contains(&"text".to_string()));
    }

    #[test]
    fn alias_from_h1_with_inline_code() {
        let input = "# `code` stuff\n\nContent.";
        let note = Note::parse("/vault/note.md", input);
        assert!(note.aliases.contains(&"code stuff".to_string()));
    }

    #[test]
    fn alias_from_h1_mixed_markdown() {
        let input = "# My [[note|ref]] and `stuff`\n\nContent.";
        let note = Note::parse("/vault/note.md", input);
        assert!(note.aliases.contains(&"My ref and stuff".to_string()));
    }

    #[test]
    fn alias_from_frontmatter_title_with_wiki_link() {
        let input = "---\ntitle: \"[[note|display]]\"\n---\n\nContent.";
        let note = Note::parse("/vault/note.md", input);
        assert!(note.aliases.contains(&"display".to_string()));
    }

    #[test]
    fn alias_plain_title_unchanged() {
        let input = "# My Note\n\nContent.";
        let note = Note::parse("/vault/note.md", input);
        assert!(note.aliases.contains(&"My Note".to_string()));
    }

    #[test]
    fn links_location_offset_by_frontmatter() {
        // Frontmatter is lines 1-3; "[[target]]" is on line 4 and "[text](url)" on line 5.
        let content = "---\ntitle: T\n---\n[[target]]\n[text](url)";
        let note = Note::parse("/vault/note.md", content);
        assert_eq!(note.links.len(), 2);
        assert_eq!(note.links[0].location.line, 4);
        assert_eq!(note.links[0].location.col_start, 0);
        assert_eq!(note.links[0].location.col_end, 10);
        assert_eq!(note.links[1].location.line, 5);
        assert_eq!(note.links[1].location.col_start, 0);
        assert_eq!(note.links[1].location.col_end, 11);
    }
}