hyalo-core 0.13.0

Core library for hyalo — frontmatter parsing, querying, and mutation for Markdown files
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
//! Core rewriting engine for `hyalo mv`.
//!
//! Given a source-file move (`old_rel` → `new_rel`), this module:
//! 1. Finds all files that link *to* the moved file (inbound links) and
//!    rewrites those links to point at the new location.
//! 2. Rewrites relative markdown links *inside* the moved file whose targets
//!    change because the file's directory context has changed (outbound links).
//!
//! The public entry point is [`plan_mv`], which returns a list of
//! [`RewritePlan`] values without touching the filesystem.  Call
//! [`execute_plans`] to apply them.

#![allow(clippy::missing_errors_doc)]

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use serde::Serialize;

use crate::case_index::CaseInsensitiveIndex;
use crate::discovery::{canonicalize_vault_dir, ensure_within_vault};
use crate::link_graph::{LinkGraph, normalize_target, relative_path_between, strip_site_prefix};
use crate::links::{LinkKind, extract_link_spans_with_original};
use crate::scanner::{
    FenceTracker, MAX_FILE_SIZE, is_comment_fence, strip_inline_code, strip_inline_comments,
};

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

/// A planned replacement within a single line of a file.
#[derive(Debug, Clone, Serialize)]
pub struct Replacement {
    /// 1-based line number.
    pub line: usize,
    /// Byte offset of `old_text` within the line.
    #[serde(skip)]
    pub byte_offset: usize,
    /// The original full link syntax, e.g. `[[old-path]]`.
    pub old_text: String,
    /// The replacement full link syntax, e.g. `[[new-path]]`.
    pub new_text: String,
}

/// A rewrite plan for a single file.
#[derive(Debug)]
pub struct RewritePlan {
    /// Absolute path to the file on disk.
    pub path: PathBuf,
    /// Vault-relative path (forward slashes).
    pub rel_path: String,
    /// All replacements to apply to this file.
    pub replacements: Vec<Replacement>,
    /// Full file content with all replacements already applied.
    pub rewritten_content: String,
    /// Fingerprint (mtime, file size) of the file when the plan was built, used
    /// to detect concurrent modifications before writing. `None` for the moved
    /// file's outbound plan (which is written to a new path after `fs::rename`
    /// and needs no check).
    pub mtime: Option<(std::time::SystemTime, u64)>,
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Plan all file rewrites needed when moving `old_rel` → `new_rel`.
///
/// Does NOT include the actual file move — only link updates in other files
/// (inbound links) and outbound link updates in the moved file itself.
///
/// Both `old_rel` and `new_rel` must use forward slashes and be relative to
/// `dir`.
pub fn plan_mv(
    dir: &Path,
    old_rel: &str,
    new_rel: &str,
    site_prefix: Option<&str>,
) -> Result<Vec<RewritePlan>> {
    // Step 1: build link graph to discover inbound links.
    // The build also yields a case-insensitive index of all vault paths, which
    // is used later to canonicalize link targets that differ only in casing.
    let build = LinkGraph::build(dir, site_prefix, None).context("building link graph")?;
    for (path, msg) in &build.warnings {
        eprintln!("warning: skipping {}: {msg}", path.display());
    }
    let graph = build.graph;
    let case_index = build.case_index;

    let old_stem = old_rel.strip_suffix(".md").unwrap_or(old_rel);
    let new_stem = new_rel.strip_suffix(".md").unwrap_or(new_rel);

    // Whether the file moves to a different directory.
    let old_dir = parent_dir(old_rel);
    let new_dir = parent_dir(new_rel);
    let dir_changed = old_dir != new_dir;

    // Step 2: gather inbound backlinks, grouped by source file.
    //
    // Skip self-links here — links that point from the moved file to itself
    // are handled as outbound rewrites. Leaving them in the inbound set would
    // produce a plan whose `path` still refers to `old_rel`, which no longer
    // exists on disk after `fs::rename` (NEW-BUG-2).
    //
    // Use the case-insensitive backlinks query so that links written with
    // different casing (e.g. `[[Web/JavaScript/…]]` targeting
    // `web/javascript/….md`) are included in the source set.  The
    // `plan_inbound_rewrites` function then re-checks the match using the
    // case index, so only genuinely matching links produce replacements.
    let backlinks = graph.backlinks_case_insensitive(old_rel);
    let old_rel_norm = old_rel.replace('\\', "/");
    let mut by_source: HashMap<PathBuf, Vec<_>> = HashMap::new();
    for entry in backlinks {
        let source_norm = entry.source.to_string_lossy().replace('\\', "/");
        if source_norm == old_rel_norm {
            continue;
        }
        by_source
            .entry(entry.source.clone())
            .or_default()
            .push(entry);
    }

    // Step 3: for each source file, build a RewritePlan for inbound links.
    let mut plans: HashMap<PathBuf, RewritePlan> = HashMap::new();

    for source_rel in by_source.keys() {
        let abs_path = dir.join(source_rel);
        let source_rel_str = source_rel.to_string_lossy().replace('\\', "/");
        let meta = std::fs::metadata(&abs_path)
            .with_context(|| format!("failed to stat {}", abs_path.display()))?;
        let file_size = meta.len();
        let file_mtime = Some(
            meta.modified()
                .with_context(|| format!("failed to read mtime for {}", abs_path.display()))
                .map(|t| (t, file_size))?,
        );
        if file_size > MAX_FILE_SIZE {
            eprintln!(
                "warning: skipping {} ({} MiB exceeds {} MiB limit)",
                abs_path.display(),
                file_size / (1024 * 1024),
                MAX_FILE_SIZE / (1024 * 1024)
            );
            continue;
        }
        let content = std::fs::read_to_string(&abs_path)
            .with_context(|| format!("reading {}", abs_path.display()))?;

        let replacements = plan_inbound_rewrites(
            &content,
            &source_rel_str,
            old_rel,
            old_stem,
            new_rel,
            new_stem,
            site_prefix,
            Some(&case_index),
        );

        if !replacements.is_empty() {
            let rewritten_content = apply_replacements(&content, &replacements);
            plans.insert(
                source_rel.clone(),
                RewritePlan {
                    path: abs_path,
                    rel_path: source_rel_str,
                    replacements,
                    rewritten_content,
                    mtime: file_mtime,
                },
            );
        }
    }

    // Step 4: plan outbound link updates for the moved file itself.
    //
    // Always run — the moved file may contain self-links that need rewriting
    // even when its directory didn't change (NEW-BUG-2). When the directory
    // does change, relative links to other files are also rebased here.
    let old_abs = dir.join(old_rel);
    let old_meta = std::fs::metadata(&old_abs)
        .with_context(|| format!("failed to stat {}", old_abs.display()))?;
    let old_file_size = old_meta.len();
    // Capture mtime before reading content so concurrent edits can be detected
    // after fs::rename (which preserves mtime).
    let old_file_mtime = old_meta
        .modified()
        .with_context(|| format!("failed to read mtime for {}", old_abs.display()))
        .map(|t| (t, old_file_size))?;
    if old_file_size > MAX_FILE_SIZE {
        eprintln!(
            "warning: skipping outbound rewrite for {} ({} MiB exceeds {} MiB limit)",
            old_abs.display(),
            old_file_size / (1024 * 1024),
            MAX_FILE_SIZE / (1024 * 1024)
        );
        return Ok(plans.into_values().collect());
    }
    let content = std::fs::read_to_string(&old_abs)
        .with_context(|| format!("reading {}", old_abs.display()))?;

    let outbound_replacements = plan_outbound_rewrites(&content, old_rel, new_rel, dir_changed);

    if !outbound_replacements.is_empty() {
        let moved_key = PathBuf::from(old_rel.replace('\\', "/"));
        // The path targets the NEW location — after fs::rename, that's where
        // the file lives and where the rewritten content must be written by
        // execute_plans.
        let new_abs = dir.join(new_rel);

        if let Some(existing) = plans.get_mut(&moved_key) {
            existing.path = new_abs;
            existing.rel_path = new_rel.to_string();
            existing.replacements.extend(outbound_replacements);
            existing.rewritten_content = apply_replacements(&content, &existing.replacements);
            // Preserve the mtime captured before reading so execute_plans can
            // still detect concurrent edits (fs::rename preserves mtime).
            existing.mtime = Some(old_file_mtime);
        } else {
            let rewritten_content = apply_replacements(&content, &outbound_replacements);
            plans.insert(
                moved_key,
                RewritePlan {
                    path: new_abs,
                    rel_path: new_rel.to_string(),
                    replacements: outbound_replacements,
                    rewritten_content,
                    // Preserve mtime — fs::rename keeps mtime, so execute_plans
                    // can detect concurrent edits before writing.
                    mtime: Some(old_file_mtime),
                },
            );
        }
    }

    Ok(plans.into_values().collect())
}

/// Split a markdown-link target into its path portion and any trailing
/// `#fragment`. Returns `(path, fragment_including_hash)`.
///
/// For `"peer.md#intro"` this returns `("peer.md", "#intro")`; for a target
/// without a fragment the second element is `""`.
fn split_target_fragment(target: &str) -> (&str, &str) {
    match target.find('#') {
        Some(idx) => (&target[..idx], &target[idx..]),
        None => (target, ""),
    }
}

/// Decide whether a markdown-link target should be considered for rewriting
/// when a file moves.
///
/// Returns `false` (skip) for link targets that clearly do **not** point at a
/// vault markdown file:
/// - Site-absolute paths (start with `/`) — these are left untouched so that
///   downstream site renderers can resolve them from their own root.
/// - URL schemes (`http://`, `mailto:`, …) and fragment-only refs (`#anchor`).
///   Windows drive-letter paths like `C:\notes\x.md` are **not** treated as
///   URL schemes.
/// - Bare tokens with no `.md` suffix *and* no path separator — these look
///   like Obsidian wikilink labels or plain anchor text rather than file
///   paths.
///
/// Any trailing `#fragment` on the target is ignored when classifying — the
/// file portion is what matters. So `peer.md#intro` is still treated as an
/// `.md` link, and `#anchor` alone is still a pure fragment.
///
/// Inbound rewriting already narrows by string-equality against the moved
/// file's rel/stem, so this extra guard mostly protects outbound rewriting
/// from blindly rebasing non-filesystem references.
fn should_rewrite_outbound_target(target: &str) -> bool {
    if target.is_empty() || target.starts_with('#') {
        return false;
    }
    let (path_part, _) = split_target_fragment(target);
    if path_part.is_empty() {
        return false;
    }
    if path_part.starts_with('/') {
        return false;
    }
    // URL schemes: `http://`, `https://`, `mailto:`, `tel:`, …
    //
    // Exclude Windows drive-letter paths (single ASCII letter followed by `:`
    // and a path separator) — they are filesystem paths, not URLs.
    if let Some(colon) = path_part.find(':') {
        let scheme = &path_part[..colon];
        let is_drive_letter = colon == 1
            && scheme
                .chars()
                .next()
                .is_some_and(|c| c.is_ascii_alphabetic())
            && path_part
                .as_bytes()
                .get(colon + 1)
                .is_some_and(|b| *b == b'/' || *b == b'\\');
        if !is_drive_letter
            && !scheme.is_empty()
            && scheme
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '-' || c == '.')
        {
            return false;
        }
    }
    // Bare token with no `.md` suffix and no path separator: treat as label.
    let has_path_sep = path_part.contains('/') || path_part.contains('\\');
    let is_md = std::path::Path::new(path_part)
        .extension()
        .is_some_and(|ext| ext.eq_ignore_ascii_case("md"));
    if !has_path_sep && !is_md {
        return false;
    }
    true
}

/// Execute rewrite plans: write each plan's `rewritten_content` back to disk.
///
/// `vault_dir` is the root vault directory.  Every plan path is asserted to be
/// within the vault before writing, which guards against future refactors
/// accidentally constructing paths that escape the vault.
pub fn execute_plans(vault_dir: &Path, plans: &[RewritePlan]) -> Result<()> {
    // Canonicalize once up front; all plan paths are resolved against this.
    let canonical_vault = canonicalize_vault_dir(vault_dir)
        .context("failed to canonicalize vault directory for write safety check")?;

    for plan in plans {
        // Safety assertion: verify the target is inside the vault before
        // writing.  Plans are generated by `plan_mv` which constrains paths to
        // the vault, but this check makes the invariant explicit and
        // survives future refactors.
        let within = ensure_within_vault(&canonical_vault, &plan.path)
            .with_context(|| format!("could not verify {} is within vault", plan.path.display()))?;
        anyhow::ensure!(
            within,
            "refusing to write outside vault: {}",
            plan.path.display()
        );

        // Detect concurrent modification: if the file was changed between
        // plan and execute, abort to avoid silently clobbering the new content.
        if let Some(expected_mtime) = plan.mtime {
            crate::frontmatter::check_mtime(&plan.path, expected_mtime)?;
        }

        crate::fs_util::atomic_write(&plan.path, plan.rewritten_content.as_bytes())
            .with_context(|| format!("writing {}", plan.path.display()))?;
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Inbound rewrite planning
// ---------------------------------------------------------------------------

/// Walk every body line in `content` and return [`Replacement`]s for links
/// that target the moved file.
///
/// When `case_index` is provided, link targets are canonicalized through it
/// before comparison against `old_rel`/`old_stem`.  This fixes BUG-6: a link
/// written as `Web/JavaScript/…` resolves to the on-disk lowercase path and is
/// therefore detected as pointing at the moved file.
#[allow(clippy::too_many_arguments)]
fn plan_inbound_rewrites(
    content: &str,
    source_rel: &str,
    old_rel: &str,
    old_stem: &str,
    new_rel: &str,
    new_stem: &str,
    site_prefix: Option<&str>,
    case_index: Option<&CaseInsensitiveIndex>,
) -> Vec<Replacement> {
    let mut replacements = Vec::new();
    let mut fence = FenceTracker::new();
    let mut in_comment_fence = false;
    let mut in_frontmatter = false;
    let mut frontmatter_done = false;
    let mut line_num = 0usize;

    for line in content.split('\n') {
        line_num += 1;

        // ---- Frontmatter handling (trim() matches scanner behaviour) ----
        if !frontmatter_done {
            if line_num == 1 && line.trim() == "---" {
                in_frontmatter = true;
                continue;
            }
            if in_frontmatter {
                if line.trim() == "---" {
                    in_frontmatter = false;
                    frontmatter_done = true;
                }
                continue;
            }
            // No frontmatter block found; mark done.
            frontmatter_done = true;
        }

        // ---- Comment fence (Obsidian %% blocks) ----
        if is_comment_fence(line) {
            in_comment_fence = !in_comment_fence;
            continue;
        }
        if in_comment_fence {
            continue;
        }

        // ---- Fenced code block ----
        if fence.process_line(line) {
            continue;
        }

        // ---- Extract and compare link spans ----
        let stripped_code = strip_inline_code(line);
        let cleaned = strip_inline_comments(stripped_code.as_ref());
        let spans = extract_link_spans_with_original(&cleaned, line);

        for span in spans {
            let matches = match span.kind {
                LinkKind::Wikilink => {
                    let t = &span.link.target;
                    // Only match wikilinks that already contain a path separator.
                    // Bare name wikilinks (e.g. [[note]]) are left alone — they
                    // don't encode a location and will work once shortest-path
                    // resolution is implemented.
                    if !(t.contains('/') || t.contains('\\')) {
                        false
                    } else if t == old_stem || t == old_rel {
                        true
                    } else if let Some(idx) = case_index {
                        // Canonicalize the written target through the case index.
                        // Normalize path separators to forward slashes first so
                        // wikilinks written with backslashes (e.g. `[[Web\\Foo]]`)
                        // can still match. Wikilinks may be written without the
                        // `.md` extension, so try both the literal target and
                        // its `.md`-appended form.
                        let t_norm = t.replace('\\', "/").to_ascii_lowercase();
                        let canonical = idx.lookup_unique(&t_norm).or_else(|| {
                            let with_md = format!("{t_norm}.md");
                            idx.lookup_unique(&with_md)
                        });
                        canonical == Some(old_rel) || canonical == Some(old_stem)
                    } else {
                        false
                    }
                }
                LinkKind::Markdown => {
                    // Absolute links (starting with `/`) are stripped of the
                    // site_prefix then compared; relative links are normalized
                    // against the source file's directory.
                    let norm = if span.link.target.starts_with('/') {
                        strip_site_prefix(&span.link.target, site_prefix)
                    } else {
                        normalize_target(Path::new(source_rel), &span.link.target)
                    };
                    if norm == old_rel || norm == old_stem {
                        true
                    } else if let Some(idx) = case_index {
                        // Try canonicalizing the normalized target through the
                        // index. Markdown links may also be written without the
                        // `.md` extension (e.g. `[x](Web/Foo)`), so try both the
                        // literal and `.md`-appended forms — mirrors the wikilink
                        // branch above.
                        let norm_lower = norm.to_ascii_lowercase();
                        let canonical = idx.lookup_unique(&norm_lower).or_else(|| {
                            let with_md = format!("{norm_lower}.md");
                            idx.lookup_unique(&with_md)
                        });
                        canonical == Some(old_rel) || canonical == Some(old_stem)
                    } else {
                        false
                    }
                }
            };

            if !matches {
                continue;
            }

            // Compute the new target text.
            let new_target = match span.kind {
                LinkKind::Wikilink => new_stem.to_string(),
                LinkKind::Markdown => {
                    // If the original link was absolute-path style, preserve that
                    // style by re-prepending the site_prefix (or just `/`).
                    if span.link.target.starts_with('/') {
                        // Preserve whether the original used .md or stem style.
                        let target = if std::path::Path::new(&span.link.target)
                            .extension()
                            .is_some_and(|ext| ext.eq_ignore_ascii_case("md"))
                        {
                            new_rel
                        } else {
                            new_stem
                        };
                        match site_prefix {
                            Some(prefix) => format!("/{prefix}/{target}"),
                            None => format!("/{target}"),
                        }
                    } else {
                        relative_path_between(source_rel, new_rel)
                    }
                }
            };

            // Build old_text and new_text using the ORIGINAL line (not cleaned).
            let old_text = line[span.full_start..span.full_end].to_string();
            let new_text = format!(
                "{}{}{}",
                &line[span.full_start..span.target_start],
                new_target,
                &line[span.target_end..span.full_end]
            );

            if old_text != new_text {
                replacements.push(Replacement {
                    line: line_num,
                    byte_offset: span.full_start,
                    old_text,
                    new_text,
                });
            }
        }
    }

    replacements
}

// ---------------------------------------------------------------------------
// Outbound rewrite planning
// ---------------------------------------------------------------------------

/// Walk every body line in `content` (which lives at `old_rel`) and return
/// [`Replacement`]s for relative markdown links whose targets change when the
/// file moves to `new_rel`.
///
/// Wikilinks are vault-relative and never change.
///
/// `dir_changed` indicates whether the move crosses directory boundaries.
/// When `false`, only self-links (pointing at `old_rel` itself) need updating;
/// all other relative links remain valid.
fn plan_outbound_rewrites(
    content: &str,
    old_rel: &str,
    new_rel: &str,
    dir_changed: bool,
) -> Vec<Replacement> {
    let mut replacements = Vec::new();
    let mut fence = FenceTracker::new();
    let mut in_comment_fence = false;
    let mut in_frontmatter = false;
    let mut frontmatter_done = false;
    let mut line_num = 0usize;

    for line in content.split('\n') {
        line_num += 1;

        // ---- Frontmatter handling (trim() matches scanner behaviour) ----
        if !frontmatter_done {
            if line_num == 1 && line.trim() == "---" {
                in_frontmatter = true;
                continue;
            }
            if in_frontmatter {
                if line.trim() == "---" {
                    in_frontmatter = false;
                    frontmatter_done = true;
                }
                continue;
            }
            frontmatter_done = true;
        }

        // ---- Comment fence ----
        if is_comment_fence(line) {
            in_comment_fence = !in_comment_fence;
            continue;
        }
        if in_comment_fence {
            continue;
        }

        // ---- Fenced code block ----
        if fence.process_line(line) {
            continue;
        }

        // ---- Extract markdown link spans only ----
        let stripped_code = strip_inline_code(line);
        let cleaned = strip_inline_comments(stripped_code.as_ref());
        let spans = extract_link_spans_with_original(&cleaned, line);

        for span in spans {
            // Only rewrite markdown links — wikilinks are vault-relative.
            if span.kind != LinkKind::Markdown {
                continue;
            }

            // Skip targets that aren't vault markdown paths (site-absolute,
            // URL schemes, bare labels). See `should_rewrite_outbound_target`.
            if !should_rewrite_outbound_target(&span.link.target) {
                continue;
            }

            // Strip any trailing `#fragment` for resolution / comparison, then
            // re-attach it to the rewritten path so anchored file links keep
            // their anchor.
            let (target_path, target_fragment) = split_target_fragment(&span.link.target);

            // Resolve target relative to the OLD location's directory.
            let resolved = normalize_target(Path::new(old_rel), target_path);

            // Self-links: the file moves to `new_rel`, so the link should
            // continue to refer to the file at its new location.
            let target_after_move = if resolved == old_rel {
                new_rel.to_string()
            } else {
                // When the directory hasn't changed, relative links to other
                // files in the same directory are still valid — skip them.
                if !dir_changed {
                    continue;
                }
                resolved
            };

            // Compute new relative path from the NEW location, then re-attach
            // any fragment that was stripped above.
            let new_target = format!(
                "{}{}",
                relative_path_between(new_rel, &target_after_move),
                target_fragment
            );

            // Original target as written in the file.
            let original_target = &line[span.target_start..span.target_end];

            if new_target == original_target {
                continue;
            }

            let old_text = line[span.full_start..span.full_end].to_string();
            let new_text = format!(
                "{}{}{}",
                &line[span.full_start..span.target_start],
                new_target,
                &line[span.target_end..span.full_end]
            );

            replacements.push(Replacement {
                line: line_num,
                byte_offset: span.full_start,
                old_text,
                new_text,
            });
        }
    }

    replacements
}

// ---------------------------------------------------------------------------
// Applying replacements to file content
// ---------------------------------------------------------------------------

/// Apply all replacements to `content`, returning the rewritten string.
///
/// Replacements are matched against lines by their `old_text`.  Multiple
/// replacements on the same line are applied right-to-left (by first
/// occurrence of `old_text`) to avoid offset shifts.
pub(crate) fn apply_replacements(content: &str, replacements: &[Replacement]) -> String {
    // Group replacements by 1-based line number.
    let mut by_line: HashMap<usize, Vec<&Replacement>> = HashMap::new();
    for r in replacements {
        by_line.entry(r.line).or_default().push(r);
    }

    // Reconstruct content line by line, preserving exact line endings.
    // We split on '\n' and re-join, tracking whether the original ended with '\n'.
    let ends_with_newline = content.ends_with('\n');

    let lines: Vec<&str> = content.split('\n').collect();
    let mut out = String::with_capacity(content.len());

    for (idx, &raw_line) in lines.iter().enumerate() {
        let line_num = idx + 1;
        let is_last = idx + 1 == lines.len();

        let mut line = raw_line.to_string();

        if let Some(repls) = by_line.get(&line_num) {
            // Sort right-to-left by byte offset so that applying one
            // substitution doesn't shift offsets for subsequent ones.
            let mut sorted: Vec<&&Replacement> = repls.iter().collect();
            sorted.sort_by(|a, b| b.byte_offset.cmp(&a.byte_offset));

            for r in sorted {
                let pos = r.byte_offset;
                let end = pos + r.old_text.len();
                if end <= line.len() && line[pos..end] == *r.old_text {
                    line = format!("{}{}{}", &line[..pos], r.new_text, &line[end..]);
                }
            }
        }

        out.push_str(&line);

        // Re-add '\n' between lines but not after the final segment.
        // split('\n') on "a\nb\n" gives ["a", "b", ""] — the trailing
        // empty segment is handled by the safety net below.
        if !is_last {
            out.push('\n');
        }
    }

    // If original content ended with '\n', ensure it still does.
    if ends_with_newline && !out.ends_with('\n') {
        out.push('\n');
    }

    out
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Return the directory part of a vault-relative path (e.g. `"sub/dir"` for
/// `"sub/dir/file.md"`).  Returns an empty string for root-level files.
fn parent_dir(rel: &str) -> &str {
    match rel.rfind('/') {
        Some(pos) => &rel[..pos],
        None => "",
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn create_vault(files: &[(&str, &str)]) -> tempfile::TempDir {
        let dir = tempfile::tempdir().unwrap();
        for (name, content) in files {
            let path = dir.path().join(name);
            if let Some(parent) = path.parent() {
                fs::create_dir_all(parent).unwrap();
            }
            fs::write(&path, content).unwrap();
        }
        dir
    }

    #[test]
    fn plan_mv_bare_wikilink_not_rewritten() {
        // Bare wikilinks (no path separator) are left alone — they are name-based
        // references that don't encode a location.
        let vault = create_vault(&[
            ("a.md", "---\ntitle: A\n---\nSee [[b]] for details\n"),
            ("b.md", "---\ntitle: B\n---\nContent\n"),
        ]);
        let plans = plan_mv(vault.path(), "b.md", "archive/b.md", None).unwrap();
        assert!(
            plans.is_empty(),
            "bare wikilink [[b]] should not be rewritten"
        );
    }

    #[test]
    fn plan_mv_bare_wikilink_with_alias_not_rewritten() {
        let vault = create_vault(&[("a.md", "See [[b|my note]] here\n"), ("b.md", "Content\n")]);
        let plans = plan_mv(vault.path(), "b.md", "sub/b.md", None).unwrap();
        assert!(
            plans.is_empty(),
            "bare wikilink [[b|my note]] should not be rewritten"
        );
    }

    #[test]
    fn plan_mv_bare_wikilink_with_fragment_not_rewritten() {
        let vault = create_vault(&[("a.md", "See [[b#section]] here\n"), ("b.md", "Content\n")]);
        let plans = plan_mv(vault.path(), "b.md", "sub/b.md", None).unwrap();
        assert!(
            plans.is_empty(),
            "bare wikilink [[b#section]] should not be rewritten"
        );
    }

    #[test]
    fn plan_mv_inbound_wikilink_with_path() {
        // Wikilinks that already contain a path ARE rewritten.
        let vault = create_vault(&[
            (
                "a.md",
                "---\ntitle: A\n---\nSee [[backlog/item]] for details\n",
            ),
            ("backlog/item.md", "---\ntitle: Item\n---\nContent\n"),
        ]);
        let plans = plan_mv(
            vault.path(),
            "backlog/item.md",
            "backlog/done/item.md",
            None,
        )
        .unwrap();
        assert_eq!(plans.len(), 1);
        assert_eq!(plans[0].rel_path, "a.md");
        assert_eq!(plans[0].replacements.len(), 1);
        assert_eq!(plans[0].replacements[0].old_text, "[[backlog/item]]");
        assert_eq!(plans[0].replacements[0].new_text, "[[backlog/done/item]]");
    }

    #[test]
    fn plan_mv_inbound_wikilink_with_path_and_alias() {
        let vault = create_vault(&[
            ("a.md", "See [[sub/b|my note]] here\n"),
            ("sub/b.md", "Content\n"),
        ]);
        let plans = plan_mv(vault.path(), "sub/b.md", "archive/b.md", None).unwrap();
        assert_eq!(plans.len(), 1);
        assert_eq!(plans[0].replacements[0].old_text, "[[sub/b|my note]]");
        assert_eq!(plans[0].replacements[0].new_text, "[[archive/b|my note]]");
    }

    #[test]
    fn plan_mv_inbound_wikilink_with_path_and_fragment() {
        let vault = create_vault(&[
            ("a.md", "See [[sub/b#section]] here\n"),
            ("sub/b.md", "Content\n"),
        ]);
        let plans = plan_mv(vault.path(), "sub/b.md", "archive/b.md", None).unwrap();
        assert_eq!(plans.len(), 1);
        assert_eq!(plans[0].replacements[0].old_text, "[[sub/b#section]]");
        assert_eq!(plans[0].replacements[0].new_text, "[[archive/b#section]]");
    }

    #[test]
    fn plan_mv_inbound_markdown_link() {
        let vault = create_vault(&[("a.md", "See [note](b.md) here\n"), ("b.md", "Content\n")]);
        let plans = plan_mv(vault.path(), "b.md", "sub/b.md", None).unwrap();
        assert_eq!(plans.len(), 1);
        assert_eq!(plans[0].replacements[0].old_text, "[note](b.md)");
        assert_eq!(plans[0].replacements[0].new_text, "[note](sub/b.md)");
    }

    #[test]
    fn plan_mv_outbound_relative_link() {
        // b.md in root links to a.md. Moving b.md to sub/b.md means the
        // relative path changes.
        let vault = create_vault(&[("a.md", "Content A\n"), ("b.md", "See [note](a.md) here\n")]);
        let plans = plan_mv(vault.path(), "b.md", "sub/b.md", None).unwrap();
        let moved_plan = plans.iter().find(|p| p.rel_path == "sub/b.md").unwrap();
        assert_eq!(moved_plan.replacements[0].old_text, "[note](a.md)");
        assert_eq!(moved_plan.replacements[0].new_text, "[note](../a.md)");
    }

    #[test]
    fn plan_mv_outbound_wikilink_unchanged() {
        // Wikilinks are vault-relative, so moving the file doesn't change them.
        let vault = create_vault(&[("a.md", "Content A\n"), ("b.md", "See [[a]] here\n")]);
        let plans = plan_mv(vault.path(), "b.md", "sub/b.md", None).unwrap();
        // b.md should NOT appear in plans (no outbound changes needed for wikilinks).
        let moved_plan = plans.iter().find(|p| p.rel_path == "b.md");
        assert!(moved_plan.is_none());
    }

    #[test]
    fn plan_mv_links_in_code_block_untouched() {
        let vault = create_vault(&[
            (
                "a.md",
                "---\ntitle: A\n---\n```\n[[sub/b]]\n```\nReal [[sub/b]]\n",
            ),
            ("sub/b.md", "Content\n"),
        ]);
        let plans = plan_mv(vault.path(), "sub/b.md", "archive/b.md", None).unwrap();
        assert_eq!(plans.len(), 1);
        // Only the real link outside code block should be rewritten.
        assert_eq!(plans[0].replacements.len(), 1);
        assert_eq!(plans[0].replacements[0].line, 7);
    }

    #[test]
    fn plan_mv_links_in_inline_code_untouched() {
        let vault = create_vault(&[
            ("a.md", "Use `[[sub/b]]` and real [[sub/b]]\n"),
            ("sub/b.md", "Content\n"),
        ]);
        let plans = plan_mv(vault.path(), "sub/b.md", "archive/b.md", None).unwrap();
        assert_eq!(plans.len(), 1);
        assert_eq!(plans[0].replacements.len(), 1);
        assert_eq!(plans[0].replacements[0].old_text, "[[sub/b]]");
    }

    #[test]
    fn plan_mv_no_links_empty_result() {
        let vault = create_vault(&[("a.md", "No links here\n"), ("b.md", "Content\n")]);
        let plans = plan_mv(vault.path(), "b.md", "sub/b.md", None).unwrap();
        assert!(plans.is_empty());
    }

    #[test]
    fn plan_mv_multiple_links_one_line() {
        let vault = create_vault(&[
            ("a.md", "See [[sub/b]] and [[sub/b|alias]]\n"),
            ("sub/b.md", "Content\n"),
        ]);
        let plans = plan_mv(vault.path(), "sub/b.md", "archive/b.md", None).unwrap();
        assert_eq!(plans.len(), 1);
        assert_eq!(plans[0].replacements.len(), 2);
    }

    #[test]
    fn execute_plans_writes_files() {
        let vault = create_vault(&[("a.md", "See [[sub/b]] here\n"), ("sub/b.md", "Content\n")]);
        let plans = plan_mv(vault.path(), "sub/b.md", "archive/b.md", None).unwrap();
        execute_plans(vault.path(), &plans).unwrap();
        let content = fs::read_to_string(vault.path().join("a.md")).unwrap();
        assert!(content.contains("[[archive/b]]"));
        assert!(!content.contains("[[sub/b]]"));
    }

    // ---- Additional edge-case tests ----

    #[test]
    fn apply_replacements_preserves_trailing_newline() {
        let content = "line one\nline two\n";
        let repls = vec![Replacement {
            line: 1,
            byte_offset: 5,
            old_text: "one".to_string(),
            new_text: "ONE".to_string(),
        }];
        let result = apply_replacements(content, &repls);
        assert_eq!(result, "line ONE\nline two\n");
    }

    #[test]
    fn apply_replacements_no_trailing_newline() {
        let content = "line one\nline two";
        let repls = vec![Replacement {
            line: 2,
            byte_offset: 5,
            old_text: "two".to_string(),
            new_text: "TWO".to_string(),
        }];
        let result = apply_replacements(content, &repls);
        assert_eq!(result, "line one\nline TWO");
    }

    #[test]
    fn plan_mv_frontmatter_links_untouched() {
        // Links inside frontmatter must not be rewritten.
        let vault = create_vault(&[
            ("a.md", "---\nrelated: \"[[sub/b]]\"\n---\nBody [[sub/b]]\n"),
            ("sub/b.md", "Content\n"),
        ]);
        let plans = plan_mv(vault.path(), "sub/b.md", "archive/b.md", None).unwrap();
        assert_eq!(plans.len(), 1);
        assert_eq!(plans[0].replacements.len(), 1);
        assert_eq!(plans[0].replacements[0].line, 4); // Body line
    }

    #[test]
    fn plan_mv_same_directory_no_outbound_changes() {
        // Moving within the same directory: outbound relative links don't change.
        let vault = create_vault(&[("a.md", "Content A\n"), ("b.md", "See [note](a.md) here\n")]);
        // Both old and new are in the root → no outbound rewrite needed.
        let plans = plan_mv(vault.path(), "b.md", "c.md", None).unwrap();
        let moved_plan = plans.iter().find(|p| p.rel_path == "b.md");
        assert!(moved_plan.is_none());
    }

    #[test]
    fn plan_mv_inbound_markdown_link_from_subdir() {
        // A file in a subdirectory links to the moved file using a relative path.
        let vault = create_vault(&[
            ("sub/a.md", "See [note](../b.md) here\n"),
            ("b.md", "Content\n"),
        ]);
        let plans = plan_mv(vault.path(), "b.md", "archive/b.md", None).unwrap();
        assert_eq!(plans.len(), 1);
        assert_eq!(plans[0].rel_path, "sub/a.md");
        assert_eq!(plans[0].replacements[0].old_text, "[note](../b.md)");
        // From sub/a.md to archive/b.md: ../archive/b.md
        assert_eq!(plans[0].replacements[0].new_text, "[note](../archive/b.md)");
    }

    #[test]
    fn plan_mv_bare_markdown_link_from_subdir_not_false_positive() {
        // sub/a.md has [note](b.md) which resolves to sub/b.md, NOT root b.md.
        // Moving root b.md must NOT rewrite this link.
        let vault = create_vault(&[
            ("sub/a.md", "See [note](b.md) here\n"),
            ("sub/b.md", "Content sub\n"),
            ("b.md", "Content root\n"),
        ]);
        let plans = plan_mv(vault.path(), "b.md", "archive/b.md", None).unwrap();
        // sub/a.md links to sub/b.md, not root b.md — should NOT be rewritten.
        let sub_plan = plans.iter().find(|p| p.rel_path == "sub/a.md");
        assert!(sub_plan.is_none(), "false positive: {plans:?}");
    }

    #[test]
    fn plan_mv_bare_wikilink_with_md_extension_not_rewritten() {
        // [[b.md]] is a bare wikilink (no path separator) — leave it alone.
        let vault = create_vault(&[("a.md", "See [[b.md]] here\n"), ("b.md", "Content\n")]);
        let plans = plan_mv(vault.path(), "b.md", "sub/b.md", None).unwrap();
        assert!(
            plans.is_empty(),
            "bare wikilink [[b.md]] should not be rewritten"
        );
    }

    #[test]
    fn plan_mv_wikilink_with_path_and_md_extension() {
        // [[sub/b.md]] has a path separator — should be rewritten.
        let vault = create_vault(&[
            ("a.md", "See [[sub/b.md]] here\n"),
            ("sub/b.md", "Content\n"),
        ]);
        let plans = plan_mv(vault.path(), "sub/b.md", "archive/b.md", None).unwrap();
        assert_eq!(plans.len(), 1);
        assert_eq!(plans[0].replacements[0].old_text, "[[sub/b.md]]");
        assert_eq!(plans[0].replacements[0].new_text, "[[archive/b]]");
    }

    // ---- Absolute-path inbound link tests ----

    #[test]
    fn plan_mv_inbound_absolute_link_with_site_prefix() {
        // a.md uses an absolute link /docs/configure/settings.md to reference
        // configure/settings.md (with site_prefix = "docs").
        // After moving configure/settings.md → config/settings.md, the link
        // must be rewritten to /docs/config/settings.md (preserving absolute style).
        let vault = create_vault(&[
            (
                "a.md",
                "See [settings](/docs/configure/settings.md) for details\n",
            ),
            ("configure/settings.md", "# Settings\n"),
        ]);
        let plans = plan_mv(
            vault.path(),
            "configure/settings.md",
            "config/settings.md",
            Some("docs"),
        )
        .unwrap();
        assert_eq!(plans.len(), 1, "should produce one rewrite plan");
        assert_eq!(plans[0].rel_path, "a.md");
        assert_eq!(plans[0].replacements.len(), 1);
        assert_eq!(
            plans[0].replacements[0].old_text,
            "[settings](/docs/configure/settings.md)"
        );
        assert_eq!(
            plans[0].replacements[0].new_text,
            "[settings](/docs/config/settings.md)"
        );
    }

    #[test]
    fn plan_mv_inbound_absolute_link_without_site_prefix() {
        // With no site_prefix, /page.md is an absolute link to page.md.
        // Moving page.md → archive/page.md must rewrite to /archive/page.md.
        let vault = create_vault(&[
            ("index.md", "See [page](/page.md) here\n"),
            ("page.md", "# Page\n"),
        ]);
        let plans = plan_mv(vault.path(), "page.md", "archive/page.md", None).unwrap();
        assert_eq!(plans.len(), 1);
        assert_eq!(plans[0].rel_path, "index.md");
        assert_eq!(plans[0].replacements[0].old_text, "[page](/page.md)");
        assert_eq!(
            plans[0].replacements[0].new_text,
            "[page](/archive/page.md)"
        );
    }

    #[test]
    fn plan_mv_inbound_absolute_link_no_false_positive_wrong_prefix() {
        // /other/page.md does NOT match configure/settings.md even with
        // site_prefix = "docs" — must not produce a spurious rewrite.
        let vault = create_vault(&[
            ("a.md", "See [other](/other/page.md) here\n"),
            ("configure/settings.md", "# Settings\n"),
            ("other/page.md", "# Other\n"),
        ]);
        let plans = plan_mv(
            vault.path(),
            "configure/settings.md",
            "config/settings.md",
            Some("docs"),
        )
        .unwrap();
        let a_plan = plans.iter().find(|p| p.rel_path == "a.md");
        assert!(
            a_plan.is_none(),
            "absolute link to a different file must not be rewritten: {plans:?}"
        );
    }

    #[test]
    fn plan_mv_inbound_absolute_stem_match() {
        // Absolute link without .md extension: /docs/configure/settings
        // should still match configure/settings.md (stem comparison).
        let vault = create_vault(&[
            (
                "a.md",
                "See [settings](/docs/configure/settings) for details\n",
            ),
            ("configure/settings.md", "# Settings\n"),
        ]);
        let plans = plan_mv(
            vault.path(),
            "configure/settings.md",
            "config/settings.md",
            Some("docs"),
        )
        .unwrap();
        assert_eq!(plans.len(), 1);
        assert_eq!(
            plans[0].replacements[0].old_text,
            "[settings](/docs/configure/settings)"
        );
        assert_eq!(
            plans[0].replacements[0].new_text,
            "[settings](/docs/config/settings)"
        );
    }

    // ---- Self-link tests (NEW-BUG-2) ----

    #[test]
    fn plan_mv_self_link_same_directory() {
        // `self.md` contains a markdown link to itself. Moving it to `other.md`
        // in the same directory must (1) succeed, (2) produce a plan whose path
        // points to the NEW location, and (3) rewrite the self-link.
        let vault = create_vault(&[("self.md", "A link to [me](self.md).\n")]);
        let plans = plan_mv(vault.path(), "self.md", "other.md", None).unwrap();
        assert_eq!(plans.len(), 1);
        let plan = &plans[0];
        assert_eq!(plan.rel_path, "other.md");
        assert_eq!(plan.path, vault.path().join("other.md"));
        assert_eq!(plan.replacements.len(), 1);
        assert_eq!(plan.replacements[0].old_text, "[me](self.md)");
        assert_eq!(plan.replacements[0].new_text, "[me](other.md)");
    }

    #[test]
    fn execute_plans_self_link_same_directory_e2e() {
        // Full mv flow: rename + rewrite without canonicalization error.
        let vault = create_vault(&[("self.md", "A link to [me](self.md).\n")]);
        let plans = plan_mv(vault.path(), "self.md", "other.md", None).unwrap();
        fs::rename(vault.path().join("self.md"), vault.path().join("other.md")).unwrap();
        execute_plans(vault.path(), &plans).unwrap();
        let content = fs::read_to_string(vault.path().join("other.md")).unwrap();
        assert!(content.contains("[me](other.md)"), "got: {content:?}");
        assert!(!content.contains("self.md"));
    }

    // ---- Outbound skip-rule tests (BUG-6) ----

    #[test]
    fn plan_mv_outbound_skips_site_absolute_links() {
        // Site-absolute links (/…) must NOT be rewritten when the moved file's
        // directory changes.
        let vault = create_vault(&[
            (
                "games/anatomy/index.md",
                "See [MDN](/en-US/docs/Web/API/Web_Workers).\n",
            ),
            ("games/anatomy-renamed/.gitkeep", ""),
        ]);
        let plans = plan_mv(
            vault.path(),
            "games/anatomy/index.md",
            "games/anatomy-renamed/index.md",
            None,
        )
        .unwrap();
        let moved_plan = plans
            .iter()
            .find(|p| p.rel_path == "games/anatomy-renamed/index.md");
        assert!(
            moved_plan.is_none(),
            "site-absolute link must not trigger a rewrite: {plans:?}"
        );
    }

    #[test]
    fn plan_mv_outbound_skips_url_schemes() {
        let vault = create_vault(&[(
            "sub/note.md",
            "See [link](https://example.com/x) and [mail](mailto:a@b.c).\n",
        )]);
        let plans = plan_mv(vault.path(), "sub/note.md", "archive/note.md", None).unwrap();
        let moved_plan = plans.iter().find(|p| p.rel_path == "archive/note.md");
        assert!(
            moved_plan.is_none(),
            "URL-scheme links must not be rewritten: {plans:?}"
        );
    }

    #[test]
    fn plan_mv_outbound_skips_fragment_only() {
        let vault = create_vault(&[("sub/note.md", "Jump to [top](#top).\n")]);
        let plans = plan_mv(vault.path(), "sub/note.md", "archive/note.md", None).unwrap();
        let moved_plan = plans.iter().find(|p| p.rel_path == "archive/note.md");
        assert!(
            moved_plan.is_none(),
            "fragment-only links must not be rewritten: {plans:?}"
        );
    }

    #[test]
    fn plan_mv_outbound_skips_bare_token_without_md_extension() {
        // `[obsidian](Note One)` — no `.md`, no path separator. Must be left
        // alone (treated as a label / Obsidian-style reference).
        let vault = create_vault(&[("sub/note.md", "See [obsidian](Note One) here.\n")]);
        let plans = plan_mv(vault.path(), "sub/note.md", "archive/note.md", None).unwrap();
        let moved_plan = plans.iter().find(|p| p.rel_path == "archive/note.md");
        assert!(
            moved_plan.is_none(),
            "bare non-md link must not be rewritten: {plans:?}"
        );
    }

    #[test]
    fn plan_mv_outbound_rewrites_genuine_relative_link() {
        // Regression: genuine relative links that would resolve differently
        // after the move are rebased. Here `sub/a.md` moves to `a.md` at the
        // root; the link `peer.md` (previously sub/peer.md) must be rewritten
        // to `sub/peer.md` from the new location.
        let vault = create_vault(&[
            ("sub/a.md", "See [peer](peer.md).\n"),
            ("sub/peer.md", "peer\n"),
        ]);
        let plans = plan_mv(vault.path(), "sub/a.md", "a.md", None).unwrap();
        let moved_plan = plans
            .iter()
            .find(|p| p.rel_path == "a.md")
            .expect("expected rewrite plan");
        assert_eq!(moved_plan.replacements.len(), 1);
        assert_eq!(moved_plan.replacements[0].old_text, "[peer](peer.md)");
        assert_eq!(moved_plan.replacements[0].new_text, "[peer](sub/peer.md)");
    }

    #[test]
    fn should_rewrite_outbound_target_rules() {
        assert!(!should_rewrite_outbound_target(""));
        assert!(!should_rewrite_outbound_target("/en-US/docs/x"));
        assert!(!should_rewrite_outbound_target("/page.md"));
        assert!(!should_rewrite_outbound_target("#anchor"));
        assert!(!should_rewrite_outbound_target("https://a.b/c"));
        assert!(!should_rewrite_outbound_target("mailto:a@b.c"));
        assert!(!should_rewrite_outbound_target("tel:+1"));
        assert!(!should_rewrite_outbound_target("Note One"));
        assert!(!should_rewrite_outbound_target("plain-label"));
        // Rewritable:
        assert!(should_rewrite_outbound_target("../notes/x.md"));
        assert!(should_rewrite_outbound_target("sub/x.md"));
        assert!(should_rewrite_outbound_target("x.md"));
        assert!(should_rewrite_outbound_target("sub/label"));
        // Fragments: classify by the path portion, not the whole target.
        assert!(
            should_rewrite_outbound_target("x.md#intro"),
            "anchored .md link should be rewritable"
        );
        assert!(
            should_rewrite_outbound_target("sub/x.md#section-1"),
            "anchored nested .md link should be rewritable"
        );
        assert!(
            !should_rewrite_outbound_target("#anchor-with-dashes"),
            "fragment-only target must still be skipped"
        );
        // Windows drive-letter paths are filesystem paths, not URL schemes.
        assert!(
            should_rewrite_outbound_target("C:/notes/x.md"),
            "Windows drive-letter forward-slash path should be rewritable"
        );
        assert!(
            should_rewrite_outbound_target("C:\\notes\\x.md"),
            "Windows drive-letter backslash path should be rewritable"
        );
    }

    #[test]
    fn plan_mv_outbound_rewrites_anchored_self_link() {
        // NEW-BUG-2 follow-up: self-links with fragments must still be rewritten
        // to point at the file's new location while preserving the anchor.
        let vault = create_vault(&[("self.md", "Jump to [intro](self.md#intro) in this file.\n")]);
        let plans = plan_mv(vault.path(), "self.md", "other.md", None).unwrap();
        assert_eq!(plans.len(), 1);
        let plan = &plans[0];
        assert_eq!(plan.rel_path, "other.md");
        assert_eq!(plan.replacements.len(), 1);
        assert_eq!(plan.replacements[0].old_text, "[intro](self.md#intro)");
        assert_eq!(plan.replacements[0].new_text, "[intro](other.md#intro)");
    }

    #[test]
    fn plan_mv_outbound_rewrites_anchored_relative_link_on_dir_change() {
        // Anchored relative links must be rebased when the directory changes.
        // `sub/a.md` moves to `a.md`; the link `peer.md#heading` should become
        // `sub/peer.md#heading` from the new location.
        let vault = create_vault(&[
            ("sub/a.md", "See [peer](peer.md#heading).\n"),
            ("sub/peer.md", "peer\n"),
        ]);
        let plans = plan_mv(vault.path(), "sub/a.md", "a.md", None).unwrap();
        let moved_plan = plans
            .iter()
            .find(|p| p.rel_path == "a.md")
            .expect("expected rewrite plan");
        assert_eq!(moved_plan.replacements.len(), 1);
        assert_eq!(
            moved_plan.replacements[0].old_text,
            "[peer](peer.md#heading)"
        );
        assert_eq!(
            moved_plan.replacements[0].new_text,
            "[peer](sub/peer.md#heading)"
        );
    }

    #[test]
    fn execute_plans_rejects_path_outside_vault() {
        // Construct a RewritePlan whose absolute path points outside the vault.
        // execute_plans must refuse to write it.
        let vault = create_vault(&[("a.md", "content\n")]);
        let outside = tempfile::tempdir().unwrap();
        let outside_path = outside.path().join("escaped.md");
        fs::write(&outside_path, "original\n").unwrap();

        let bad_plan = RewritePlan {
            path: outside_path.clone(),
            rel_path: "escaped.md".to_string(),
            replacements: vec![],
            rewritten_content: "malicious\n".to_string(),
            mtime: None,
        };

        let result = execute_plans(vault.path(), &[bad_plan]);
        assert!(result.is_err(), "must refuse to write outside vault");

        // Original file must be untouched.
        let content = fs::read_to_string(&outside_path).unwrap();
        assert_eq!(content, "original\n");
    }

    // ---------------------------------------------------------------------------
    // Case-insensitive inbound rewrite tests (BUG-6)
    // ---------------------------------------------------------------------------

    #[test]
    fn plan_mv_case_insensitive_wikilink_inbound() {
        // MDN-shape: file on disk `web/javascript/reference/iteration_protocols/index.md`.
        // Another file `promise/any/index.md` links to `Web/JavaScript/Reference/Iteration_protocols/index`
        // (PascalCase as commonly used in MDN-mirror vaults).
        // Moving the lowercase file should detect the PascalCase link as inbound.
        let vault = create_vault(&[
            (
                "web/javascript/reference/iteration_protocols/index.md",
                "# Iteration Protocols\n",
            ),
            (
                "promise/any/index.md",
                "See [[Web/JavaScript/Reference/Iteration_protocols/index]]\n",
            ),
        ]);
        let plans = plan_mv(
            vault.path(),
            "web/javascript/reference/iteration_protocols/index.md",
            "web/javascript/reference/iteration_protocols_v2/index.md",
            None,
        )
        .unwrap();

        // promise/any/index.md should have been detected as an inbound link source.
        let promise_plan = plans.iter().find(|p| p.rel_path == "promise/any/index.md");
        assert!(
            promise_plan.is_some(),
            "case-insensitive inbound link in promise/any/index.md should produce a rewrite plan; got plans: {plans:?}"
        );
    }

    #[test]
    fn plan_mv_case_insensitive_markdown_link_inbound() {
        // Same BUG-6 scenario but with a markdown-style link.
        let vault = create_vault(&[
            ("web/foo.md", "Content\n"),
            ("other.md", "See [link](Web/Foo.md)\n"),
        ]);
        let plans = plan_mv(vault.path(), "web/foo.md", "archive/foo.md", None).unwrap();

        let other_plan = plans.iter().find(|p| p.rel_path == "other.md");
        assert!(
            other_plan.is_some(),
            "case-insensitive inbound markdown link should produce a rewrite plan; got: {plans:?}"
        );
    }
}