obsidian-rs-core 0.6.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
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
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
use std::collections::{BTreeSet, HashMap};
use std::env::current_dir;
use std::fs;
use std::io::Write;
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use gray_matter::Pod;
use indexmap::IndexMap;
use rayon::prelude::*;

use crate::health::VaultHealthReport;
use crate::{InlineLocation, Link, LocatedLink, LocatedTag, Location, Note, NoteError, VaultError, common, search};

#[derive(Clone)]
pub struct Vault {
    /// The root directory of the vault.
    path: PathBuf,
    /// When `Some`, an authoritative, complete in-memory snapshot of every note in the vault,
    /// keyed by absolute path. Searches and scans iterate this map and never touch the filesystem.
    /// When `None`, the vault operates in disk-walk mode. In-memory overrides and unsaved notes are
    /// simply entries in this map.
    cached_notes: Option<HashMap<PathBuf, Arc<Note>>>,
}

impl std::fmt::Debug for Vault {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Vault")
            .field("path", &self.path)
            .field("cached_note_count", &self.cached_notes.as_ref().map(HashMap::len))
            .finish()
    }
}

impl Vault {
    /// Opens a vault at the given path, returning an error if the path does not exist or is not a
    /// directory.
    pub fn open(path: impl AsRef<Path>) -> Result<Self, VaultError> {
        let path = common::normalize_path(path, None);
        if !path.is_dir() {
            return Err(VaultError::NotADirectory(path));
        }
        Ok(Vault {
            path,
            cached_notes: None,
        })
    }

    /// Opens a vault and immediately caches all notes in memory.
    ///
    /// This is intended for long-lived processes, such as LSP or MCP servers, that repeatedly query
    /// the same vault and receive explicit file-change notifications to refresh stale entries.
    pub fn open_cached(path: impl AsRef<Path>) -> Result<Self, VaultError> {
        let mut vault = Self::open(path)?;
        vault.cache_notes();
        Ok(vault)
    }

    /// Opens the nearest vault by walking up from the current directory, looking for an
    /// `.obsidian/` directory. Falls back to the current directory if none is found.
    pub fn open_from_cwd() -> Result<Self, VaultError> {
        let cwd = std::env::current_dir()?;
        let mut current = cwd.as_path();
        loop {
            if current.join(".obsidian").is_dir() {
                return Self::open(current);
            }
            match current.parent() {
                Some(parent) => current = parent,
                None => break,
            }
        }
        Self::open(&cwd)
    }

    pub fn path(&self) -> &Path {
        self.path.as_path()
    }

    pub fn is_note_path(path: impl AsRef<Path>) -> bool {
        path.as_ref().extension().and_then(|ext| ext.to_str()) == Some("md")
    }

    pub fn normalize_path(&self, path: impl AsRef<Path>) -> PathBuf {
        common::normalize_path(path, Some(&self.path))
    }

    pub fn cache_notes(&mut self) {
        let notes = search::find_note_paths(&self.path)
            .collect::<Vec<_>>()
            .into_par_iter()
            .filter_map(|path| Note::from_path(path).ok())
            .map(|note| (note.path.clone(), Arc::new(note)))
            .collect();
        self.cached_notes = Some(notes);
    }

    pub fn has_cached_note(&self, path: impl AsRef<Path>) -> bool {
        let path = self.normalize_path(path);
        self.cached_notes
            .as_ref()
            .is_some_and(|notes| notes.contains_key(&path))
    }

    fn has_known_note_path(&self, path: &Path) -> bool {
        let path = common::normalize_path(path, None);
        if let Some(cached_notes) = self.cached_notes.as_ref() {
            cached_notes.contains_key(&path)
        } else {
            path.is_file()
        }
    }

    pub fn note_for_path(&self, path: impl AsRef<Path>) -> Result<Note, VaultError> {
        let path = self.normalize_path(path);
        if let Some(note) = self.cached_notes.as_ref().and_then(|notes| notes.get(&path)) {
            return Ok(note.as_ref().clone());
        }
        Ok(Note::from_path(path)?)
    }

    pub fn text_for_path(&self, path: impl AsRef<Path>) -> Result<String, VaultError> {
        let path = self.normalize_path(path);
        if let Some(note) = self.cached_notes.as_ref().and_then(|notes| notes.get(&path)) {
            return Ok(note.text().to_string());
        }
        Ok(fs::read_to_string(path)?)
    }

    pub fn refresh_cached_note(&mut self, path: impl AsRef<Path>) -> Result<bool, VaultError> {
        let path = self.normalize_path(path);
        let Some(cached_notes) = self.cached_notes.as_mut() else {
            return Ok(false);
        };
        if !Self::is_note_path(&path) || !path.is_file() {
            cached_notes.remove(&path);
            return Ok(false);
        }

        let note = Note::from_path(&path)?;
        cached_notes.insert(note.path.clone(), Arc::new(note));
        Ok(true)
    }

    pub fn remove_cached_note(&mut self, path: impl AsRef<Path>) -> bool {
        let path = self.normalize_path(path);
        self.cached_notes
            .as_mut()
            .is_some_and(|cached_notes| cached_notes.remove(&path).is_some())
    }

    /// Resolve a note based on a path, filename, ID, title, or alias.
    pub fn resolve_note(&self, note: &str) -> Result<Note, VaultError> {
        // First try as a path.
        if let Ok((path, _)) = self.resolve_note_path(note, true) {
            return self.note_for_path(path);
        }

        // Then search by ID, aliases, and potentially filename.
        let mut search = self.search().or_has_id(note).or_has_alias(note).ignore_case();
        if note.ends_with(".md") && !note.contains('/') {
            let glob = format!("**/{}", note);
            let stem = note.trim_end_matches(".md");
            search = search.or_glob(glob).or_has_id(stem).or_has_alias(stem);
        }

        let results = search.execute().map_err(VaultError::Search)?;
        let mut notes: Vec<Note> = results.into_iter().filter_map(|r| r.ok()).collect();

        if notes.is_empty() {
            return Err(VaultError::NoteNotFound(note.to_string()));
        }

        if notes.len() == 1 {
            return Ok(notes.remove(0));
        }

        // If we have more than one match, check for *exact* (case-sensitive) matches on ID and aliases before giving up.
        let paths = notes.iter().map(|n| n.path.clone()).collect();
        let mut notes: Vec<_> = notes
            .into_iter()
            .filter(|n| n.id == note || n.aliases.iter().any(|a| a == note))
            .collect();

        if notes.len() == 1 {
            return Ok(notes.remove(0));
        }

        Err(VaultError::AmbiguousNoteIdentifier(note.to_string(), paths))
    }

    /// Resolve a note path argument, which may be absolute or relative to either the current working
    /// directory or the vault root.
    /// Returns the resolved absolute path and the root it was resolved against, if any.
    pub fn resolve_note_path(
        &self,
        path: impl AsRef<Path>,
        strict: bool,
    ) -> Result<(std::path::PathBuf, Option<std::path::PathBuf>), VaultError> {
        let path = path.as_ref().to_path_buf();
        if path.is_absolute() {
            if self.has_known_note_path(&path) || !strict {
                return Ok((common::normalize_path(&path, None), None));
            } else {
                return Err(VaultError::NoteNotFound(path.to_string_lossy().to_string()));
            }
        }

        // If the cwd is inside of the vault root, prefer resolving against the cwd to avoid surprising
        // behavior where a note exists in the vault but can't be found because the user is working in
        // a subdirectory.
        let cwd = current_dir()?;
        let mut cwd_resolved = common::normalize_path(&path, Some(&cwd));
        if cwd_resolved.starts_with(&self.path) {
            // Return right away if it exists, otherwise check if the extension is missing.
            if self.has_known_note_path(&cwd_resolved) {
                return Ok((cwd_resolved, Some(cwd)));
            } else if cwd_resolved.extension().is_none() {
                cwd_resolved.set_extension("md");
                if self.has_known_note_path(&cwd_resolved) {
                    return Ok((cwd_resolved, Some(cwd)));
                }
            }

            // In strict mode, if we still haven't found an existing path, try the same thing against the vault root.
            // Otherwise return the cwd-resolved path even if it doesn't exist, since
            // that's more likely what the user intended than the vault root.
            let mut vault_resolved = common::normalize_path(&path, Some(&self.path));
            if strict {
                if self.has_known_note_path(&vault_resolved) {
                    return Ok((vault_resolved, Some(self.path.clone())));
                } else if vault_resolved.extension().is_none() {
                    vault_resolved.set_extension("md");
                    if self.has_known_note_path(&vault_resolved) {
                        return Ok((vault_resolved, Some(self.path.clone())));
                    }
                }
            } else {
                return Ok((cwd_resolved, Some(cwd)));
            }
        } else {
            let mut vault_resolved = common::normalize_path(&path, Some(&self.path));
            if self.has_known_note_path(&vault_resolved) {
                return Ok((vault_resolved, Some(self.path.clone())));
            } else if vault_resolved.extension().is_none() {
                vault_resolved.set_extension("md");
                if self.has_known_note_path(&vault_resolved) {
                    return Ok((vault_resolved, Some(self.path.clone())));
                }
            }

            if !strict {
                return Ok((vault_resolved, Some(self.path.clone())));
            }
        }

        Err(VaultError::NoteNotFound(path.to_string_lossy().to_string()))
    }

    /// Loads all notes in the vault in parallel, each retaining its full contents in
    /// [`Note::text`].
    pub fn notes(&self) -> Vec<Result<Note, NoteError>> {
        self.notes_filtered(|_| true)
    }

    /// Inserts or replaces an in-memory note in the cached snapshot. While present, this note
    /// shadows its on-disk counterpart (matched by `note.path`) across all vault operations. Notes
    /// with a path that does not exist on disk are included as additional candidates.
    ///
    /// Only meaningful for a cached vault (see [`open_cached`](Self::open_cached)); a no-op when the
    /// vault is in disk-walk mode.
    pub fn load_note(&mut self, mut note: Note) {
        let resolved_path = self
            .resolve_note_path(&note.path, false)
            .map(|(n, _)| n)
            .unwrap_or_else(|_| note.path.clone());
        note.path = resolved_path;
        if let Some(cached_notes) = self.cached_notes.as_mut() {
            cached_notes.insert(note.path.clone(), Arc::new(note));
        }
    }

    /// Restores the on-disk version of a note in the cached snapshot, re-reading it from disk (or
    /// removing it from the cache if it no longer exists). Does nothing in disk-walk mode.
    pub fn unload_note(&mut self, path: &Path) {
        let resolved_path = self
            .resolve_note_path(path, false)
            .map(|(n, _)| n)
            .unwrap_or_else(|_| path.into());
        let _ = self.refresh_cached_note(&resolved_path);
    }

    pub fn note_is_loaded(&self, path: impl AsRef<Path>) -> bool {
        self.has_cached_note(path)
    }

    /// Like [`notes`](Self::notes), but skips notes whose path does not satisfy `filter`.
    /// In disk-walk mode, filtering happens at the filesystem traversal level, before any file is
    /// read.
    pub fn notes_filtered(&self, filter: impl Fn(&Path) -> bool) -> Vec<Result<Note, NoteError>> {
        if let Some(cached_notes) = &self.cached_notes {
            let mut results: Vec<Result<Note, NoteError>> = cached_notes
                .values()
                .filter(|note| filter(&note.path))
                .map(|note| Ok(note.as_ref().clone()))
                .collect();
            results.sort_by(|left, right| {
                let left_path = left.as_ref().ok().map(|note| &note.path);
                let right_path = right.as_ref().ok().map(|note| &note.path);
                left_path.cmp(&right_path)
            });
            results
        } else {
            search::find_notes_filtered(&self.path, filter)
        }
    }

    /// Scans the vault for health issues: duplicate IDs, duplicate aliases, broken links, and stranded notes.
    ///
    /// Only notes whose path satisfies `filter` are included. Pass `|_| true` to scan
    /// everything, or use a glob-based closure (see [`notes_filtered`](Self::notes_filtered))
    /// to exclude specific paths.
    ///
    /// Note-load failures are silently skipped (consistent with other vault scan methods).
    pub fn check(&self, filter: impl Fn(&Path) -> bool) -> VaultHealthReport {
        let notes: Vec<Note> = self.notes_filtered(filter).into_iter().filter_map(|r| r.ok()).collect();
        crate::health::check_notes(&self.path, &notes)
    }

    /// Returns a [`SearchQuery`](search::SearchQuery) rooted at this vault's path.
    /// For a cached vault, the query runs against the in-memory snapshot (including any notes
    /// registered via [`load_note`](Self::load_note)); otherwise it walks the filesystem.
    pub fn search(&self) -> search::SearchQuery<'_> {
        let query = search::SearchQuery::new(&self.path);
        if let Some(cached_notes) = &self.cached_notes {
            query.with_cached_notes(cached_notes)
        } else {
            query
        }
    }

    /// Returns all unique tags used in the vault, aggregated from frontmatter and inline tags.
    pub fn list_tags(&self) -> Result<Vec<String>, VaultError> {
        if self.cached_notes.is_some() {
            let mut tags = BTreeSet::new();
            for note in self.notes_filtered(|_| true).into_iter().filter_map(Result::ok) {
                tags.extend(note.tags.into_iter().map(|tag| tag.tag.to_lowercase()));
            }
            Ok(tags.into_iter().collect())
        } else {
            search::find_all_tags(&self.path).map_err(VaultError::Note)
        }
    }

    /// Find all occurrences of specific tags, grouped by the note they appear in. Tags are matched
    /// case-insensitively, and sub-tags are gathered as well.
    pub fn find_tags(&self, tags: &[String]) -> Result<Vec<(Note, Vec<LocatedTag>)>, VaultError> {
        search::find_tags_with_query(self.search(), tags).map_err(VaultError::Search)
    }

    /// Find and replaces all occurrences of `old_tag` with the new `new_tag` and return
    /// the occurrences and location of the new tag.
    pub fn rename_tag(&mut self, old_tag: &str, new_tag: &str) -> Result<Vec<(Note, Vec<LocatedTag>)>, VaultError> {
        let mut results: Vec<(Note, Vec<LocatedTag>)> = Vec::new();
        for (mut note, tags) in self.find_tags(&[old_tag.into()])? {
            let mut tags_by_line: HashMap<usize, Vec<InlineLocation>> = HashMap::new();
            for lt in tags {
                match lt.location {
                    // Replace frontmatter tags immediately.
                    Location::Frontmatter => {
                        note.remove_tag(&lt.tag)?;
                        note.add_tag(new_tag)?;
                    }
                    // And gather tags in the body by their line number (1-indexed).
                    Location::Inline(loc) => {
                        tags_by_line.entry(loc.line).or_default();
                        tags_by_line.get_mut(&loc.line).unwrap().push(loc);
                    }
                };
            }

            if !tags_by_line.is_empty() {
                // Replace all occurrences of the old tag in the body with the new one.
                let mut lines: Vec<String> = note.body().lines().map(|s| s.to_string()).collect();
                for (lnum, locs) in tags_by_line.drain() {
                    let line = lines.get_mut(lnum - 1 - note.frontmatter_line_count).unwrap();
                    let mut offset = 0;
                    for loc in locs {
                        line.replace_range(
                            (offset + loc.col_start)..(offset + loc.col_end),
                            &format!("#{}", new_tag),
                        );
                        offset += new_tag.len() - old_tag.len();
                    }
                }

                let body = lines.join("\n");
                note.update_content(Some(&body), None)?;
            }

            // Re-collect the located tags.
            let tags = note
                .tags
                .iter()
                .filter_map(|lt| if lt.tag == new_tag { Some(lt.clone()) } else { None })
                .collect();

            // Persist to disk and keep any cached snapshot in sync.
            note.write()?;
            self.refresh_cached_note(&note.path)?;

            results.push((note, tags));
        }

        Ok(results)
    }

    /// Returns all notes in the vault that link to `target`, paired with the specific
    /// [`LocatedLink`]s within each note that point to it.
    ///
    /// Only wiki links (`[[target]]`) and markdown links (`[text](target.md)`) are
    /// considered. Embed links are excluded. Notes that fail to load are silently skipped.
    pub fn backlinks(&self, target: &Note) -> Result<Vec<(Note, Vec<LocatedLink>)>, VaultError> {
        if self.cached_notes.is_some() {
            let notes: Vec<Note> = self.notes().into_iter().filter_map(Result::ok).collect();
            return Ok(self
                .backlinks_from(&notes, target)
                .into_iter()
                .map(|(note, links)| (note.clone(), links))
                .collect());
        }

        let results = self
            .search()
            .and_links_to(target.clone())
            .execute()
            .map_err(VaultError::Search)?;
        let notes: Vec<Note> = results.into_iter().filter_map(|r| r.ok()).collect();
        let results = notes
            .into_iter()
            .map(|source| {
                let matching = search::find_matching_links(&source, target, &self.path);
                (source, matching)
            })
            .collect();
        Ok(results)
    }

    /// Like [`backlinks`](Self::backlinks), but operates on an already-loaded slice of notes
    /// instead of reading from disk. Returns references into `notes`.
    pub fn backlinks_from<'a>(&self, notes: &'a [Note], target: &Note) -> Vec<(&'a Note, Vec<LocatedLink>)> {
        crate::health::backlinks_from(notes, target, &self.path)
    }

    /// Computes all replacement pairs for a rename without performing any I/O.
    fn compute_rename_op(&self, note: &Note, new_path: &Path) -> Result<RenameOp, VaultError> {
        let new_dir = new_path.parent().unwrap_or_else(|| Path::new("."));
        if !new_dir.is_dir() {
            return Err(VaultError::DirectoryNotFound(new_dir.to_path_buf()));
        }

        if new_path.exists() {
            return Err(VaultError::NoteAlreadyExists(new_path.to_path_buf()));
        }

        let new_stem = new_path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or_default()
            .to_string();

        let old_stem = note
            .path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or_default()
            .to_string();

        let id_needs_update = note.id == old_stem;
        // let frontmatter_id_will_update =
        //     id_needs_update && note.frontmatter.as_ref().is_some_and(|fm| fm.contains_key("id"));

        let backlinks = self.backlinks(note)?;
        let mut per_note_replacements: Vec<(Note, Vec<(LocatedLink, String)>)> = Vec::new();

        for (source_note, links) in backlinks {
            let mut replacements: Vec<(LocatedLink, String)> = Vec::new();

            for ll in links {
                let new_text = match &ll.link {
                    Link::Wiki { target, heading, alias } if id_needs_update && target == &old_stem => {
                        let mut wiki = format!("[[{}", new_stem);
                        if let Some(h) = heading {
                            wiki.push('#');
                            wiki.push_str(h);
                        }
                        if let Some(a) = alias {
                            wiki.push('|');
                            wiki.push_str(a);
                        }
                        wiki.push_str("]]");
                        Some(wiki)
                    }
                    Link::Wiki { .. } => None,
                    Link::Markdown { text, url } => {
                        let fragment = url.find('#').map(|i| url[i..].to_string());
                        let new_url = common::relative_path(&self.path, new_path);
                        let new_url_str = new_url.to_string_lossy().replace('\\', "/");
                        let full_url = match fragment {
                            Some(f) => format!("{}{}", new_url_str, f),
                            None => new_url_str,
                        };
                        Some(format!("[{}]({})", text, full_url))
                    }
                    _ => None,
                };
                if let Some(text) = new_text {
                    replacements.push((ll, text));
                }
            }

            if !replacements.is_empty() {
                per_note_replacements.push((source_note, replacements));
            }
        }

        Ok(RenameOp {
            new_stem,
            frontmatter_id_will_update: id_needs_update,
            per_note_replacements,
        })
    }

    /// Renames `note` to `new_path` (full destination path), updating all backlinks.
    ///
    /// Wiki links targeting the old ID are rewritten to the new stem. Markdown links pointing
    /// to the old path are rewritten to the new path. Wiki links targeting an alias are left
    /// unchanged. Returns the reloaded [`Note`] at the new path.
    ///
    /// Returns [`VaultError::DirectoryNotFound`] if the parent directory of `new_path` does not
    /// exist, and [`VaultError::NoteAlreadyExists`] if `new_path` is already occupied.
    pub fn rename(&mut self, note: &Note, new_path: &Path) -> Result<Note, VaultError> {
        let new_path = common::normalize_path(new_path, Some(&self.path));
        let op = self.compute_rename_op(note, &new_path)?;

        let mut renamed = note.clone();
        renamed.path = new_path.clone();
        if op.frontmatter_id_will_update {
            renamed.id = op.new_stem;
        }

        renamed.write()?;
        std::fs::remove_file(&note.path)?;
        self.remove_cached_note(&note.path);
        self.refresh_cached_note(&new_path)?;

        for (source_note, replacements) in op.per_note_replacements {
            let raw_content = self.text_for_path(&source_note.path)?;
            let new_content = common::rewrite_links(&raw_content, replacements);
            std::fs::write(&source_note.path, new_content)?;
            self.refresh_cached_note(&source_note.path)?;
        }

        self.note_for_path(&new_path)
    }

    /// Returns a preview of what [`rename`](Self::rename) would change without touching the filesystem.
    ///
    /// Same validation and error variants as `rename`.
    pub fn rename_preview(&self, note: &Note, new_path: &Path) -> Result<RenamePreview, VaultError> {
        let edits = self.rename_edits(note, new_path)?;
        let updated_notes = edits
            .backlink_edits
            .iter()
            .map(|(path, replacements)| (path.clone(), replacements.len()))
            .collect();

        Ok(RenamePreview {
            new_path: edits.new_path,
            id_will_update: edits.id_will_update,
            updated_notes,
        })
    }

    /// Returns the exact backlink edits that [`rename`](Self::rename) would make without touching the filesystem.
    ///
    /// Same validation and error variants as `rename`.
    pub fn rename_edits(&self, note: &Note, new_path: &Path) -> Result<RenameEdits, VaultError> {
        let new_path = common::normalize_path(new_path, Some(&self.path));
        let op = self.compute_rename_op(note, &new_path)?;

        let mut backlink_edits: Vec<(PathBuf, Vec<(LocatedLink, String)>)> = op
            .per_note_replacements
            .into_iter()
            .map(|(source_note, replacements)| (source_note.path, replacements))
            .collect();
        backlink_edits.sort_by(|(a, _), (b, _)| a.cmp(b));

        Ok(RenameEdits {
            new_path: new_path.to_path_buf(),
            new_stem: op.new_stem,
            id_will_update: op.frontmatter_id_will_update,
            backlink_edits,
        })
    }

    fn raw_note_sections<'a>(note_path: &Path, raw: &'a str) -> (&'a str, &'a str) {
        let parsed = Note::parse(note_path, raw);
        let body_start = raw
            .split_inclusive('\n')
            .take(parsed.frontmatter_line_count)
            .map(str::len)
            .sum();
        raw.split_at(body_start)
    }

    fn apply_raw_note_update(&mut self, note_path: &Path, raw: String) -> Result<Note, VaultError> {
        let note_path = self.normalize_path(note_path);
        let updated = Note::parse(&note_path, &raw);

        let parent = note_path.parent().unwrap_or_else(|| Path::new("."));
        let mut tmp = tempfile::NamedTempFile::new_in(parent)?;
        tmp.write_all(raw.as_bytes())?;
        tmp.persist(&note_path).map_err(|e| e.error)?;
        self.refresh_cached_note(&note_path)?;

        Ok(updated)
    }

    /// Replaces the first (and only) occurrence of `old_string` in the body text of `note`
    /// with `new_string`, preserving the rest of the raw note text unchanged.
    ///
    /// Returns [`VaultError::StringNotFound`] if `old_string` does not appear in the body, and
    /// [`VaultError::StringFoundMultipleTimes`] if it appears more than once. Both checks operate
    /// on the raw body text only (frontmatter excluded).
    pub fn patch_note(&mut self, note: &Note, old_string: &str, new_string: &str) -> Result<Note, VaultError> {
        let raw = self.text_for_path(&note.path)?;
        let (raw_frontmatter, raw_body) = Self::raw_note_sections(&note.path, &raw);

        let count = raw_body.matches(old_string).count();
        if count == 0 {
            return Err(VaultError::StringNotFound(note.path.clone()));
        }
        if count > 1 {
            return Err(VaultError::StringFoundMultipleTimes(note.path.clone()));
        }

        let patched_body = raw_body.replacen(old_string, new_string, 1);
        let patched_raw = format!("{raw_frontmatter}{patched_body}");
        self.apply_raw_note_update(&note.path, patched_raw)
    }

    /// Appends `content` exactly as provided to the end of `note`'s body text, preserving the raw
    /// frontmatter block unchanged.
    pub fn append_to_note(&mut self, note: &Note, content: &str) -> Result<Note, VaultError> {
        let raw = self.text_for_path(&note.path)?;
        let (raw_frontmatter, raw_body) = Self::raw_note_sections(&note.path, &raw);
        let appended_raw = format!("{raw_frontmatter}{raw_body}{content}");
        self.apply_raw_note_update(&note.path, appended_raw)
    }

    /// Returns the exact source/new-note file contents that [`extract_to_note`](Self::extract_to_note)
    /// would produce without touching the filesystem.
    pub fn extract_to_note_edits(
        &self,
        note: &Note,
        selection: &ExtractSelection,
        new_path: impl AsRef<Path>,
        new_id: Option<&str>,
        replace_with: Option<&str>,
    ) -> Result<ExtractEdits, VaultError> {
        let raw = self.text_for_path(&note.path)?;
        self.extract_to_note_edits_from_text(&note.path, &raw, selection, new_path, new_id, replace_with)
    }

    /// Like [`extract_to_note_edits`](Self::extract_to_note_edits), but uses the provided raw source
    /// text instead of reading from the vault. This is useful for editor integrations that need
    /// unsaved buffer contents to participate in the core extraction logic.
    pub fn extract_to_note_edits_from_text(
        &self,
        source_path: impl AsRef<Path>,
        raw_source: &str,
        selection: &ExtractSelection,
        new_path: impl AsRef<Path>,
        new_id: Option<&str>,
        replace_with: Option<&str>,
    ) -> Result<ExtractEdits, VaultError> {
        let source_path = self.normalize_path(source_path);
        let new_path = self.prepare_new_note_path(new_path.as_ref());
        if self.has_known_note_path(&new_path) {
            return Err(VaultError::NoteAlreadyExists(new_path));
        }

        let op =
            self.compute_extract_op_from_text(&source_path, raw_source, selection, &new_path, new_id, replace_with)?;
        Ok(ExtractEdits {
            source_path: source_path.clone(),
            source_content: op.source_raw,
            source_note: op.source_note,
            new_path: op.new_note.path.clone(),
            new_content: op.new_raw,
            new_note: op.new_note,
        })
    }

    /// Extracts a section or span of body text from `note` into a new note.
    ///
    /// The source note is updated in-place, preserving its raw frontmatter formatting, while the
    /// new note is created at `new_path`. If `replace_with` is `None`, the source note is updated
    /// with a wiki link to the new note's ID.
    pub fn extract_to_note(
        &mut self,
        note: &Note,
        selection: &ExtractSelection,
        new_path: impl AsRef<Path>,
        new_id: Option<&str>,
        replace_with: Option<&str>,
    ) -> Result<ExtractResult, VaultError> {
        let edits = self.extract_to_note_edits(note, selection, new_path, new_id, replace_with)?;

        if let Some(parent) = edits.new_path.parent() {
            fs::create_dir_all(parent)?;
        }

        edits.new_note.write()?;

        let source_note = match self.apply_raw_note_update(&edits.source_path, edits.source_content.clone()) {
            Ok(note) => note,
            Err(error) => {
                let _ = fs::remove_file(&edits.new_path);
                return Err(error);
            }
        };

        Ok(ExtractResult {
            source_note,
            new_note: edits.new_note,
        })
    }

    fn prepare_new_note_path(&self, new_path: &Path) -> PathBuf {
        let mut path = if new_path.is_absolute() {
            new_path.to_path_buf()
        } else {
            self.path.join(new_path)
        };
        if path.extension().is_none() {
            path.set_extension("md");
        }
        common::normalize_path(path, Some(&self.path))
    }

    fn compute_extract_op_from_text(
        &self,
        source_path: &Path,
        raw_source: &str,
        selection: &ExtractSelection,
        new_path: &Path,
        new_id: Option<&str>,
        replace_with: Option<&str>,
    ) -> Result<ExtractOp, VaultError> {
        let resolved = resolve_extract_selection(source_path, raw_source, selection)?;

        let mut extracted = raw_source[resolved.extracted_range.clone()].to_string();
        if let Some(root_level) = resolved.section_root_level {
            extracted = normalize_section_heading_levels(&extracted, root_level);
        }
        extracted = rewrite_relative_markdown_links(&extracted, source_path, new_path);

        let new_note = build_extracted_note(new_path, &extracted, new_id)?;
        let new_raw = new_note.read(true)?;

        let default_link = format!("[[{}]]", new_note.id);
        let replacement = replace_with.unwrap_or(default_link.as_str());
        let source_raw = if resolved.section_root_level.is_some() {
            replace_section_body(raw_source, resolved.source_replace_range, replacement)
        } else {
            replace_text_range(raw_source, resolved.source_replace_range, replacement)
        };
        let source_note = Note::parse(source_path, &source_raw);

        Ok(ExtractOp {
            source_raw,
            source_note,
            new_raw,
            new_note,
        })
    }

    /// Computes all changes required to merge `sources` into `dest_path` without performing I/O.
    fn compute_merge_op(&self, sources: &[Note], dest_path: impl AsRef<Path>) -> Result<MergeOp, VaultError> {
        use std::collections::HashMap;

        let dest_path = dest_path.as_ref();
        let dest_dir = &dest_path.parent().unwrap_or_else(|| Path::new("."));
        if !dest_dir.is_dir() {
            return Err(VaultError::DirectoryNotFound(dest_dir.to_path_buf()));
        }

        for source in sources {
            if source.path == dest_path {
                return Err(VaultError::MergeSourceIsDestination(source.path.clone()));
            }
        }

        let dest_is_new = !dest_path.exists();

        let dest_stem = dest_path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or_default()
            .to_string();

        let source_paths: Vec<&Path> = sources.iter().map(|s| s.path.as_path()).collect();

        // Aggregate backlink replacements per linking note, skipping sources and dest.
        let mut replacements_by_path: HashMap<PathBuf, Vec<(LocatedLink, String)>> = HashMap::new();

        for source in sources {
            let backlinks = self.backlinks(source)?;
            for (linking_note, links) in backlinks {
                if source_paths.iter().any(|p| *p == linking_note.path) {
                    continue;
                }
                if linking_note.path == dest_path {
                    continue;
                }

                let entry = replacements_by_path.entry(linking_note.path.clone()).or_default();

                for ll in links {
                    let new_text = match &ll.link {
                        Link::Wiki { heading, alias, .. } => {
                            let mut wiki = format!("[[{}", dest_stem);
                            if let Some(h) = heading {
                                wiki.push('#');
                                wiki.push_str(h);
                            }
                            if let Some(a) = alias {
                                wiki.push('|');
                                wiki.push_str(a);
                            }
                            wiki.push_str("]]");
                            Some(wiki)
                        }
                        Link::Markdown { text, url } => {
                            let fragment = url.find('#').map(|i| url[i..].to_string());
                            let new_url = common::relative_path(&self.path, dest_path);
                            let new_url_str = new_url.to_string_lossy().replace('\\', "/");
                            let full_url = match fragment {
                                Some(f) => format!("{}{}", new_url_str, f),
                                None => new_url_str.to_string(),
                            };
                            Some(format!("[{}]({})", text, full_url))
                        }
                        _ => None,
                    };
                    if let Some(text) = new_text {
                        entry.push((ll, text));
                    }
                }
            }
        }

        let per_note_replacements: Vec<(PathBuf, Vec<(LocatedLink, String)>)> = replacements_by_path
            .into_iter()
            .filter(|(_, r)| !r.is_empty())
            .collect();

        // Load existing destination if present.
        let (dest_body, dest_fm_tags, dest_fm_aliases, dest_frontmatter) = if dest_is_new {
            (String::new(), Vec::<String>::new(), Vec::<String>::new(), None)
        } else {
            let d = Note::from_path(dest_path)?;
            let tags = d
                .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())
                .collect::<Vec<_>>();
            let aliases = d
                .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::<Vec<_>>();
            let body = d.body().trim_start().to_string();
            let fm = d.frontmatter;
            (body, tags, aliases, fm)
        };

        // Build merged body.
        let mut body_parts: Vec<String> = Vec::new();
        if !dest_body.is_empty() {
            body_parts.push(dest_body);
        }
        for source in sources {
            let body = source.body().trim_start().to_string();
            if !body.is_empty() {
                body_parts.push(body);
            }
        }
        let merged_content = body_parts.join("\n\n---\n\n");

        // Build merged frontmatter: dest wins on id/title, union on tags/aliases.
        let mut fm: IndexMap<String, Pod> = dest_frontmatter.unwrap_or_default();

        let mut tag_strings: Vec<String> = dest_fm_tags;
        for source in sources {
            for lt in source
                .tags
                .iter()
                .filter(|t| matches!(t.location, Location::Frontmatter))
            {
                if !tag_strings.contains(&lt.tag) {
                    tag_strings.push(lt.tag.clone());
                }
            }
        }
        if !tag_strings.is_empty() {
            fm.insert(
                "tags".to_string(),
                Pod::Array(tag_strings.clone().into_iter().map(Pod::String).collect()),
            );
        }

        let mut alias_strings: Vec<String> = dest_fm_aliases;
        for source in sources {
            let src_aliases: Vec<String> = source
                .frontmatter
                .as_ref()
                .and_then(|sfm| sfm.get("aliases"))
                .and_then(|p| p.as_vec().ok())
                .unwrap_or_default()
                .into_iter()
                .filter_map(|p| p.as_string().ok())
                .collect();
            for alias in src_aliases {
                if !alias_strings.contains(&alias) {
                    alias_strings.push(alias);
                }
            }
        }
        if !alias_strings.is_empty() {
            fm.insert(
                "aliases".to_string(),
                Pod::Array(alias_strings.clone().into_iter().map(Pod::String).collect()),
            );
        }

        // Union remaining source frontmatter fields (dest wins on conflicts; id/tags/aliases are
        // excluded because they're handled above or must not be inherited from sources).
        const SKIP_KEYS: &[&str] = &["id", "tags", "aliases"];
        for source in sources {
            if let Some(sfm) = &source.frontmatter {
                for (k, v) in sfm {
                    if !SKIP_KEYS.contains(&k.as_str()) {
                        fm.entry(k.clone()).or_insert_with(|| v.clone());
                    }
                }
            }
        }

        let merged_frontmatter = if fm.is_empty() { None } else { Some(fm) };

        Ok(MergeOp {
            dest_is_new,
            merged_content,
            merged_frontmatter,
            merged_tags: tag_strings
                .into_iter()
                .map(|tag| LocatedTag {
                    tag,
                    location: Location::Frontmatter,
                })
                .collect(),
            merged_aliases: alias_strings,
            per_note_replacements,
        })
    }

    /// Merges `sources` into `dest_path`: appends each source's body to the destination,
    /// union-merges tags and aliases, rewrites all backlinks to sources in other notes to
    /// point to the destination, and deletes the source files.
    ///
    /// The destination is created if it doesn't exist, or its content is appended to if it does.
    /// Returns the resulting destination [`Note`].
    pub fn merge(&mut self, sources: &[Note], dest_path: &impl AsRef<Path>) -> Result<Note, VaultError> {
        let dest_path = common::normalize_path(dest_path, Some(&self.path));
        let op = self.compute_merge_op(sources, &dest_path)?;

        // Build and write destination note.
        if op.dest_is_new {
            let mut dest = Note::builder(dest_path.clone())?
                .aliases(&op.merged_aliases)
                .located_tags(&op.merged_tags)
                .build()?;
            dest.update_content(Some(&op.merged_content), op.merged_frontmatter)?;
            dest.write()?;
        } else {
            let mut dest = Note::from_path(&dest_path)?;
            dest.update_content(Some(&op.merged_content), op.merged_frontmatter)?;
            dest.write()?;
        };
        self.refresh_cached_note(&dest_path)?;

        // Rewrite backlinks in external notes.
        for (note_path, replacements) in op.per_note_replacements {
            let raw_content = self.text_for_path(&note_path)?;
            let new_content = common::rewrite_links(&raw_content, replacements);
            std::fs::write(&note_path, new_content)?;
            self.refresh_cached_note(&note_path)?;
        }

        // Delete source files.
        for source in sources {
            std::fs::remove_file(&source.path)?;
            self.remove_cached_note(&source.path);
        }

        self.note_for_path(&dest_path)
    }

    /// Returns a preview of what [`merge`](Self::merge) would change without touching the filesystem.
    ///
    /// Same validation and error variants as `merge`.
    pub fn merge_preview(&self, sources: &[Note], dest_path: impl AsRef<Path>) -> Result<MergePreview, VaultError> {
        let dest_path = common::normalize_path(dest_path, Some(&self.path));
        let op = self.compute_merge_op(sources, &dest_path)?;

        let mut updated_notes: Vec<(PathBuf, usize)> = op
            .per_note_replacements
            .iter()
            .map(|(path, reps)| (path.clone(), reps.len()))
            .collect();
        updated_notes.sort_by(|(a, _), (b, _)| a.cmp(b));

        Ok(MergePreview {
            dest_path: dest_path.to_path_buf(),
            dest_is_new: op.dest_is_new,
            sources: sources.iter().map(|s| s.path.clone()).collect(),
            updated_notes,
        })
    }
}

struct RenameOp {
    new_stem: String,
    frontmatter_id_will_update: bool,
    /// Only notes with ≥1 replacement included.
    per_note_replacements: Vec<(Note, Vec<(LocatedLink, String)>)>,
}

/// Public summary of what a rename would change, without touching the filesystem.
pub struct RenamePreview {
    pub new_path: PathBuf,
    pub id_will_update: bool,
    /// Notes with backlinks that would be rewritten, sorted by path. Each entry is (path, link_count).
    pub updated_notes: Vec<(PathBuf, usize)>,
}

/// Exact text changes required for backlink rewrites during a note rename.
pub struct RenameEdits {
    pub new_path: PathBuf,
    pub new_stem: String,
    pub id_will_update: bool,
    /// Notes with backlinks that would be rewritten, sorted by path.
    pub backlink_edits: Vec<(PathBuf, Vec<(LocatedLink, String)>)>,
}

/// Character-based span within a note's body text.
///
/// Lines are 1-indexed, columns are 0-indexed, and the end position is exclusive.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TextSpan {
    pub start_line: usize,
    pub start_col: usize,
    pub end_line: usize,
    pub end_col: usize,
}

/// Selection target for note extraction.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ExtractSelection {
    Section(String),
    Span(TextSpan),
}

/// Result of extracting content from one note into another.
#[derive(Clone)]
pub struct ExtractResult {
    pub source_note: Note,
    pub new_note: Note,
}

/// Exact file contents that note extraction would write.
#[derive(Clone)]
pub struct ExtractEdits {
    pub source_path: PathBuf,
    pub source_content: String,
    pub source_note: Note,
    pub new_path: PathBuf,
    pub new_content: String,
    pub new_note: Note,
}

/// Public summary of what a merge would change, without touching the filesystem.
pub struct MergePreview {
    pub dest_path: PathBuf,
    pub dest_is_new: bool,
    /// Source paths that would be deleted.
    pub sources: Vec<PathBuf>,
    /// Notes with backlinks to any source that would be rewritten, sorted by path. Each entry is (path, link_count).
    pub updated_notes: Vec<(PathBuf, usize)>,
}

struct MergeOp {
    dest_is_new: bool,
    /// Combined body content for the destination note (no leading whitespace).
    merged_content: String,
    /// Merged frontmatter for the destination note.
    merged_frontmatter: Option<IndexMap<String, Pod>>,
    /// Merged frontmatter tags
    merged_tags: Vec<LocatedTag>,
    /// Merged aliases
    merged_aliases: Vec<String>,
    /// External notes (not sources, not dest) with backlinks to rewrite.
    per_note_replacements: Vec<(PathBuf, Vec<(LocatedLink, String)>)>,
}

struct ExtractOp {
    source_raw: String,
    source_note: Note,
    new_raw: String,
    new_note: Note,
}

struct ResolvedExtract {
    extracted_range: Range<usize>,
    source_replace_range: Range<usize>,
    section_root_level: Option<usize>,
}

struct ResolvedSection {
    start_byte: usize,
    body_start_byte: usize,
    end_byte: usize,
    level: usize,
}

struct HeadingFragmentSegment {
    raw: String,
    normalized: String,
}

struct HeadingPathSegment {
    text: String,
    normalized_anchor: String,
    resolved_anchor: String,
}

fn resolve_extract_selection(
    note_path: &Path,
    raw_source: &str,
    selection: &ExtractSelection,
) -> Result<ResolvedExtract, VaultError> {
    let (raw_frontmatter, raw_body) = Vault::raw_note_sections(note_path, raw_source);
    let body_start = raw_frontmatter.len();

    match selection {
        ExtractSelection::Section(section) => {
            let resolved = resolve_section_bounds(note_path, raw_body, section)?;
            Ok(ResolvedExtract {
                extracted_range: (body_start + resolved.start_byte)..(body_start + resolved.end_byte),
                source_replace_range: (body_start + resolved.body_start_byte)..(body_start + resolved.end_byte),
                section_root_level: Some(resolved.level),
            })
        }
        ExtractSelection::Span(span) => {
            let start = line_col_to_byte_index(raw_source, span.start_line, span.start_col).ok_or_else(|| {
                invalid_extract_span(
                    note_path,
                    format!(
                        "start position {}:{} is outside the note",
                        span.start_line, span.start_col
                    ),
                )
            })?;
            let end = line_col_to_byte_index(raw_source, span.end_line, span.end_col).ok_or_else(|| {
                invalid_extract_span(
                    note_path,
                    format!("end position {}:{} is outside the note", span.end_line, span.end_col),
                )
            })?;
            if start >= end {
                return Err(invalid_extract_span(
                    note_path,
                    "start position must come before end position".to_string(),
                ));
            }
            if start < body_start || end < body_start {
                return Err(invalid_extract_span(
                    note_path,
                    "span overlaps frontmatter; only note body text can be extracted".to_string(),
                ));
            }
            Ok(ResolvedExtract {
                extracted_range: start..end,
                source_replace_range: start..end,
                section_root_level: None,
            })
        }
    }
}

fn resolve_section_bounds(note_path: &Path, raw_body: &str, section: &str) -> Result<ResolvedSection, VaultError> {
    let expected = parse_heading_fragment_segments(section);
    if expected.is_empty() {
        return Err(VaultError::SectionNotFound {
            path: note_path.to_path_buf(),
            section: section.to_string(),
        });
    }

    let lines: Vec<&str> = raw_body.split_inclusive('\n').collect();
    let mut line_starts = Vec::with_capacity(lines.len());
    let mut offset = 0;
    for line in &lines {
        line_starts.push(offset);
        offset += line.len();
    }

    let mut seen_anchors = HashMap::new();
    let mut current_path = Vec::new();
    let mut matches = Vec::new();
    let mut in_fenced_code = false;

    for (index, line) in lines.iter().enumerate() {
        let (line_content, _) = split_line_ending(line);
        if is_fence_line(line_content) {
            in_fenced_code = !in_fenced_code;
            continue;
        }
        if in_fenced_code {
            continue;
        }

        let Some((level, heading_text)) = heading_line_parts(line_content) else {
            continue;
        };
        let Some(resolved_anchor) = resolve_heading_anchor(&heading_text, &mut seen_anchors) else {
            continue;
        };
        let normalized_anchor = normalize_heading_anchor(&heading_text);

        current_path.truncate(level.saturating_sub(1));
        current_path.push(HeadingPathSegment {
            text: heading_text,
            normalized_anchor,
            resolved_anchor,
        });

        if heading_path_matches(&current_path, &expected) {
            matches.push(ResolvedSection {
                start_byte: line_starts[index],
                body_start_byte: line_starts[index] + line.len(),
                end_byte: find_section_end(&lines, index + 1, level, raw_body.len()),
                level,
            });
        }
    }

    match matches.len() {
        0 => Err(VaultError::SectionNotFound {
            path: note_path.to_path_buf(),
            section: section.to_string(),
        }),
        1 => Ok(matches.remove(0)),
        _ => Err(VaultError::AmbiguousSection {
            path: note_path.to_path_buf(),
            section: section.to_string(),
        }),
    }
}

fn find_section_end(lines: &[&str], start_index: usize, level: usize, raw_len: usize) -> usize {
    let mut offset = lines.iter().take(start_index).map(|line| line.len()).sum();
    let mut in_fenced_code = false;

    for line in lines.iter().skip(start_index) {
        let (line_content, _) = split_line_ending(line);
        if is_fence_line(line_content) {
            in_fenced_code = !in_fenced_code;
            offset += line.len();
            continue;
        }
        if !in_fenced_code
            && let Some((next_level, _)) = heading_line_parts(line_content)
            && next_level <= level
        {
            return offset;
        }
        offset += line.len();
    }

    raw_len
}

fn parse_heading_fragment_segments(heading: &str) -> Vec<HeadingFragmentSegment> {
    heading
        .split('#')
        .filter(|segment| !segment.is_empty())
        .map(|segment| HeadingFragmentSegment {
            raw: segment.to_string(),
            normalized: normalize_heading_anchor(segment),
        })
        .collect()
}

fn heading_path_matches(path: &[HeadingPathSegment], expected: &[HeadingFragmentSegment]) -> bool {
    if expected.len() > path.len() {
        return false;
    }

    path[path.len() - expected.len()..]
        .iter()
        .zip(expected.iter())
        .all(|(candidate, expected_segment)| heading_segment_matches(candidate, expected_segment))
}

fn heading_segment_matches(candidate: &HeadingPathSegment, expected: &HeadingFragmentSegment) -> bool {
    candidate.text == expected.raw
        || candidate.normalized_anchor == expected.normalized
        || candidate.resolved_anchor == expected.normalized
}

fn heading_line_parts(line: &str) -> Option<(usize, String)> {
    let trimmed = line.trim_start();
    if !trimmed.starts_with('#') {
        return None;
    }

    let marker_bytes = trimmed
        .char_indices()
        .take_while(|(_, ch)| *ch == '#')
        .last()
        .map_or(0, |(index, ch)| index + ch.len_utf8());
    let level = trimmed[..marker_bytes].chars().count();
    let after_markers = &trimmed[marker_bytes..];
    let content = after_markers.trim_start();
    if content.is_empty() {
        return None;
    }

    let heading_text = strip_optional_heading_closing_hashes(content);
    if heading_text.is_empty() {
        return None;
    }

    Some((level, heading_text.to_string()))
}

fn heading_marker_span(line: &str) -> Option<(Range<usize>, usize)> {
    let trimmed = line.trim_start();
    if !trimmed.starts_with('#') {
        return None;
    }

    let marker_bytes = trimmed
        .char_indices()
        .take_while(|(_, ch)| *ch == '#')
        .last()
        .map_or(0, |(index, ch)| index + ch.len_utf8());
    let level = trimmed[..marker_bytes].chars().count();
    let after_markers = &trimmed[marker_bytes..];
    let content = after_markers.trim_start();
    if content.is_empty() {
        return None;
    }

    let heading_text = strip_optional_heading_closing_hashes(content);
    if heading_text.is_empty() {
        return None;
    }

    let leading_bytes = line.len() - trimmed.len();
    Some((leading_bytes..leading_bytes + marker_bytes, level))
}

fn strip_optional_heading_closing_hashes(text: &str) -> &str {
    let trimmed = text.trim_end();
    let without_hashes = trimmed.trim_end_matches('#');
    if without_hashes.len() == trimmed.len() || !without_hashes.chars().last().is_some_and(char::is_whitespace) {
        return trimmed;
    }

    without_hashes.trim_end()
}

fn normalize_heading_anchor(text: &str) -> String {
    let mut anchor = String::new();
    let mut last_was_separator = true;

    for ch in text.trim().chars().flat_map(char::to_lowercase) {
        if ch.is_alphanumeric() || ch == '_' {
            anchor.push(ch);
            last_was_separator = false;
        } else if (ch.is_whitespace() || ch == '-') && !last_was_separator && !anchor.is_empty() {
            anchor.push('-');
            last_was_separator = true;
        }
    }

    while anchor.ends_with('-') {
        anchor.pop();
    }

    anchor
}

fn resolve_heading_anchor(heading_text: &str, seen_anchors: &mut HashMap<String, usize>) -> Option<String> {
    let base_anchor = normalize_heading_anchor(heading_text);
    if base_anchor.is_empty() {
        return None;
    }

    let seen_count = seen_anchors.entry(base_anchor.clone()).or_default();
    let anchor = if *seen_count == 0 {
        base_anchor
    } else {
        format!("{base_anchor}-{seen_count}")
    };
    *seen_count += 1;

    Some(anchor)
}

fn normalize_section_heading_levels(content: &str, root_level: usize) -> String {
    if root_level <= 1 {
        return content.to_string();
    }

    let shift = root_level.saturating_sub(1);
    let mut normalized = String::with_capacity(content.len());
    let mut in_fenced_code = false;

    for line in content.split_inclusive('\n') {
        let (line_content, line_ending) = split_line_ending(line);
        if is_fence_line(line_content) {
            in_fenced_code = !in_fenced_code;
            normalized.push_str(line);
            continue;
        }
        if in_fenced_code {
            normalized.push_str(line);
            continue;
        }

        let Some((marker_range, level)) = heading_marker_span(line_content) else {
            normalized.push_str(line);
            continue;
        };
        let new_level = level.saturating_sub(shift).max(1);
        normalized.push_str(&line_content[..marker_range.start]);
        normalized.push_str(&"#".repeat(new_level));
        normalized.push_str(&line_content[marker_range.end..]);
        normalized.push_str(line_ending);
    }

    normalized
}

fn rewrite_relative_markdown_links(content: &str, source_path: &Path, new_path: &Path) -> String {
    let replacements = crate::link::parse_links(content)
        .into_iter()
        .filter_map(|located_link| {
            let Link::Markdown { text, url } = located_link.link.clone() else {
                return None;
            };
            let new_url = rewrite_relative_markdown_url(source_path, new_path, &url)?;
            Some((located_link, format!("[{text}]({new_url})")))
        })
        .collect::<Vec<_>>();

    if replacements.is_empty() {
        content.to_string()
    } else {
        common::rewrite_links(content, replacements)
    }
}

fn rewrite_relative_markdown_url(source_path: &Path, new_path: &Path, url: &str) -> Option<String> {
    if !is_relative_markdown_url(url) {
        return None;
    }

    let (path_part, fragment) = match url.split_once('#') {
        Some((path, fragment)) => (path, Some(fragment)),
        None => (url, None),
    };
    let source_dir = source_path.parent().unwrap_or(source_path);
    let target_path = if path_part.is_empty() {
        source_path.to_path_buf()
    } else {
        common::normalize_path(source_dir.join(common::percent_decode(path_part)), None)
    };
    let target_dir = new_path.parent().unwrap_or_else(|| Path::new("."));
    let mut new_url = common::relative_path(target_dir, &target_path)
        .to_string_lossy()
        .replace('\\', "/");
    if let Some(fragment) = fragment
        && !fragment.is_empty()
    {
        new_url.push('#');
        new_url.push_str(fragment);
    }
    Some(new_url)
}

fn is_relative_markdown_url(url: &str) -> bool {
    if url.starts_with('/') {
        return false;
    }

    let path_part = url.split('#').next().unwrap_or(url);
    if let Some((scheme, _)) = path_part.split_once(':')
        && !scheme.is_empty()
        && scheme.chars().next().is_some_and(|ch| ch.is_ascii_alphabetic())
        && scheme
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || ch == '+' || ch == '-' || ch == '.')
    {
        return false;
    }

    true
}

fn replace_text_range(raw: &str, range: Range<usize>, replacement: &str) -> String {
    let mut updated = String::with_capacity(raw.len() - (range.end - range.start) + replacement.len());
    updated.push_str(&raw[..range.start]);
    updated.push_str(replacement);
    updated.push_str(&raw[range.end..]);
    updated
}

fn replace_section_body(raw: &str, range: Range<usize>, replacement: &str) -> String {
    let prefix = &raw[..range.start];
    let suffix = &raw[range.end..];
    let mut updated = String::with_capacity(raw.len() - (range.end - range.start) + replacement.len() + 2);
    updated.push_str(prefix);
    if !replacement.is_empty() {
        if !prefix.ends_with('\n') && !replacement.starts_with('\n') {
            updated.push('\n');
        }
        updated.push_str(replacement);
        if !suffix.is_empty() && !replacement.ends_with('\n') {
            updated.push('\n');
        }
    }
    updated.push_str(suffix);
    updated
}

fn build_extracted_note(new_path: &Path, body: &str, new_id: Option<&str>) -> Result<Note, VaultError> {
    let mut builder = Note::builder(new_path)?;
    if let Some(id) = new_id.map(str::trim).filter(|id| !id.is_empty()) {
        builder = builder.id(id);
    }
    builder.body(body).build().map_err(VaultError::Note)
}

fn line_col_to_byte_index(text: &str, line: usize, col: usize) -> Option<usize> {
    if line == 0 {
        return None;
    }

    let mut line_starts = vec![0];
    for (index, ch) in text.char_indices() {
        if ch == '\n' {
            line_starts.push(index + 1);
        }
    }

    let start = *line_starts.get(line - 1)?;
    let line_end = line_starts
        .get(line)
        .copied()
        .map(|next| next - 1)
        .unwrap_or(text.len());
    let mut line_text = &text[start..line_end];
    if let Some(stripped) = line_text.strip_suffix('\r') {
        line_text = stripped;
    }

    let char_count = line_text.chars().count();
    if col > char_count {
        return None;
    }
    if col == char_count {
        return Some(start + line_text.len());
    }

    for (seen, (offset, _)) in line_text.char_indices().enumerate() {
        if seen == col {
            return Some(start + offset);
        }
    }

    Some(start + line_text.len())
}

fn split_line_ending(line: &str) -> (&str, &str) {
    if let Some(without_newline) = line.strip_suffix('\n') {
        if let Some(without_crlf) = without_newline.strip_suffix('\r') {
            (without_crlf, "\r\n")
        } else {
            (without_newline, "\n")
        }
    } else {
        (line, "")
    }
}

fn is_fence_line(line: &str) -> bool {
    let trimmed = line.trim_start();
    trimmed.starts_with("```") || trimmed.starts_with("~~~")
}

fn invalid_extract_span(path: &Path, message: String) -> VaultError {
    VaultError::InvalidExtractSpan {
        path: path.to_path_buf(),
        message,
    }
}

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

    // --- constructor tests ---

    #[test]
    fn open_from_cwd_finds_obsidian_dir() {
        let dir = tempfile::tempdir().unwrap();
        let subdir = dir.path().join("notes/daily");
        fs::create_dir_all(&subdir).unwrap();
        fs::create_dir(dir.path().join(".obsidian")).unwrap();

        let original_cwd = std::env::current_dir().unwrap();
        std::env::set_current_dir(&subdir).unwrap();
        let vault = Vault::open_from_cwd().unwrap();
        std::env::set_current_dir(original_cwd).unwrap();

        assert_eq!(vault.path.canonicalize().unwrap(), dir.path().canonicalize().unwrap());
    }

    #[test]
    fn cached_vault_refresh_path_adds_created_note_and_updates_health() {
        let vault_dir = tempfile::tempdir().unwrap();
        fs::create_dir(vault_dir.path().join(".obsidian")).unwrap();
        let source_path = vault_dir.path().join("source.md");
        fs::write(&source_path, "See [[target]].").unwrap();

        let mut vault = Vault::open_cached(vault_dir.path()).unwrap();
        assert_eq!(vault.notes().len(), 1);
        assert_eq!(vault.check(|_| true).broken_links.len(), 1);

        let target_path = vault_dir.path().join("target.md");
        fs::write(&target_path, "---\nid: target\n---\n").unwrap();
        assert!(vault.refresh_cached_note(&target_path).unwrap());

        assert_eq!(vault.notes().len(), 2);
        assert!(vault.check(|_| true).broken_links.is_empty());
    }

    #[test]
    fn cached_vault_remove_path_removes_note_and_updates_health() {
        let vault_dir = tempfile::tempdir().unwrap();
        fs::create_dir(vault_dir.path().join(".obsidian")).unwrap();
        let source_path = vault_dir.path().join("source.md");
        fs::write(&source_path, "See [[target]].").unwrap();
        let target_path = vault_dir.path().join("target.md");
        fs::write(&target_path, "---\nid: target\n---\n").unwrap();

        let mut vault = Vault::open_cached(vault_dir.path()).unwrap();
        assert!(vault.check(|_| true).broken_links.is_empty());

        let target_path = target_path.canonicalize().unwrap();
        fs::remove_file(&target_path).unwrap();
        assert!(vault.remove_cached_note(&target_path));

        let report = vault.check(|_| true);
        assert_eq!(report.note_count, 1);
        assert_eq!(report.broken_links.len(), 1);
    }

    #[test]
    fn cached_vault_health_checks_markdown_links_against_cached_disk_notes() {
        let vault_dir = tempfile::tempdir().unwrap();
        fs::create_dir(vault_dir.path().join(".obsidian")).unwrap();
        let source_path = vault_dir.path().join("source.md");
        fs::write(&source_path, "See [target](target.md).").unwrap();
        let target_path = vault_dir.path().join("target.md");
        fs::write(&target_path, "---\nid: target\n---\n").unwrap();

        let mut vault = Vault::open_cached(vault_dir.path()).unwrap();
        let target_path = target_path.canonicalize().unwrap();
        fs::remove_file(&target_path).unwrap();

        assert!(
            vault.check(|_| true).broken_links.is_empty(),
            "cached target should keep markdown link valid until cache is updated"
        );

        assert!(vault.remove_cached_note(&target_path));
        assert_eq!(vault.check(|_| true).broken_links.len(), 1);
    }

    #[test]
    fn cached_vault_search_uses_cached_disk_notes_until_refresh() {
        let vault_dir = tempfile::tempdir().unwrap();
        fs::create_dir(vault_dir.path().join(".obsidian")).unwrap();
        let note_path = vault_dir.path().join("note.md");
        fs::write(
            &note_path,
            "---\nid: cached-id\ntags: [cached]\n---\n\ncached body #cached-inline\n",
        )
        .unwrap();

        let mut vault = Vault::open_cached(vault_dir.path()).unwrap();
        fs::write(
            &note_path,
            "---\nid: fresh-id\ntags: [fresh]\n---\n\nfresh body #fresh-inline\n",
        )
        .unwrap();

        let cached_id_results = vault.search().or_has_id("cached-id").execute().unwrap();
        assert_eq!(cached_id_results.into_iter().filter_map(Result::ok).count(), 1);
        assert!(
            vault
                .search()
                .or_has_id("fresh-id")
                .execute()
                .unwrap()
                .into_iter()
                .filter_map(Result::ok)
                .next()
                .is_none()
        );

        let cached_content_results = vault.search().and_content_contains("cached body").execute().unwrap();
        assert_eq!(cached_content_results.into_iter().filter_map(Result::ok).count(), 1);
        assert!(
            vault
                .search()
                .and_content_contains("fresh body")
                .execute()
                .unwrap()
                .into_iter()
                .filter_map(Result::ok)
                .next()
                .is_none()
        );

        assert_eq!(vault.find_tags(&["cached".to_string()]).unwrap().len(), 1);
        assert!(vault.find_tags(&["fresh".to_string()]).unwrap().is_empty());

        assert!(vault.refresh_cached_note(&note_path).unwrap());
        let fresh_id_results = vault.search().or_has_id("fresh-id").execute().unwrap();
        assert_eq!(fresh_id_results.into_iter().filter_map(Result::ok).count(), 1);
        assert_eq!(vault.find_tags(&["fresh".to_string()]).unwrap().len(), 1);
    }

    #[test]
    fn open_from_cwd_falls_back_to_cwd_when_no_obsidian_dir() {
        let dir = tempfile::tempdir().unwrap();

        let original_cwd = std::env::current_dir().unwrap();
        std::env::set_current_dir(&dir).unwrap();
        let vault = Vault::open_from_cwd().unwrap();
        std::env::set_current_dir(original_cwd).unwrap();

        assert_eq!(vault.path.canonicalize().unwrap(), dir.path().canonicalize().unwrap());
    }

    #[test]
    fn open_valid_directory() {
        let dir = tempfile::tempdir().unwrap();
        // std::fs::create_dir(&dir).unwrap();
        let vault = Vault::open(dir.path()).expect("should open valid directory");
        assert_eq!(vault.path, common::normalize_path(dir.path(), None));
    }

    #[test]
    fn open_nonexistent_path_errors() {
        let result = Vault::open("/nonexistent/path/to/vault");
        assert!(result.is_err());
    }

    #[test]
    fn open_file_path_errors() {
        let file = tempfile::NamedTempFile::new().unwrap();
        let result = Vault::open(file.path());
        assert!(result.is_err());
    }

    // --- note resolution tests ---

    #[test]
    fn resolve_note_by_filename() {
        let dir = tempfile::tempdir().unwrap();
        let subdir = dir.path().join("subdir");
        fs::create_dir(&subdir).unwrap();
        fs::write(dir.path().join("root.md"), "---\nid: root\n---\n\nRoot note.").unwrap();
        fs::write(subdir.join("nested.md"), "---\nid: nested\n---\n\nNested note.").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = vault.resolve_note("root.md").expect("should resolve root.md");
        assert_eq!(note.id, "root");

        let note = vault
            .resolve_note("nested.md")
            .expect("should resolve subdir/nested.md");
        assert_eq!(note.id, "nested");
    }

    #[test]
    fn resolve_note_by_alias_exact_match() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(
            dir.path().join("note_a.md"),
            "---\nid: note_a\naliases: [Foo, A]\n---\n\nNote A.",
        )
        .unwrap();
        fs::write(
            dir.path().join("note_b.md"),
            "---\nid: note_b\naliases: [foo, B]\n---\n\nNote B.",
        )
        .unwrap();

        let vault = Vault::open(dir.path()).unwrap();

        let note = vault.resolve_note("Foo").expect("should resolve note");
        assert_eq!(note.id, "note_a");

        let note = vault.resolve_note("foo").expect("should resolve note");
        assert_eq!(note.id, "note_b");
    }

    // --- note loading tests ---

    #[test]
    fn notes_loads_md_files() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("a.md"), "# Note A\n\nContent A.").unwrap();
        fs::write(dir.path().join("b.md"), "# Note B\n\nContent B.").unwrap();
        fs::write(dir.path().join("not-a-note.txt"), "ignored").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let notes: Vec<Note> = vault.notes().into_iter().map(|r| r.unwrap()).collect();
        assert_eq!(notes.len(), 2);
    }

    #[test]
    fn notes_finds_nested_md_files() {
        let dir = tempfile::tempdir().unwrap();
        let subdir = dir.path().join("subdir");
        fs::create_dir(&subdir).unwrap();
        fs::write(dir.path().join("root.md"), "Root note.").unwrap();
        fs::write(subdir.join("nested.md"), "Nested note.").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let notes: Vec<Note> = vault.notes().into_iter().map(|r| r.unwrap()).collect();
        assert_eq!(notes.len(), 2);
    }

    // --- backlinks tests ---

    #[test]
    fn backlinks_wiki_by_id() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "---\nid: my-id\n---\nTarget.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[my-id]].").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert_eq!(backlinks.len(), 1);
        assert!(backlinks[0].0.path.ends_with("source.md"));
        assert_eq!(backlinks[0].1.len(), 1);
    }

    #[test]
    fn backlinks_wiki_by_stem_when_id_differs() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("my-note.md"), "---\nid: custom-id\n---\nTarget.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[my-note]].").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("my-note.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert_eq!(backlinks.len(), 1);
        assert!(backlinks[0].0.path.ends_with("source.md"));
    }

    #[test]
    fn backlinks_wiki_by_alias() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "---\naliases: [t-alias]\n---\nTarget.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[t-alias]].").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert_eq!(backlinks.len(), 1);
    }

    #[test]
    fn backlinks_wiki_by_title() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "# My Title\n\nContent.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[My Title]].").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert_eq!(backlinks.len(), 1);
    }

    #[test]
    fn backlinks_wiki_with_heading_suffix() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[target#section]].").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert_eq!(backlinks.len(), 1);
    }

    #[test]
    fn backlinks_excludes_self() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Self link: [[target]].").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert!(backlinks.is_empty());
    }

    #[test]
    fn backlinks_excludes_notes_with_no_match() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(dir.path().join("other.md"), "No links here.").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert!(backlinks.is_empty());
    }

    #[test]
    fn backlinks_returns_all_matching_links_from_one_note() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[target]] and also [[target]].").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert_eq!(backlinks.len(), 1);
        assert_eq!(backlinks[0].1.len(), 2);
    }

    #[test]
    fn backlinks_no_match_on_unrelated_wiki_link() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[other-note]].").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert!(backlinks.is_empty());
    }

    #[test]
    fn backlinks_markdown_relative_path() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(dir.path().join("source.md"), "[link](target.md)").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert_eq!(backlinks.len(), 1);
        assert!(backlinks[0].0.path.ends_with("source.md"));
    }

    #[test]
    fn backlinks_markdown_fragment_stripped() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(dir.path().join("source.md"), "[link](target.md#section)").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert_eq!(backlinks.len(), 1);
    }

    #[test]
    fn backlinks_markdown_parent_traversal() {
        let dir = tempfile::tempdir().unwrap();
        let subdir = dir.path().join("sub");
        fs::create_dir(&subdir).unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(subdir.join("source.md"), "[link](../target.md)").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert_eq!(backlinks.len(), 1);
    }

    #[test]
    fn backlinks_markdown_external_url_excluded() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(dir.path().join("source.md"), "[link](https://example.com/target.md)").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert!(backlinks.is_empty());
    }

    #[test]
    fn backlinks_markdown_absolute_path_excluded() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(dir.path().join("source.md"), "[link](/absolute/target.md)").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert!(backlinks.is_empty());
    }

    #[test]
    fn backlinks_markdown_extension_less_excluded() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(dir.path().join("source.md"), "[link](target)").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let target = Note::from_path(dir.path().join("target.md")).unwrap();
        let backlinks = vault.backlinks(&target).unwrap();

        assert!(backlinks.is_empty());
    }

    // --- rename tag tests ---

    #[test]
    fn rename_tag_basic() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(
            dir.path().join("note.md"),
            "---\nid: note\ntags:\n- foo\n- old-tag\n---\n\nHello world #old-tag here and #old-tag there.",
        )
        .unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        vault.rename_tag("old-tag", "new-tag").unwrap();

        let content = fs::read_to_string(dir.path().join("note.md")).unwrap();
        assert_eq!(
            content,
            "---\nid: note\ntags:\n- foo\n- new-tag\n---\n\nHello world #new-tag here and #new-tag there."
        );
    }

    // --- patch_note tests ---

    #[test]
    fn patch_note_replaces_string() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("note.md"), "Hello world.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("note.md")).unwrap();
        vault.patch_note(&note, "world", "Rust").unwrap();

        let content = fs::read_to_string(dir.path().join("note.md")).unwrap();
        assert_eq!(content, "Hello Rust.");
    }

    #[test]
    fn patch_note_string_not_found_errors() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("note.md"), "Hello world.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("note.md")).unwrap();
        let result = vault.patch_note(&note, "missing", "replacement");

        assert!(matches!(result, Err(VaultError::StringNotFound(_))));
    }

    #[test]
    fn patch_note_multiple_matches_errors() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("note.md"), "foo and foo").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("note.md")).unwrap();
        let result = vault.patch_note(&note, "foo", "bar");

        assert!(matches!(result, Err(VaultError::StringFoundMultipleTimes(_))));
    }

    #[test]
    fn patch_note_preserves_explicit_empty_frontmatter_arrays() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("note.md"), "---\ntags: []\n---\n\nHello world.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("note.md")).unwrap();
        let patched = vault.patch_note(&note, "world", "Rust").unwrap();

        let content = fs::read_to_string(dir.path().join("note.md")).unwrap();
        assert_eq!(content, "---\ntags: []\n---\n\nHello Rust.");
        assert_eq!(
            patched.frontmatter_json().unwrap().get("tags"),
            Some(&serde_json::json!([]))
        );
    }

    #[test]
    fn patch_note_does_not_work_in_frontmatter() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("note.md"), "---\ntitle: Old Title\n---\nBody.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("note.md")).unwrap();
        assert!(vault.patch_note(&note, "Old Title", "New Title").is_err());
    }

    #[test]
    fn patch_note_returns_reloaded_note() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("note.md"), "---\ntitle: Before\n---\n# Before\nBody.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("note.md")).unwrap();
        let patched = vault.patch_note(&note, "Before", "After").unwrap();

        assert_eq!(patched.body(), "# After\nBody.");
    }

    // --- append_to_note tests ---

    #[test]
    fn append_to_note_appends_content_and_returns_reloaded_note() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("note.md"), "Hello.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("note.md")).unwrap();
        let appended = vault.append_to_note(&note, "\nWorld.").unwrap();

        let content = fs::read_to_string(dir.path().join("note.md")).unwrap();
        assert_eq!(content, "Hello.\nWorld.");
        assert_eq!(appended.body(), "Hello.\nWorld.");
    }

    #[test]
    fn append_to_note_preserves_explicit_empty_frontmatter_arrays() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("note.md"), "---\ntags: []\naliases: []\n---\n\nHello.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("note.md")).unwrap();
        let appended = vault.append_to_note(&note, "\nWorld.").unwrap();

        let content = fs::read_to_string(dir.path().join("note.md")).unwrap();
        assert_eq!(content, "---\ntags: []\naliases: []\n---\n\nHello.\nWorld.");
        assert_eq!(
            appended.frontmatter_json().unwrap().get("tags"),
            Some(&serde_json::json!([]))
        );
        assert_eq!(
            appended.frontmatter_json().unwrap().get("aliases"),
            Some(&serde_json::json!([]))
        );
    }

    #[test]
    fn append_to_note_reparses_inline_links_and_tags() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("note.md"), "Start.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("note.md")).unwrap();
        let appended = vault.append_to_note(&note, "\nSee [[target]]. #new-tag").unwrap();

        assert_eq!(appended.links.len(), 1);
        assert!(
            appended
                .tags
                .iter()
                .any(|tag| { tag.tag == "new-tag" && matches!(tag.location, Location::Inline(_)) })
        );
    }

    // --- extract_to_note tests ---

    #[test]
    fn extract_span_creates_new_note_and_replaces_source_text() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path().join("source.md");
        fs::write(&source_path, "---\ntags: []\n---\n\nHello world.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(&source_path).unwrap();
        let result = vault
            .extract_to_note(
                &note,
                &ExtractSelection::Span(TextSpan {
                    start_line: 5,
                    start_col: 6,
                    end_line: 5,
                    end_col: 11,
                }),
                dir.path().join("new.md"),
                None,
                None,
            )
            .unwrap();

        assert_eq!(
            fs::read_to_string(&source_path).unwrap(),
            "---\ntags: []\n---\n\nHello [[new]]."
        );
        assert_eq!(
            fs::read_to_string(dir.path().join("new.md")).unwrap(),
            "---\nid: new\n---\n\nworld"
        );
        assert_eq!(result.source_note.body(), "Hello [[new]].");
        assert_eq!(result.new_note.id, "new");
    }

    #[test]
    fn extract_span_rewrites_relative_markdown_links() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path().join("journal/daily/source.md");
        fs::create_dir_all(source_path.parent().unwrap()).unwrap();
        fs::create_dir_all(dir.path().join("notes")).unwrap();
        fs::write(dir.path().join("notes/topic.md"), "# Topic\n").unwrap();
        let line = "See [Target](../../notes/topic.md#section).";
        fs::write(&source_path, line).unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(&source_path).unwrap();
        vault
            .extract_to_note(
                &note,
                &ExtractSelection::Span(TextSpan {
                    start_line: 1,
                    start_col: 0,
                    end_line: 1,
                    end_col: line.chars().count(),
                }),
                dir.path().join("projects/extract.md"),
                None,
                Some("See [[extract]]."),
            )
            .unwrap();

        let extracted = fs::read_to_string(dir.path().join("projects/extract.md")).unwrap();
        assert!(extracted.contains("[Target](../notes/topic.md#section)"));
    }

    #[test]
    fn extract_section_keeps_source_heading_and_normalizes_extracted_headings() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path().join("source.md");
        fs::write(
            &source_path,
            "# Root\n\n## Section\nIntro\n### Child\nBody\n\n## Next\nStay.\n",
        )
        .unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(&source_path).unwrap();
        vault
            .extract_to_note(
                &note,
                &ExtractSelection::Section("Section".to_string()),
                dir.path().join("section.md"),
                None,
                None,
            )
            .unwrap();

        let source = fs::read_to_string(&source_path).unwrap();
        assert!(source.contains("## Section\n[[section]]\n## Next"));

        let extracted = fs::read_to_string(dir.path().join("section.md")).unwrap();
        assert!(extracted.contains("id: section"));
        assert!(extracted.contains("# Section"));
        assert!(extracted.contains("## Child"));
        assert!(!extracted.contains("### Child"));
    }

    #[test]
    fn extract_section_default_link_uses_new_id() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path().join("source.md");
        fs::write(&source_path, "## Section\nBody.\n").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(&source_path).unwrap();
        vault
            .extract_to_note(
                &note,
                &ExtractSelection::Section("Section".to_string()),
                dir.path().join("section.md"),
                Some("section-id"),
                None,
            )
            .unwrap();

        let source = fs::read_to_string(&source_path).unwrap();
        let extracted = fs::read_to_string(dir.path().join("section.md")).unwrap();
        assert!(source.contains("[[section-id]]"));
        assert!(extracted.contains("id: section-id"));
    }

    #[test]
    fn extract_section_default_id_uses_normalized_filename_id() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path().join("source.md");
        fs::write(&source_path, "## Section\nBody.\n").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(&source_path).unwrap();
        vault
            .extract_to_note(
                &note,
                &ExtractSelection::Section("Section".to_string()),
                dir.path().join("Café Note.md"),
                None,
                None,
            )
            .unwrap();

        let source = fs::read_to_string(&source_path).unwrap();
        let extracted = fs::read_to_string(dir.path().join("Café Note.md")).unwrap();
        assert!(source.contains("[[cafe-note]]"));
        assert!(extracted.contains("id: cafe-note"));
    }

    #[test]
    fn extract_section_duplicate_heading_errors() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path().join("source.md");
        fs::write(
            &source_path,
            "# Root\n\n## Section\nOne.\n\n## Other\nBody.\n\n## Section\nTwo.\n",
        )
        .unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(&source_path).unwrap();
        let error = vault
            .extract_to_note_edits(
                &note,
                &ExtractSelection::Section("Section".to_string()),
                dir.path().join("section.md"),
                None,
                None,
            )
            .err()
            .expect("duplicate sections should error");

        assert!(matches!(error, VaultError::AmbiguousSection { .. }));
    }

    #[test]
    fn extract_span_overlapping_frontmatter_errors() {
        let dir = tempfile::tempdir().unwrap();
        let source_path = dir.path().join("source.md");
        fs::write(&source_path, "---\nid: source\n---\n\nBody.\n").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(&source_path).unwrap();
        let error = vault
            .extract_to_note_edits(
                &note,
                &ExtractSelection::Span(TextSpan {
                    start_line: 2,
                    start_col: 0,
                    end_line: 2,
                    end_col: 2,
                }),
                dir.path().join("frontmatter.md"),
                None,
                None,
            )
            .err()
            .expect("frontmatter spans should error");

        assert!(matches!(error, VaultError::InvalidExtractSpan { .. }));
    }

    // --- rename tests ---

    #[test]
    fn rename_basic() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("old.md"), "Content.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("old.md")).unwrap();
        let renamed = vault.rename(&note, &dir.path().join("new.md")).unwrap();

        assert!(!dir.path().join("old.md").exists());
        assert!(dir.path().join("new.md").exists());
        assert_eq!(renamed.id, "new");
    }

    #[test]
    fn rename_explicit_id_equals_stem_updated() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("old-note.md"), "---\nid: old-note\n---\nContent.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[old-note]].").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("old-note.md")).unwrap();
        let renamed = vault.rename(&note, &dir.path().join("new-note.md")).unwrap();

        assert!(!dir.path().join("old-note.md").exists());
        assert!(dir.path().join("new-note.md").exists());
        assert_eq!(renamed.id, "new-note");

        let source_content = fs::read_to_string(dir.path().join("source.md")).unwrap();
        assert_eq!(source_content, "See [[new-note]].");
    }

    #[test]
    fn rename_explicit_id_differs_from_stem_unchanged() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("my-note.md"), "---\nid: custom-id\n---\nContent.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[my-note]].").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("my-note.md")).unwrap();
        let renamed = vault.rename(&note, &dir.path().join("renamed-note.md")).unwrap();

        assert_eq!(renamed.id, "custom-id");

        // Wiki link targeting the old stem should be unchanged
        let source_content = fs::read_to_string(dir.path().join("source.md")).unwrap();
        assert_eq!(source_content, "See [[my-note]].");
    }

    #[test]
    fn rename_updates_markdown_backlinks() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("old.md"), "Target.").unwrap();
        fs::write(dir.path().join("source.md"), "[link](old.md)").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("old.md")).unwrap();
        vault.rename(&note, &dir.path().join("new.md")).unwrap();

        let source_content = fs::read_to_string(dir.path().join("source.md")).unwrap();
        assert_eq!(source_content, "[link](new.md)");
    }

    #[test]
    fn rename_updates_wiki_backlinks_by_stem() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("old-stem.md"), "Content.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[old-stem]].").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("old-stem.md")).unwrap();
        vault.rename(&note, &dir.path().join("new-stem.md")).unwrap();

        let source_content = fs::read_to_string(dir.path().join("source.md")).unwrap();
        assert_eq!(source_content, "See [[new-stem]].");
    }

    #[test]
    fn rename_leaves_wiki_alias_links_unchanged() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "---\naliases: [my-alias]\n---\nContent.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[my-alias]].").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("target.md")).unwrap();
        vault.rename(&note, &dir.path().join("renamed-target.md")).unwrap();

        let source_content = fs::read_to_string(dir.path().join("source.md")).unwrap();
        assert_eq!(source_content, "See [[my-alias]].");
    }

    #[test]
    fn rename_moves_to_different_directory() {
        let dir = tempfile::tempdir().unwrap();
        let subdir = dir.path().join("sub");
        fs::create_dir(&subdir).unwrap();
        fs::write(dir.path().join("root.md"), "Root.").unwrap();
        fs::write(dir.path().join("source.md"), "[link](root.md)").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("root.md")).unwrap();
        vault.rename(&note, &subdir.join("root.md")).unwrap();

        assert!(!dir.path().join("root.md").exists());
        assert!(subdir.join("root.md").exists());

        let source_content = fs::read_to_string(dir.path().join("source.md")).unwrap();
        assert_eq!(source_content, "[link](sub/root.md)");
    }

    #[test]
    fn rename_directory_not_found_errors() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("old.md"), "Content.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("old.md")).unwrap();
        let result = vault.rename(&note, &dir.path().join("nonexistent/new.md"));

        assert!(matches!(result, Err(VaultError::DirectoryNotFound(_))));
    }

    #[test]
    fn rename_target_already_exists_errors() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("old.md"), "Old.").unwrap();
        fs::write(dir.path().join("new.md"), "Already exists.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("old.md")).unwrap();
        let result = vault.rename(&note, &dir.path().join("new.md"));

        assert!(matches!(result, Err(VaultError::NoteAlreadyExists(_))));
    }

    // --- rename_preview tests ---

    #[test]
    fn rename_preview_basic() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("old.md"), "Content.").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("old.md")).unwrap();
        let preview = vault.rename_preview(&note, &dir.path().join("new.md")).unwrap();

        assert_eq!(
            preview.new_path,
            common::normalize_path(dir.path().join("new.md"), None)
        );
        assert!(preview.updated_notes.is_empty());
        assert!(preview.id_will_update);
    }

    #[test]
    fn rename_preview_with_wiki_backlink() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[target]].").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("target.md")).unwrap();
        let preview = vault.rename_preview(&note, &dir.path().join("renamed.md")).unwrap();

        assert_eq!(preview.updated_notes.len(), 1);
        assert!(preview.updated_notes[0].0.ends_with("source.md"));
        assert_eq!(preview.updated_notes[0].1, 1);
    }

    #[test]
    fn rename_preview_with_markdown_backlink() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(dir.path().join("source.md"), "[link](target.md)").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("target.md")).unwrap();
        let preview = vault.rename_preview(&note, &dir.path().join("renamed.md")).unwrap();

        assert_eq!(preview.updated_notes.len(), 1);
        assert!(preview.updated_notes[0].0.ends_with("source.md"));
        assert_eq!(preview.updated_notes[0].1, 1);
    }

    #[test]
    fn rename_edits_include_exact_backlink_replacements() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "---\nid: target\n---\nTarget.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[target]] and [link](target.md).").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("target.md")).unwrap();
        let edits = vault.rename_edits(&note, &dir.path().join("renamed.md")).unwrap();

        assert_eq!(edits.new_stem, "renamed");
        assert!(edits.id_will_update);
        assert_eq!(edits.backlink_edits.len(), 1);
        assert!(edits.backlink_edits[0].0.ends_with("source.md"));
        let replacements = edits.backlink_edits[0]
            .1
            .iter()
            .map(|(_, new_text)| new_text.as_str())
            .collect::<Vec<_>>();
        assert_eq!(replacements, vec!["[[renamed]]", "[link](renamed.md)"]);
    }

    #[test]
    fn rename_preview_id_will_update() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("old-note.md"), "---\nid: old-note\n---\nContent.").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("old-note.md")).unwrap();
        let preview = vault.rename_preview(&note, &dir.path().join("new-note.md")).unwrap();

        assert!(preview.id_will_update);
    }

    #[test]
    fn rename_preview_id_will_not_update() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("my-note.md"), "---\nid: custom-id\n---\nContent.").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("my-note.md")).unwrap();
        let preview = vault
            .rename_preview(&note, &dir.path().join("renamed-note.md"))
            .unwrap();

        assert!(!preview.id_will_update);
    }

    #[test]
    fn rename_preview_excludes_alias_only_links() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "---\naliases: [my-alias]\n---\nContent.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[my-alias]].").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("target.md")).unwrap();
        let preview = vault.rename_preview(&note, &dir.path().join("renamed.md")).unwrap();

        // The alias link is a backlink but won't be rewritten, so updated_notes is empty
        assert!(preview.updated_notes.is_empty());
    }

    #[test]
    fn rename_preview_does_not_modify_filesystem() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("old.md"), "Content.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[old]].").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("old.md")).unwrap();
        vault.rename_preview(&note, &dir.path().join("new.md")).unwrap();

        assert!(dir.path().join("old.md").exists());
        assert!(!dir.path().join("new.md").exists());

        let source_content = fs::read_to_string(dir.path().join("source.md")).unwrap();
        assert_eq!(source_content, "See [[old]].");
    }

    #[test]
    fn rename_preview_directory_not_found() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("old.md"), "Content.").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("old.md")).unwrap();
        let result = vault.rename_preview(&note, &dir.path().join("nonexistent/new.md"));

        assert!(matches!(result, Err(VaultError::DirectoryNotFound(_))));
    }

    #[test]
    fn rename_preview_target_already_exists() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("old.md"), "Old.").unwrap();
        fs::write(dir.path().join("new.md"), "Already exists.").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("old.md")).unwrap();
        let result = vault.rename_preview(&note, &dir.path().join("new.md"));

        assert!(matches!(result, Err(VaultError::NoteAlreadyExists(_))));
    }

    #[test]
    fn rename_preview_updated_notes_sorted_by_path() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(dir.path().join("z-source.md"), "See [[target]].").unwrap();
        fs::write(dir.path().join("a-source.md"), "See [[target]].").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("target.md")).unwrap();
        let preview = vault.rename_preview(&note, &dir.path().join("renamed.md")).unwrap();

        assert_eq!(preview.updated_notes.len(), 2);
        assert!(preview.updated_notes[0].0 < preview.updated_notes[1].0);
    }

    #[test]
    fn rename_markdown_link_with_subdir() {
        let dir = tempfile::tempdir().unwrap();
        let subdir = dir.path().join("sub");
        fs::create_dir(&subdir).unwrap();
        fs::write(dir.path().join("root.md"), "Root.").unwrap();
        fs::write(subdir.join("source.md"), "[link](root.md)\n[link](sub/target.md)").unwrap();
        fs::write(subdir.join("target.md"), "Target.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();

        {
            let note = Note::from_path(dir.path().join("root.md")).unwrap();
            vault.rename(&note, &dir.path().join("new-root.md")).unwrap();

            let source_content = fs::read_to_string(subdir.join("source.md")).unwrap();
            assert_eq!(source_content, "[link](new-root.md)\n[link](sub/target.md)");
        }

        {
            let note = Note::from_path(subdir.join("target.md")).unwrap();
            vault.rename(&note, &subdir.join("new-target.md")).unwrap();

            let source_content = fs::read_to_string(subdir.join("source.md")).unwrap();
            assert_eq!(source_content, "[link](new-root.md)\n[link](sub/new-target.md)");
        }
    }

    #[test]
    fn rename_multiple_links_same_source() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("target.md"), "Target.").unwrap();
        fs::write(dir.path().join("source.md"), "[first](target.md)\n[second](target.md)").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("target.md")).unwrap();
        vault.rename(&note, &dir.path().join("renamed.md")).unwrap();

        let source_content = fs::read_to_string(dir.path().join("source.md")).unwrap();
        assert_eq!(source_content, "[first](renamed.md)\n[second](renamed.md)");
    }

    #[test]
    fn rename_preserves_fragment() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("old.md"), "Old.").unwrap();
        fs::write(dir.path().join("source.md"), "[link](old.md#section)").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("old.md")).unwrap();
        vault.rename(&note, &dir.path().join("new.md")).unwrap();

        let source_content = fs::read_to_string(dir.path().join("source.md")).unwrap();
        assert_eq!(source_content, "[link](new.md#section)");
    }

    #[test]
    fn rename_wiki_preserves_heading_and_alias() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("old-stem.md"), "Content.").unwrap();
        fs::write(dir.path().join("source.md"), "See [[old-stem#h1|display]].").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("old-stem.md")).unwrap();
        vault.rename(&note, &dir.path().join("new-stem.md")).unwrap();

        let source_content = fs::read_to_string(dir.path().join("source.md")).unwrap();
        assert_eq!(source_content, "See [[new-stem#h1|display]].");
    }

    // --- merge tests ---

    #[test]
    fn merge_basic_creates_dest_and_deletes_sources() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("a.md"), "Body A.").unwrap();
        fs::write(dir.path().join("b.md"), "Body B.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let a = Note::from_path(dir.path().join("a.md")).unwrap();
        let b = Note::from_path(dir.path().join("b.md")).unwrap();
        let dest_path = dir.path().join("combined.md");
        vault.merge(&[a, b], &dest_path).unwrap();

        assert!(!dir.path().join("a.md").exists());
        assert!(!dir.path().join("b.md").exists());
        assert!(dest_path.exists());
        let content = fs::read_to_string(&dest_path).unwrap();
        assert!(content.contains("Body A."));
        assert!(content.contains("Body B."));
    }

    #[test]
    fn merge_into_existing_appends_content() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("src.md"), "Source body.").unwrap();
        fs::write(dir.path().join("dest.md"), "Existing body.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let src = Note::from_path(dir.path().join("src.md")).unwrap();
        vault.merge(&[src], &dir.path().join("dest.md")).unwrap();

        assert!(!dir.path().join("src.md").exists());
        let content = fs::read_to_string(dir.path().join("dest.md")).unwrap();
        assert!(content.contains("Existing body."));
        assert!(content.contains("Source body."));
    }

    #[test]
    fn merge_unions_tags() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("a.md"), "---\ntags: [rust]\n---\nBody A.").unwrap();
        fs::write(dir.path().join("b.md"), "---\ntags: [obsidian]\n---\nBody B.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let a = Note::from_path(dir.path().join("a.md")).unwrap();
        let b = Note::from_path(dir.path().join("b.md")).unwrap();
        let dest_path = dir.path().join("combined.md");
        vault.merge(&[a, b], &dest_path).unwrap();

        let combined = Note::from_path(&dest_path).unwrap();
        assert!(
            combined
                .tags
                .iter()
                .any(|t| t.tag == "rust" && matches!(t.location, Location::Frontmatter))
        );
        assert!(
            combined
                .tags
                .iter()
                .any(|t| t.tag == "obsidian" && matches!(t.location, Location::Frontmatter))
        );
    }

    #[test]
    fn merges_not_inherit_source_id() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(
            dir.path().join("src.md"),
            "---\nid: source-id\nauthor: alice\n---\nBody.",
        )
        .unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let src = Note::from_path(dir.path().join("src.md")).unwrap();
        let dest_path = dir.path().join("dest.md");
        vault.merge(&[src], &dest_path).unwrap();

        let dest = Note::from_path(&dest_path).unwrap();
        let fm = dest.frontmatter.unwrap();
        // id must NOT come from source
        assert_ne!(dest.id, "source-id");
        assert!(fm.contains_key("id"));
        // other fields ARE inherited when dest is new
        assert!(fm.contains_key("author"));
    }

    #[test]
    fn merge_other_frontmatter_fields_inherited_from_source_when_dest_is_new() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(
            dir.path().join("src.md"),
            "---\nauthor: alice\ncreated: 2024-01-01\n---\nBody.",
        )
        .unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let src = Note::from_path(dir.path().join("src.md")).unwrap();
        let dest_path = dir.path().join("dest.md");
        vault.merge(&[src], &dest_path).unwrap();

        let dest = Note::from_path(&dest_path).unwrap();
        let fm = dest.frontmatter.unwrap();
        assert!(fm.contains_key("author"));
        assert!(fm.contains_key("created"));
    }

    #[test]
    fn merge_dest_wins_on_conflicting_fields() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("src.md"), "---\nauthor: alice\n---\nSource.").unwrap();
        fs::write(dir.path().join("dest.md"), "---\nauthor: bob\n---\nDest.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let src = Note::from_path(dir.path().join("src.md")).unwrap();
        vault.merge(&[src], &dir.path().join("dest.md")).unwrap();

        let dest = Note::from_path(dir.path().join("dest.md")).unwrap();
        let fm = dest.frontmatter.unwrap();
        assert_eq!(fm["author"].as_string().unwrap(), "bob");
    }

    #[test]
    fn merge_updates_wiki_backlinks() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("src.md"), "Source.").unwrap();
        fs::write(dir.path().join("linker.md"), "See [[src]].").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let src = Note::from_path(dir.path().join("src.md")).unwrap();
        vault.merge(&[src], &dir.path().join("dest.md")).unwrap();

        let linker = fs::read_to_string(dir.path().join("linker.md")).unwrap();
        assert_eq!(linker, "See [[dest]].");
    }

    #[test]
    fn merge_source_is_dest_errors() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("note.md"), "Content.").unwrap();

        let mut vault = Vault::open(dir.path()).unwrap();
        let note = Note::from_path(dir.path().join("note.md")).unwrap();
        let result = vault.merge(&[note], &dir.path().join("note.md"));

        assert!(matches!(result, Err(VaultError::MergeSourceIsDestination(_))));
    }

    #[test]
    fn merge_preview_does_not_modify_filesystem() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("src.md"), "Source.").unwrap();
        fs::write(dir.path().join("linker.md"), "See [[src]].").unwrap();

        let vault = Vault::open(dir.path()).unwrap();
        let src = Note::from_path(dir.path().join("src.md")).unwrap();
        vault.merge_preview(&[src], dir.path().join("dest.md")).unwrap();

        assert!(dir.path().join("src.md").exists());
        assert!(!dir.path().join("dest.md").exists());
        let linker = fs::read_to_string(dir.path().join("linker.md")).unwrap();
        assert_eq!(linker, "See [[src]].");
    }
}