mbr-markdown-browser 0.4.5

A fast, featureful markdown viewer, browser, and (optional) static site generator
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
//! Link transformation for trailing-slash URL convention.
//!
//! When markdown files are served with trailing-slash URLs (e.g., `docs/guide.md` → `/docs/guide/`),
//! relative links in the markdown need to be adjusted so they resolve correctly from the browser's
//! perspective.
//!
//! ## Problem
//!
//! A link `[other](other.md)` in `docs/guide.md`:
//! - Filesystem: refers to `docs/other.md` (sibling file)
//! - From URL `/docs/guide/`: browser resolves `other.md` as `/docs/guide/other.md` (WRONG)
//! - Correct URL: `/docs/other/`
//!
//! ## Solution
//!
//! Transform relative links by:
//! 1. Adding `../` prefix for regular markdown files (not index files)
//! 2. Replacing markdown extensions with trailing slash
//! 3. Collapsing index file references to their directory

/// Configuration for link transformation.
#[derive(Debug, Clone)]
pub struct LinkTransformConfig {
    /// Markdown file extensions (e.g., ["md", "markdown"])
    pub markdown_extensions: Vec<String>,
    /// Index filename (e.g., "index.md")
    pub index_file: String,
    /// Whether the current file is an index file (affects ../ prefix)
    pub is_index_file: bool,
    /// Page depth for converting root-relative URLs to relative (build mode).
    /// None = leave root-relative URLs unchanged (server mode).
    pub url_depth: Option<usize>,
}

impl Default for LinkTransformConfig {
    fn default() -> Self {
        Self {
            markdown_extensions: vec!["md".to_string()],
            index_file: "index.md".to_string(),
            is_index_file: false,
            url_depth: None,
        }
    }
}

/// Transform a relative link URL for the trailing-slash URL convention.
///
/// # Rules
///
/// 1. Absolute URLs (`http://`, `https://`, `//`) → unchanged
/// 2. Root-relative URLs (starts with `/`) → unchanged
/// 3. Anchor-only links (`#...`) → unchanged
/// 4. Data/javascript URLs → unchanged
/// 5. Relative markdown links → prepend `../` (if not index file), replace extension with `/`
/// 6. Relative static files → prepend `../` (if not index file)
///
/// # Examples
///
/// ```
/// use mbr::link_transform::{transform_link, LinkTransformConfig};
///
/// let config = LinkTransformConfig {
///     markdown_extensions: vec!["md".to_string()],
///     index_file: "index.md".to_string(),
///     is_index_file: false,
///     url_depth: None,
/// };
///
/// // Regular markdown file: add ../ and trailing slash
/// assert_eq!(transform_link("other.md", &config), "../other/");
///
/// // Index file config: no ../ prefix
/// let index_config = LinkTransformConfig { is_index_file: true, ..config.clone() };
/// assert_eq!(transform_link("other.md", &index_config), "other/");
///
/// // Absolute URLs unchanged
/// assert_eq!(transform_link("https://example.com", &config), "https://example.com");
/// ```
pub fn transform_link(url: &str, config: &LinkTransformConfig) -> String {
    // Empty or whitespace-only
    if url.is_empty() || url.trim().is_empty() {
        return url.to_string();
    }

    // Anchor-only links
    if url.starts_with('#') {
        return url.to_string();
    }

    // Absolute URLs (http://, https://, //)
    if is_absolute_url(url) {
        return url.to_string();
    }

    // Root-relative URLs — convert to relative in build mode
    if url.starts_with('/') {
        return match config.url_depth {
            Some(depth) => make_relative_url(url, depth),
            None => url.to_string(),
        };
    }

    // Data URLs and javascript URLs
    if url.starts_with("data:") || url.starts_with("javascript:") {
        return url.to_string();
    }

    // Special protocol links (mailto, tel, sms, etc.) - leave unchanged
    if url.starts_with("mailto:")
        || url.starts_with("tel:")
        || url.starts_with("sms:")
        || url.starts_with("callto:")
    {
        return url.to_string();
    }

    // Split into path and suffix (anchor/query)
    let (path, suffix) = split_url_parts(url);

    // Empty path after splitting (e.g., just "?query" or malformed)
    if path.is_empty() {
        return url.to_string();
    }

    // Normalize: strip leading "./"
    let path = path.strip_prefix("./").unwrap_or(&path);

    // Count and strip existing "../" prefixes
    let (parent_count, remaining_path) = count_parent_traversals(path);

    // If nothing remains after stripping ../, just return with adjusted parents
    if remaining_path.is_empty() {
        let prefix = if config.is_index_file {
            "../".repeat(parent_count)
        } else {
            "../".repeat(parent_count + 1)
        };
        return format!("{}{}", prefix, suffix);
    }

    // Check if it's a markdown file
    if let Some(base_path) = strip_markdown_extension(remaining_path, &config.markdown_extensions) {
        // Check if it ends with index file (without extension)
        let index_stem = config
            .index_file
            .strip_suffix(".md")
            .or_else(|| config.index_file.strip_suffix(".markdown"))
            .unwrap_or(&config.index_file);

        let final_path = if base_path.ends_with(index_stem) {
            // Collapse index file to directory
            let stripped = base_path
                .strip_suffix(index_stem)
                .unwrap_or(base_path)
                .trim_end_matches('/');
            if stripped.is_empty() {
                // Just "index.md" -> "./" for index files, "../" for regular
                "".to_string()
            } else {
                format!("{}/", stripped)
            }
        } else {
            format!("{}/", base_path)
        };

        // Build prefix based on parent count and whether current file is index
        let prefix = if config.is_index_file {
            "../".repeat(parent_count)
        } else {
            "../".repeat(parent_count + 1)
        };

        // Handle edge case: if final_path is empty and we have no prefix, use "./"
        if final_path.is_empty() && prefix.is_empty() {
            return format!("./{}", suffix);
        }

        return format!("{}{}{}", prefix, final_path, suffix);
    }

    // Static file: just add ../ prefix
    let prefix = if config.is_index_file {
        "../".repeat(parent_count)
    } else {
        "../".repeat(parent_count + 1)
    };

    format!("{}{}{}", prefix, remaining_path, suffix)
}

/// Check if a URL is absolute (has protocol or is protocol-relative).
fn is_absolute_url(url: &str) -> bool {
    url.starts_with("http://")
        || url.starts_with("https://")
        || url.starts_with("//")
        || url.starts_with("ftp://")
        || url.starts_with("file://")
}

/// Split a URL into path and suffix (anchor # or query ?).
/// Returns (path, suffix) where suffix includes the delimiter.
fn split_url_parts(url: &str) -> (String, String) {
    // Find first occurrence of # or ?
    let anchor_pos = url.find('#');
    let query_pos = url.find('?');

    let split_pos = match (anchor_pos, query_pos) {
        (Some(a), Some(q)) => Some(a.min(q)),
        (Some(a), None) => Some(a),
        (None, Some(q)) => Some(q),
        (None, None) => None,
    };

    match split_pos {
        Some(pos) => (url[..pos].to_string(), url[pos..].to_string()),
        None => (url.to_string(), String::new()),
    }
}

/// Count leading "../" sequences and return (count, remaining_path).
fn count_parent_traversals(path: &str) -> (usize, &str) {
    let mut count = 0;
    let mut remaining = path;

    while let Some(rest) = remaining.strip_prefix("../") {
        count += 1;
        remaining = rest;
    }

    (count, remaining)
}

/// Strip markdown extension if present, returning the base path.
fn strip_markdown_extension<'a>(path: &'a str, extensions: &[String]) -> Option<&'a str> {
    for ext in extensions {
        let suffix = format!(".{}", ext);
        if path.ends_with(&suffix) {
            return Some(&path[..path.len() - suffix.len()]);
        }
    }
    None
}

/// Convert an absolute URL path to a relative URL from the given depth.
///
/// Examples (from depth 2):
/// - "/" → "../../"
/// - "/docs/" → "../../docs/"
/// - "/docs/guide/" → "../../docs/guide/"
pub fn make_relative_url(absolute_url: &str, depth: usize) -> String {
    let target = absolute_url.trim_start_matches('/');
    if target.is_empty() {
        // Link to root
        if depth == 0 {
            "./".to_string()
        } else {
            "../".repeat(depth)
        }
    } else {
        // Go up to root, then down to target
        if depth == 0 {
            target.to_string()
        } else {
            format!("{}{}", "../".repeat(depth), target)
        }
    }
}

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

    fn regular_config() -> LinkTransformConfig {
        LinkTransformConfig {
            markdown_extensions: vec!["md".to_string(), "markdown".to_string()],
            index_file: "index.md".to_string(),
            is_index_file: false,
            url_depth: None,
        }
    }

    fn index_config() -> LinkTransformConfig {
        LinkTransformConfig {
            is_index_file: true,
            ..regular_config()
        }
    }

    // =========================================================================
    // Regular markdown files (is_index_file: false)
    // =========================================================================

    #[test]
    fn test_simple_relative_md() {
        assert_eq!(transform_link("other.md", &regular_config()), "../other/");
    }

    #[test]
    fn test_subdirectory_md() {
        assert_eq!(
            transform_link("sub/doc.md", &regular_config()),
            "../sub/doc/"
        );
    }

    #[test]
    fn test_parent_traversal() {
        assert_eq!(
            transform_link("../other.md", &regular_config()),
            "../../other/"
        );
    }

    #[test]
    fn test_double_parent() {
        assert_eq!(
            transform_link("../../root.md", &regular_config()),
            "../../../root/"
        );
    }

    #[test]
    fn test_index_collapse() {
        assert_eq!(
            transform_link("folder/index.md", &regular_config()),
            "../folder/"
        );
    }

    #[test]
    fn test_nested_index_collapse() {
        assert_eq!(transform_link("a/b/index.md", &regular_config()), "../a/b/");
    }

    #[test]
    fn test_just_index_md() {
        // Link to index.md in same directory
        assert_eq!(transform_link("index.md", &regular_config()), "../");
    }

    #[test]
    fn test_static_file() {
        assert_eq!(
            transform_link("image.png", &regular_config()),
            "../image.png"
        );
    }

    #[test]
    fn test_nested_static() {
        assert_eq!(
            transform_link("assets/img.png", &regular_config()),
            "../assets/img.png"
        );
    }

    #[test]
    fn test_md_with_anchor() {
        assert_eq!(
            transform_link("other.md#section", &regular_config()),
            "../other/#section"
        );
    }

    #[test]
    fn test_md_with_query() {
        assert_eq!(
            transform_link("other.md?foo=bar", &regular_config()),
            "../other/?foo=bar"
        );
    }

    #[test]
    fn test_md_with_query_and_anchor() {
        assert_eq!(
            transform_link("other.md?foo=bar#section", &regular_config()),
            "../other/?foo=bar#section"
        );
    }

    #[test]
    fn test_explicit_current_dir() {
        assert_eq!(transform_link("./other.md", &regular_config()), "../other/");
    }

    #[test]
    fn test_alternate_extension() {
        assert_eq!(
            transform_link("other.markdown", &regular_config()),
            "../other/"
        );
    }

    #[test]
    fn test_parent_static_file() {
        assert_eq!(
            transform_link("../image.png", &regular_config()),
            "../../image.png"
        );
    }

    // =========================================================================
    // Index files (is_index_file: true)
    // =========================================================================

    #[test]
    fn test_index_simple_relative_md() {
        assert_eq!(transform_link("other.md", &index_config()), "other/");
    }

    #[test]
    fn test_index_subdirectory_md() {
        assert_eq!(transform_link("sub/doc.md", &index_config()), "sub/doc/");
    }

    #[test]
    fn test_index_parent_traversal() {
        assert_eq!(transform_link("../other.md", &index_config()), "../other/");
    }

    #[test]
    fn test_index_double_parent() {
        assert_eq!(
            transform_link("../../root.md", &index_config()),
            "../../root/"
        );
    }

    #[test]
    fn test_index_static_file() {
        // Index files don't need ../ for siblings
        assert_eq!(transform_link("image.png", &index_config()), "image.png");
    }

    #[test]
    fn test_index_nested_static() {
        assert_eq!(
            transform_link("assets/img.png", &index_config()),
            "assets/img.png"
        );
    }

    #[test]
    fn test_index_md_with_anchor() {
        assert_eq!(
            transform_link("other.md#section", &index_config()),
            "other/#section"
        );
    }

    #[test]
    fn test_index_parent_static() {
        assert_eq!(
            transform_link("../image.png", &index_config()),
            "../image.png"
        );
    }

    #[test]
    fn test_index_to_index_collapse() {
        assert_eq!(
            transform_link("folder/index.md", &index_config()),
            "folder/"
        );
    }

    // =========================================================================
    // URLs that should be unchanged (both modes)
    // =========================================================================

    #[test]
    fn test_absolute_https() {
        let url = "https://example.com/path";
        assert_eq!(transform_link(url, &regular_config()), url);
        assert_eq!(transform_link(url, &index_config()), url);
    }

    #[test]
    fn test_absolute_http() {
        let url = "http://example.com/path";
        assert_eq!(transform_link(url, &regular_config()), url);
        assert_eq!(transform_link(url, &index_config()), url);
    }

    #[test]
    fn test_protocol_relative() {
        let url = "//cdn.example.com/file.js";
        assert_eq!(transform_link(url, &regular_config()), url);
        assert_eq!(transform_link(url, &index_config()), url);
    }

    #[test]
    fn test_root_relative() {
        let url = "/docs/guide/";
        assert_eq!(transform_link(url, &regular_config()), url);
        assert_eq!(transform_link(url, &index_config()), url);
    }

    #[test]
    fn test_anchor_only() {
        let url = "#section";
        assert_eq!(transform_link(url, &regular_config()), url);
        assert_eq!(transform_link(url, &index_config()), url);
    }

    #[test]
    fn test_empty_link() {
        assert_eq!(transform_link("", &regular_config()), "");
        assert_eq!(transform_link("", &index_config()), "");
    }

    #[test]
    fn test_data_url() {
        let url = "data:image/png;base64,abc123";
        assert_eq!(transform_link(url, &regular_config()), url);
    }

    #[test]
    fn test_javascript_url() {
        let url = "javascript:void(0)";
        assert_eq!(transform_link(url, &regular_config()), url);
    }

    #[test]
    fn test_mailto_url() {
        let url = "mailto:test@example.com";
        assert_eq!(transform_link(url, &regular_config()), url);
    }

    #[test]
    fn test_ftp_url() {
        let url = "ftp://ftp.example.com/file.txt";
        assert_eq!(transform_link(url, &regular_config()), url);
    }

    // =========================================================================
    // Edge cases
    // =========================================================================

    #[test]
    fn test_file_with_dots_in_name() {
        // my.file.md should only strip the final .md
        assert_eq!(
            transform_link("my.file.md", &regular_config()),
            "../my.file/"
        );
    }

    #[test]
    fn test_non_md_extension() {
        assert_eq!(
            transform_link("readme.txt", &regular_config()),
            "../readme.txt"
        );
    }

    #[test]
    fn test_just_query() {
        // Edge case: just a query string
        assert_eq!(transform_link("?foo=bar", &regular_config()), "?foo=bar");
    }

    #[test]
    fn test_deeply_nested_path() {
        assert_eq!(
            transform_link("a/b/c/d/file.md", &regular_config()),
            "../a/b/c/d/file/"
        );
    }

    #[test]
    fn test_mixed_traversal_and_descent() {
        assert_eq!(
            transform_link("../sibling/doc.md", &regular_config()),
            "../../sibling/doc/"
        );
    }

    // =========================================================================
    // Root-relative URL transformation with url_depth (build mode)
    // =========================================================================

    fn build_config(depth: usize) -> LinkTransformConfig {
        LinkTransformConfig {
            url_depth: Some(depth),
            ..regular_config()
        }
    }

    #[test]
    fn test_root_relative_with_depth_0() {
        assert_eq!(
            transform_link("/videos/demo.mp4", &build_config(0)),
            "videos/demo.mp4"
        );
    }

    #[test]
    fn test_root_relative_with_depth_1() {
        assert_eq!(
            transform_link("/videos/demo.mp4", &build_config(1)),
            "../videos/demo.mp4"
        );
    }

    #[test]
    fn test_root_relative_with_depth_2() {
        assert_eq!(
            transform_link("/videos/demo.mp4", &build_config(2)),
            "../../videos/demo.mp4"
        );
    }

    #[test]
    fn test_root_relative_to_root_with_depth() {
        assert_eq!(transform_link("/", &build_config(0)), "./");
        assert_eq!(transform_link("/", &build_config(1)), "../");
        assert_eq!(transform_link("/", &build_config(2)), "../../");
    }

    #[test]
    fn test_root_relative_tag_link_with_depth() {
        assert_eq!(
            transform_link("/tags/rust/", &build_config(2)),
            "../../tags/rust/"
        );
    }

    #[test]
    fn test_root_relative_unchanged_without_depth() {
        // Server mode (url_depth: None) leaves root-relative unchanged
        assert_eq!(
            transform_link("/videos/demo.mp4", &regular_config()),
            "/videos/demo.mp4"
        );
        assert_eq!(
            transform_link("/tags/rust/", &regular_config()),
            "/tags/rust/"
        );
    }

    // =========================================================================
    // make_relative_url
    // =========================================================================

    #[test]
    fn test_make_relative_url_to_root() {
        assert_eq!(make_relative_url("/", 0), "./");
        assert_eq!(make_relative_url("/", 1), "../");
        assert_eq!(make_relative_url("/", 2), "../../");
    }

    #[test]
    fn test_make_relative_url_to_path() {
        assert_eq!(make_relative_url("/docs/", 0), "docs/");
        assert_eq!(make_relative_url("/docs/guide/", 0), "docs/guide/");
        assert_eq!(make_relative_url("/docs/", 1), "../docs/");
        assert_eq!(make_relative_url("/other/", 1), "../other/");
        assert_eq!(make_relative_url("/docs/", 2), "../../docs/");
        assert_eq!(make_relative_url("/docs/guide/", 2), "../../docs/guide/");
    }
}

#[cfg(test)]
mod proptests {
    use super::*;
    use proptest::prelude::*;

    fn regular_config() -> LinkTransformConfig {
        LinkTransformConfig {
            markdown_extensions: vec!["md".to_string(), "markdown".to_string()],
            index_file: "index.md".to_string(),
            is_index_file: false,
            url_depth: None,
        }
    }

    fn index_config() -> LinkTransformConfig {
        LinkTransformConfig {
            is_index_file: true,
            ..regular_config()
        }
    }

    proptest! {
        /// Transformation is deterministic
        #[test]
        fn prop_deterministic(url in ".*") {
            let config = regular_config();
            let r1 = transform_link(&url, &config);
            let r2 = transform_link(&url, &config);
            prop_assert_eq!(r1, r2);
        }

        /// Absolute HTTPS URLs are never modified
        #[test]
        fn prop_https_unchanged(path in "[a-zA-Z0-9./_-]*") {
            let url = format!("https://example.com/{}", path);
            let config = regular_config();
            prop_assert_eq!(transform_link(&url, &config), url);
        }

        /// Absolute HTTP URLs are never modified
        #[test]
        fn prop_http_unchanged(path in "[a-zA-Z0-9./_-]*") {
            let url = format!("http://example.com/{}", path);
            let config = regular_config();
            prop_assert_eq!(transform_link(&url, &config), url);
        }

        /// Protocol-relative URLs are never modified
        #[test]
        fn prop_protocol_relative_unchanged(path in "[a-zA-Z0-9./_-]*") {
            let url = format!("//cdn.example.com/{}", path);
            let config = regular_config();
            prop_assert_eq!(transform_link(&url, &config), url);
        }

        /// Root-relative URLs are unchanged when url_depth is None (server mode)
        #[test]
        fn prop_root_relative_unchanged(path in "/[a-zA-Z0-9./_-]*") {
            let config = regular_config();
            prop_assert_eq!(transform_link(&path, &config), path);
        }

        /// Root-relative URLs are relativized when url_depth is Some (build mode)
        #[test]
        fn prop_root_relative_relativized(
            path in "[a-zA-Z][a-zA-Z0-9/_-]{0,20}",
            depth in 0usize..5
        ) {
            let url = format!("/{}", path);
            let mut config = regular_config();
            config.url_depth = Some(depth);
            let result = transform_link(&url, &config);
            // Result should NOT start with /
            prop_assert!(!result.starts_with('/'), "Should be relative: {}", result);
            // Result should contain the original path (without leading /)
            prop_assert!(result.ends_with(&path), "Should end with path {}: {}", path, result);
        }

        /// Anchor-only links are never modified
        #[test]
        fn prop_anchor_only_unchanged(anchor in "#[a-zA-Z0-9_-]*") {
            let config = regular_config();
            prop_assert_eq!(transform_link(&anchor, &config), anchor);
        }

        /// Empty links are never modified
        #[test]
        fn prop_empty_unchanged(_dummy in 0..1i32) {
            let config = regular_config();
            prop_assert_eq!(transform_link("", &config), "");
        }

        /// Regular markdown links always get ../ prepended
        #[test]
        fn prop_regular_md_gets_parent(name in "[a-zA-Z][a-zA-Z0-9_-]{0,20}") {
            let url = format!("{}.md", name);
            let config = regular_config();
            let result = transform_link(&url, &config);
            prop_assert!(result.starts_with("../"), "Expected ../ prefix: {}", result);
        }

        /// Index file markdown links don't get extra ../
        #[test]
        fn prop_index_md_no_extra_parent(name in "[a-zA-Z][a-zA-Z0-9_-]{0,20}") {
            let url = format!("{}.md", name);
            let config = index_config();
            let result = transform_link(&url, &config);
            prop_assert!(!result.starts_with("../"), "Should not have ../ prefix: {}", result);
        }

        /// Transformed markdown links end with /
        #[test]
        fn prop_md_ends_with_slash(name in "[a-zA-Z][a-zA-Z0-9_-]{0,20}") {
            let url = format!("{}.md", name);
            let config = regular_config();
            let result = transform_link(&url, &config);
            // Strip any anchor/query to check the path
            let base = result.split(&['?', '#'][..]).next().unwrap();
            prop_assert!(base.ends_with('/'), "Path should end with /: {}", base);
        }

        /// Anchors are preserved through transformation
        #[test]
        fn prop_anchor_preserved(
            name in "[a-zA-Z][a-zA-Z0-9_-]{0,10}",
            anchor in "[a-zA-Z][a-zA-Z0-9_-]{0,10}"
        ) {
            let url = format!("{}.md#{}", name, anchor);
            let config = regular_config();
            let result = transform_link(&url, &config);
            prop_assert!(result.contains(&format!("#{}", anchor)), "Anchor not preserved: {}", result);
        }

        /// Query strings are preserved through transformation
        #[test]
        fn prop_query_preserved(
            name in "[a-zA-Z][a-zA-Z0-9_-]{0,10}",
            query in "[a-zA-Z][a-zA-Z0-9_=-]{0,10}"
        ) {
            let url = format!("{}.md?{}", name, query);
            let config = regular_config();
            let result = transform_link(&url, &config);
            prop_assert!(result.contains(&format!("?{}", query)), "Query not preserved: {}", result);
        }
    }
}