rumdl 0.1.51

A fast Markdown linter written in Rust (Ru(st) MarkDown Linter)
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
/// MkDocs PyMdown extensions support
///
/// This module provides support for various PyMdown Markdown extensions
/// commonly used with MkDocs Material:
///
/// - **InlineHilite**: Inline code highlighting `` `#!python code` ``
/// - **Keys**: Keyboard key notation `++ctrl+alt+delete++`
/// - **Caret**: Superscript and insert `^superscript^` and `^^insert^^`
/// - **Tilde**: Subscript and strikethrough `~subscript~` and `~~strike~~`
/// - **Mark**: Highlight text `==highlighted==`
/// - **SmartSymbols**: Auto-replace symbols `(c)` → `©`
///
/// ## Architecture
///
/// All markup detection follows a consistent span-based pattern:
/// 1. `find_*_spans(line) -> Vec<(usize, usize)>` - find byte ranges
/// 2. `contains_*(line) -> bool` - check if markup exists
/// 3. `is_in_*(line, position) -> bool` - check if position is inside markup
///
/// For double-takes-precedence patterns (caret: ^^/^, tilde: ~~/~):
/// - Double-delimiter spans are found first
/// - Single-delimiter spans exclude positions inside double spans
///
/// ## References
///
/// - [PyMdown Extensions](https://facelessuser.github.io/pymdown-extensions/)
use regex::Regex;
use std::sync::LazyLock;

// ============================================================================
// Core span utilities
// ============================================================================

/// Check if a byte position falls within any span.
/// Assumes spans are sorted by start position for early-exit optimization.
#[inline]
fn position_in_spans(position: usize, spans: &[(usize, usize)]) -> bool {
    for &(start, end) in spans {
        if position < start {
            return false;
        }
        if position < end {
            return true;
        }
    }
    false
}

/// Find all regex matches as (start, end) byte spans.
#[inline]
fn find_regex_spans(line: &str, pattern: &Regex) -> Vec<(usize, usize)> {
    pattern.find_iter(line).map(|m| (m.start(), m.end())).collect()
}

/// Find single-delimiter spans (like `~sub~` or `^super^`) that are NOT inside
/// double-delimiter spans (like `~~strike~~` or `^^insert^^`).
///
/// Rules for single-delimiter content:
/// - Must have at least one character between delimiters
/// - Cannot contain whitespace (per PyMdown spec)
/// - Cannot be inside a double-delimiter span
fn find_single_delim_spans(line: &str, delim: char, double_spans: &[(usize, usize)]) -> Vec<(usize, usize)> {
    let mut spans = Vec::new();
    let mut chars = line.char_indices().peekable();
    let delim_len = delim.len_utf8();

    while let Some((start_byte, ch)) = chars.next() {
        // Skip if inside a double-delimiter span
        if position_in_spans(start_byte, double_spans) {
            continue;
        }

        if ch != delim {
            continue;
        }

        // Check if this is a double delimiter (skip it entirely)
        if chars.peek().is_some_and(|(_, c)| *c == delim) {
            chars.next();
            continue;
        }

        // Look for closing single delimiter
        let mut found_content = false;
        let mut has_whitespace = false;

        for (byte_pos, inner_ch) in chars.by_ref() {
            // If we enter a double-delimiter span, stop looking
            if position_in_spans(byte_pos, double_spans) {
                break;
            }

            if inner_ch == delim {
                // Check it's not the start of a double delimiter
                let is_double = chars.peek().is_some_and(|(_, c)| *c == delim);
                if !is_double && found_content && !has_whitespace {
                    spans.push((start_byte, byte_pos + delim_len));
                }
                break;
            }

            found_content = true;
            if inner_ch.is_whitespace() {
                has_whitespace = true;
            }
        }
    }

    spans
}

/// Merge overlapping or adjacent spans. Input must be sorted by start.
fn merge_spans(spans: &[(usize, usize)]) -> Vec<(usize, usize)> {
    if spans.is_empty() {
        return Vec::new();
    }

    let mut merged = Vec::with_capacity(spans.len());
    let mut current = spans[0];

    for &(start, end) in &spans[1..] {
        if start <= current.1 {
            current.1 = current.1.max(end);
        } else {
            merged.push(current);
            current = (start, end);
        }
    }
    merged.push(current);
    merged
}

// ============================================================================
// InlineHilite: `#!lang code` syntax for inline code with syntax highlighting
// ============================================================================

/// Pattern to match InlineHilite syntax: `#!language code`
static INLINE_HILITE_PATTERN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"`#!([a-zA-Z][a-zA-Z0-9_+-]*)\s+[^`]+`").unwrap());

/// Pattern to match inline hilite shebang at the start of backtick content
static INLINE_HILITE_SHEBANG: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^#!([a-zA-Z][a-zA-Z0-9_+-]*)").unwrap());

/// Check if a line contains InlineHilite syntax
#[inline]
pub fn contains_inline_hilite(line: &str) -> bool {
    line.contains('`') && line.contains("#!") && INLINE_HILITE_PATTERN.is_match(line)
}

/// Check if code span content starts with InlineHilite shebang
#[inline]
pub fn is_inline_hilite_content(content: &str) -> bool {
    INLINE_HILITE_SHEBANG.is_match(content)
}

// ============================================================================
// Keys: ++key++ syntax for keyboard keys
// ============================================================================

/// Pattern to match keyboard key notation: `++key++` or `++key1+key2++`
static KEYS_PATTERN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"\+\+([a-zA-Z0-9_-]+(?:\+[a-zA-Z0-9_-]+)*)\+\+").unwrap());

/// Common keyboard key names for validation
pub const COMMON_KEYS: &[&str] = &[
    "ctrl",
    "alt",
    "shift",
    "cmd",
    "meta",
    "win",
    "windows",
    "option",
    "enter",
    "return",
    "tab",
    "space",
    "backspace",
    "delete",
    "del",
    "insert",
    "ins",
    "home",
    "end",
    "pageup",
    "pagedown",
    "up",
    "down",
    "left",
    "right",
    "escape",
    "esc",
    "capslock",
    "numlock",
    "scrolllock",
    "printscreen",
    "pause",
    "break",
    "f1",
    "f2",
    "f3",
    "f4",
    "f5",
    "f6",
    "f7",
    "f8",
    "f9",
    "f10",
    "f11",
    "f12",
];

/// Parsed keyboard shortcut
#[derive(Debug, Clone, PartialEq)]
pub struct KeyboardShortcut {
    pub full_text: String,
    pub keys: Vec<String>,
    pub start: usize,
    pub end: usize,
}

/// Find all keyboard shortcut spans
pub fn find_keys_spans(line: &str) -> Vec<(usize, usize)> {
    if !line.contains("++") {
        return Vec::new();
    }
    find_regex_spans(line, &KEYS_PATTERN)
}

/// Check if a line contains keyboard key notation
#[inline]
pub fn contains_keys(line: &str) -> bool {
    line.contains("++") && KEYS_PATTERN.is_match(line)
}

/// Find all keyboard shortcuts in a line
pub fn find_keyboard_shortcuts(line: &str) -> Vec<KeyboardShortcut> {
    if !line.contains("++") {
        return Vec::new();
    }

    KEYS_PATTERN
        .find_iter(line)
        .map(|m| {
            let full_text = m.as_str().to_string();
            let inner = &full_text[2..full_text.len() - 2];
            let keys = inner.split('+').map(String::from).collect();
            KeyboardShortcut {
                full_text,
                keys,
                start: m.start(),
                end: m.end(),
            }
        })
        .collect()
}

/// Check if a position in a line is within a keyboard shortcut
pub fn is_in_keys(line: &str, position: usize) -> bool {
    position_in_spans(position, &find_keys_spans(line))
}

// ============================================================================
// Caret: ^superscript^ and ^^insert^^ syntax
// ============================================================================

/// Pattern to match insert: `^^text^^` (double caret)
/// Handles content with single carets inside (e.g., `^^a^b^^`)
static INSERT_PATTERN: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\^\^[^\^]+(?:\^[^\^]+)*\^\^").unwrap());

/// Find all insert (^^text^^) spans
pub fn find_insert_spans(line: &str) -> Vec<(usize, usize)> {
    if !line.contains("^^") {
        return Vec::new();
    }
    find_regex_spans(line, &INSERT_PATTERN)
}

/// Find all superscript (^text^) spans, excluding those inside insert spans
pub fn find_superscript_spans(line: &str) -> Vec<(usize, usize)> {
    if !line.contains('^') {
        return Vec::new();
    }
    let insert_spans = find_insert_spans(line);
    find_single_delim_spans(line, '^', &insert_spans)
}

/// Check if a line contains superscript syntax (^text^ not inside ^^insert^^)
#[inline]
pub fn contains_superscript(line: &str) -> bool {
    !find_superscript_spans(line).is_empty()
}

/// Check if a line contains insert syntax (^^text^^)
#[inline]
pub fn contains_insert(line: &str) -> bool {
    line.contains("^^") && INSERT_PATTERN.is_match(line)
}

/// Check if a position is within superscript or insert markup
pub fn is_in_caret_markup(line: &str, position: usize) -> bool {
    if !line.contains('^') {
        return false;
    }
    let insert_spans = find_insert_spans(line);
    if position_in_spans(position, &insert_spans) {
        return true;
    }
    let super_spans = find_single_delim_spans(line, '^', &insert_spans);
    position_in_spans(position, &super_spans)
}

// ============================================================================
// Tilde: ~subscript~ and ~~strikethrough~~ syntax
// ============================================================================

/// Pattern to match strikethrough: `~~text~~` (double tilde)
/// Handles content with single tildes inside (e.g., `~~a~b~~`)
static STRIKETHROUGH_PATTERN: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"~~[^~]+(?:~[^~]+)*~~").unwrap());

/// Find all strikethrough (~~text~~) spans
pub fn find_strikethrough_spans(line: &str) -> Vec<(usize, usize)> {
    if !line.contains("~~") {
        return Vec::new();
    }
    find_regex_spans(line, &STRIKETHROUGH_PATTERN)
}

/// Find all subscript (~text~) spans, excluding those inside strikethrough spans
pub fn find_subscript_spans(line: &str) -> Vec<(usize, usize)> {
    if !line.contains('~') {
        return Vec::new();
    }
    let strike_spans = find_strikethrough_spans(line);
    find_single_delim_spans(line, '~', &strike_spans)
}

/// Check if a line contains subscript syntax (~text~ not inside ~~strike~~)
#[inline]
pub fn contains_subscript(line: &str) -> bool {
    !find_subscript_spans(line).is_empty()
}

/// Check if a line contains strikethrough syntax (~~text~~)
#[inline]
pub fn contains_strikethrough(line: &str) -> bool {
    line.contains("~~") && STRIKETHROUGH_PATTERN.is_match(line)
}

/// Check if a position is within subscript or strikethrough markup
pub fn is_in_tilde_markup(line: &str, position: usize) -> bool {
    if !line.contains('~') {
        return false;
    }
    let strike_spans = find_strikethrough_spans(line);
    if position_in_spans(position, &strike_spans) {
        return true;
    }
    let sub_spans = find_single_delim_spans(line, '~', &strike_spans);
    position_in_spans(position, &sub_spans)
}

// ============================================================================
// Mark: ==highlighted== syntax
// ============================================================================

/// Pattern to match highlight/mark: `==text==`
static MARK_PATTERN: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"==([^=]+)==").unwrap());

/// Find all mark (==text==) spans
pub fn find_mark_spans(line: &str) -> Vec<(usize, usize)> {
    if !line.contains("==") {
        return Vec::new();
    }
    find_regex_spans(line, &MARK_PATTERN)
}

/// Check if a line contains mark/highlight syntax
#[inline]
pub fn contains_mark(line: &str) -> bool {
    line.contains("==") && MARK_PATTERN.is_match(line)
}

/// Check if a position is within mark markup
pub fn is_in_mark(line: &str, position: usize) -> bool {
    position_in_spans(position, &find_mark_spans(line))
}

// ============================================================================
// SmartSymbols: (c), (tm), (r), -->, <--, etc.
// ============================================================================

/// Pattern to match any SmartSymbol that might be replaced
static SMART_SYMBOL_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?:\(c\)|\(C\)|\(r\)|\(R\)|\(tm\)|\(TM\)|\(p\)|\.\.\.|-{2,3}|<->|<-|->|<=>|<=|=>|1/4|1/2|3/4|\+-|!=)")
        .unwrap()
});

/// Find all SmartSymbol spans
pub fn find_smart_symbol_spans(line: &str) -> Vec<(usize, usize)> {
    // Quick rejection checks
    if !line.contains('(')
        && !line.contains("...")
        && !line.contains("--")
        && !line.contains("->")
        && !line.contains("<-")
        && !line.contains("=>")
        && !line.contains("<=")
        && !line.contains("1/")
        && !line.contains("3/")
        && !line.contains("+-")
        && !line.contains("!=")
    {
        return Vec::new();
    }
    find_regex_spans(line, &SMART_SYMBOL_PATTERN)
}

/// Check if a line contains potential SmartSymbol patterns
#[inline]
pub fn contains_smart_symbols(line: &str) -> bool {
    !find_smart_symbol_spans(line).is_empty()
}

/// Check if a position is at a SmartSymbol
pub fn is_in_smart_symbol(line: &str, position: usize) -> bool {
    position_in_spans(position, &find_smart_symbol_spans(line))
}

// ============================================================================
// Combined utilities
// ============================================================================

/// Check if a position is within any PyMdown extension markup
pub fn is_in_pymdown_markup(line: &str, position: usize) -> bool {
    is_in_keys(line, position)
        || is_in_caret_markup(line, position)
        || is_in_tilde_markup(line, position)
        || is_in_mark(line, position)
        || is_in_smart_symbol(line, position)
}

/// Mask all PyMdown extension markup with spaces (single-pass)
///
/// This function collects all markup spans and replaces them with spaces
/// in a single pass, preserving string length for position-based operations.
pub fn mask_pymdown_markup(line: &str) -> String {
    // Collect all spans to mask
    let mut all_spans: Vec<(usize, usize)> = Vec::new();

    // Keys
    all_spans.extend(find_keys_spans(line));

    // Caret: insert and superscript
    if line.contains('^') {
        let insert_spans = find_insert_spans(line);
        let super_spans = find_single_delim_spans(line, '^', &insert_spans);
        all_spans.extend(insert_spans);
        all_spans.extend(super_spans);
    }

    // Tilde: strikethrough and subscript
    if line.contains('~') {
        let strike_spans = find_strikethrough_spans(line);
        let sub_spans = find_single_delim_spans(line, '~', &strike_spans);
        all_spans.extend(strike_spans);
        all_spans.extend(sub_spans);
    }

    // Mark
    all_spans.extend(find_mark_spans(line));

    // Early return if nothing to mask
    if all_spans.is_empty() {
        return line.to_string();
    }

    // Sort by start position and merge overlapping spans
    all_spans.sort_unstable_by_key(|&(start, _)| start);
    let merged = merge_spans(&all_spans);

    // Build result in single pass
    let mut result = String::with_capacity(line.len());
    let mut last_end = 0;

    for (start, end) in merged {
        result.push_str(&line[last_end..start]);
        // Use spaces to preserve length
        for _ in 0..(end - start) {
            result.push(' ');
        }
        last_end = end;
    }
    result.push_str(&line[last_end..]);

    result
}

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

    // =========================================================================
    // Core utility tests
    // =========================================================================

    #[test]
    fn test_position_in_spans_empty() {
        assert!(!position_in_spans(0, &[]));
        assert!(!position_in_spans(100, &[]));
    }

    #[test]
    fn test_position_in_spans_early_exit() {
        let spans = [(10, 20), (30, 40)];
        assert!(!position_in_spans(5, &spans)); // Before all spans
        assert!(!position_in_spans(25, &spans)); // Between spans
        assert!(!position_in_spans(50, &spans)); // After all spans
    }

    #[test]
    fn test_position_in_spans_inside() {
        let spans = [(10, 20), (30, 40)];
        assert!(position_in_spans(10, &spans)); // Start of first span
        assert!(position_in_spans(15, &spans)); // Middle of first span
        assert!(position_in_spans(19, &spans)); // End-1 of first span
        assert!(!position_in_spans(20, &spans)); // End of first span (exclusive)
        assert!(position_in_spans(30, &spans)); // Start of second span
    }

    #[test]
    fn test_merge_spans_empty() {
        assert!(merge_spans(&[]).is_empty());
    }

    #[test]
    fn test_merge_spans_no_overlap() {
        let spans = [(0, 5), (10, 15), (20, 25)];
        let merged = merge_spans(&spans);
        assert_eq!(merged, vec![(0, 5), (10, 15), (20, 25)]);
    }

    #[test]
    fn test_merge_spans_overlapping() {
        let spans = [(0, 10), (5, 15), (20, 25)];
        let merged = merge_spans(&spans);
        assert_eq!(merged, vec![(0, 15), (20, 25)]);
    }

    #[test]
    fn test_merge_spans_adjacent() {
        let spans = [(0, 10), (10, 20)];
        let merged = merge_spans(&spans);
        assert_eq!(merged, vec![(0, 20)]);
    }

    // =========================================================================
    // InlineHilite tests
    // =========================================================================

    #[test]
    fn test_contains_inline_hilite() {
        assert!(contains_inline_hilite("`#!python print('hello')`"));
        assert!(contains_inline_hilite("Use `#!js alert('hi')` for alerts"));
        assert!(contains_inline_hilite("`#!c++ cout << x;`"));

        assert!(!contains_inline_hilite("`regular code`"));
        assert!(!contains_inline_hilite("#! not in backticks"));
        assert!(!contains_inline_hilite("`#!` empty"));
    }

    #[test]
    fn test_is_inline_hilite_content() {
        assert!(is_inline_hilite_content("#!python print()"));
        assert!(is_inline_hilite_content("#!js code"));

        assert!(!is_inline_hilite_content("regular code"));
        assert!(!is_inline_hilite_content(" #!python with space"));
    }

    // =========================================================================
    // Keys tests
    // =========================================================================

    #[test]
    fn test_contains_keys() {
        assert!(contains_keys("Press ++ctrl++ to continue"));
        assert!(contains_keys("++ctrl+alt+delete++"));
        assert!(contains_keys("Use ++cmd+shift+p++ for command palette"));

        assert!(!contains_keys("Use + for addition"));
        assert!(!contains_keys("a++ increment"));
        assert!(!contains_keys("++incomplete"));
    }

    #[test]
    fn test_find_keyboard_shortcuts() {
        let shortcuts = find_keyboard_shortcuts("Press ++ctrl+c++ then ++ctrl+v++");
        assert_eq!(shortcuts.len(), 2);
        assert_eq!(shortcuts[0].keys, vec!["ctrl", "c"]);
        assert_eq!(shortcuts[1].keys, vec!["ctrl", "v"]);

        let shortcuts = find_keyboard_shortcuts("++ctrl+alt+delete++");
        assert_eq!(shortcuts.len(), 1);
        assert_eq!(shortcuts[0].keys, vec!["ctrl", "alt", "delete"]);
    }

    #[test]
    fn test_is_in_keys() {
        let line = "Press ++ctrl++ here";
        assert!(!is_in_keys(line, 0)); // "P"
        assert!(!is_in_keys(line, 5)); // " "
        assert!(is_in_keys(line, 6)); // first +
        assert!(is_in_keys(line, 10)); // "r"
        assert!(is_in_keys(line, 13)); // last +
        assert!(!is_in_keys(line, 14)); // " "
    }

    // =========================================================================
    // Caret tests
    // =========================================================================

    #[test]
    fn test_contains_superscript() {
        assert!(contains_superscript("E=mc^2^"));
        assert!(contains_superscript("x^n^ power"));

        assert!(!contains_superscript("no caret here"));
        assert!(!contains_superscript("^^insert^^")); // double caret is insert
    }

    #[test]
    fn test_contains_insert() {
        assert!(contains_insert("^^inserted text^^"));
        assert!(contains_insert("Some ^^new^^ text"));

        assert!(!contains_insert("^superscript^"));
        assert!(!contains_insert("no markup"));
    }

    #[test]
    fn test_find_superscript_spans() {
        let spans = find_superscript_spans("E=mc^2^");
        assert_eq!(spans.len(), 1);
        assert_eq!(&"E=mc^2^"[spans[0].0..spans[0].1], "^2^");
    }

    #[test]
    fn test_superscript_not_inside_insert() {
        // ^x^ inside ^^text^^ should not be detected as superscript
        let line = "^^some^x^text^^";
        let spans = find_superscript_spans(line);
        assert!(spans.is_empty(), "Superscript inside insert should not be detected");
    }

    #[test]
    fn test_is_in_caret_markup() {
        let line = "Text ^super^ here";
        assert!(!is_in_caret_markup(line, 0));
        assert!(is_in_caret_markup(line, 5)); // "^"
        assert!(is_in_caret_markup(line, 8)); // "p"
        assert!(!is_in_caret_markup(line, 13)); // " "

        let line2 = "Text ^^insert^^ here";
        assert!(is_in_caret_markup(line2, 5)); // first ^
        assert!(is_in_caret_markup(line2, 10)); // "e"
    }

    // =========================================================================
    // Tilde tests
    // =========================================================================

    #[test]
    fn test_contains_subscript() {
        assert!(contains_subscript("H~2~O"));
        assert!(contains_subscript("x~n~ power"));

        assert!(!contains_subscript("no tilde here"));
        assert!(!contains_subscript("~~strikethrough~~"));
    }

    #[test]
    fn test_contains_strikethrough() {
        assert!(contains_strikethrough("~~deleted text~~"));
        assert!(contains_strikethrough("Some ~~old~~ text"));
        assert!(contains_strikethrough("~~a~b~~")); // single tilde inside is OK

        assert!(!contains_strikethrough("~subscript~"));
        assert!(!contains_strikethrough("no markup"));
    }

    #[test]
    fn test_find_subscript_spans() {
        let spans = find_subscript_spans("H~2~O");
        assert_eq!(spans.len(), 1);
        assert_eq!(&"H~2~O"[spans[0].0..spans[0].1], "~2~");
    }

    #[test]
    fn test_subscript_not_inside_strikethrough() {
        let line = "~~some~x~text~~";
        let spans = find_subscript_spans(line);
        assert!(
            spans.is_empty(),
            "Subscript inside strikethrough should not be detected"
        );
    }

    #[test]
    fn test_multiple_subscripts() {
        let line = "~a~ and ~b~";
        let spans = find_subscript_spans(line);
        assert_eq!(spans.len(), 2);
        assert_eq!(&line[spans[0].0..spans[0].1], "~a~");
        assert_eq!(&line[spans[1].0..spans[1].1], "~b~");
    }

    #[test]
    fn test_subscript_no_whitespace() {
        let line = "~no spaces allowed~";
        let spans = find_subscript_spans(line);
        assert!(spans.is_empty(), "Subscript with whitespace should not match");
    }

    #[test]
    fn test_is_in_tilde_markup() {
        let line = "Text ~sub~ here";
        assert!(!is_in_tilde_markup(line, 0));
        assert!(is_in_tilde_markup(line, 5)); // "~"
        assert!(is_in_tilde_markup(line, 7)); // "u"
        assert!(!is_in_tilde_markup(line, 12)); // " "

        let line2 = "Text ~~strike~~ here";
        assert!(is_in_tilde_markup(line2, 5)); // first ~
        assert!(is_in_tilde_markup(line2, 10)); // "i"
    }

    #[test]
    fn test_subscript_vs_strikethrough_coexist() {
        let line = "H~2~O is ~~not~~ water";
        assert!(contains_subscript(line));
        assert!(contains_strikethrough(line));
    }

    #[test]
    fn test_strikethrough_with_internal_tilde() {
        // ~~a~b~~ should match as one strikethrough, not as strikethrough + subscript
        let line = "~~a~b~~";
        assert!(contains_strikethrough(line));

        let strike_spans = find_strikethrough_spans(line);
        assert_eq!(strike_spans.len(), 1);
        assert_eq!(&line[strike_spans[0].0..strike_spans[0].1], "~~a~b~~");

        // No subscript should be found
        assert!(!contains_subscript(line));
    }

    // =========================================================================
    // Mark tests
    // =========================================================================

    #[test]
    fn test_contains_mark() {
        assert!(contains_mark("This is ==highlighted== text"));
        assert!(contains_mark("==important=="));

        assert!(!contains_mark("no highlight"));
        assert!(!contains_mark("a == b comparison")); // spaces
    }

    #[test]
    fn test_is_in_mark() {
        let line = "Text ==highlight== more";
        assert!(!is_in_mark(line, 0));
        assert!(is_in_mark(line, 5)); // first =
        assert!(is_in_mark(line, 10)); // "h"
        assert!(!is_in_mark(line, 19)); // " "
    }

    // =========================================================================
    // SmartSymbols tests
    // =========================================================================

    #[test]
    fn test_contains_smart_symbols() {
        assert!(contains_smart_symbols("Copyright (c) 2024"));
        assert!(contains_smart_symbols("This is (tm) trademarked"));
        assert!(contains_smart_symbols("Left arrow <- here"));
        assert!(contains_smart_symbols("Right arrow -> there"));
        assert!(contains_smart_symbols("Em dash --- here"));
        assert!(contains_smart_symbols("Fraction 1/2"));

        assert!(!contains_smart_symbols("No symbols here"));
        assert!(!contains_smart_symbols("(other) parentheses"));
    }

    #[test]
    fn test_is_in_smart_symbol() {
        let line = "Copyright (c) text";
        assert!(!is_in_smart_symbol(line, 0));
        assert!(is_in_smart_symbol(line, 10)); // "("
        assert!(is_in_smart_symbol(line, 11)); // "c"
        assert!(is_in_smart_symbol(line, 12)); // ")"
        assert!(!is_in_smart_symbol(line, 14)); // " "
    }

    // =========================================================================
    // Combined tests
    // =========================================================================

    #[test]
    fn test_is_in_pymdown_markup() {
        assert!(is_in_pymdown_markup("++ctrl++", 2));
        assert!(is_in_pymdown_markup("^super^", 1));
        assert!(is_in_pymdown_markup("~sub~", 1));
        assert!(is_in_pymdown_markup("~~strike~~", 2));
        assert!(is_in_pymdown_markup("==mark==", 2));
        assert!(is_in_pymdown_markup("(c)", 1));

        assert!(!is_in_pymdown_markup("plain text", 5));
    }

    #[test]
    fn test_mask_pymdown_markup() {
        let line = "Press ++ctrl++ and ^super^ with ==mark==";
        let masked = mask_pymdown_markup(line);
        assert!(!masked.contains("++"));
        assert!(!masked.contains("^super^"));
        assert!(!masked.contains("==mark=="));
        assert!(masked.contains("Press"));
        assert!(masked.contains("and"));
        assert!(masked.contains("with"));
        assert_eq!(masked.len(), line.len());
    }

    #[test]
    fn test_mask_pymdown_markup_with_tilde() {
        let line = "H~2~O is ~~deleted~~ water";
        let masked = mask_pymdown_markup(line);
        assert!(!masked.contains("~2~"));
        assert!(!masked.contains("~~deleted~~"));
        assert!(masked.contains("H"));
        assert!(masked.contains("O is"));
        assert!(masked.contains("water"));
        assert_eq!(masked.len(), line.len());
    }

    #[test]
    fn test_mask_preserves_unmasked_text() {
        let line = "plain text without markup";
        let masked = mask_pymdown_markup(line);
        assert_eq!(masked, line);
    }

    #[test]
    fn test_mask_complex_mixed_markup() {
        let line = "++ctrl++ ^2^ ~x~ ~~old~~ ==new==";
        let masked = mask_pymdown_markup(line);
        // All markup should be masked
        assert!(!masked.contains("++"));
        assert!(!masked.contains("^2^"));
        assert!(!masked.contains("~x~"));
        assert!(!masked.contains("~~old~~"));
        assert!(!masked.contains("==new=="));
        // Length preserved
        assert_eq!(masked.len(), line.len());
    }

    // =========================================================================
    // Edge case tests
    // =========================================================================

    #[test]
    fn test_empty_line() {
        assert!(!contains_keys(""));
        assert!(!contains_superscript(""));
        assert!(!contains_subscript(""));
        assert!(!contains_mark(""));
        assert_eq!(mask_pymdown_markup(""), "");
    }

    #[test]
    fn test_unclosed_delimiters() {
        assert!(!contains_superscript("^unclosed"));
        assert!(!contains_subscript("~unclosed"));
        assert!(!contains_mark("==unclosed"));
        assert!(!contains_keys("++unclosed"));
    }

    #[test]
    fn test_adjacent_markup() {
        let line = "^a^^b^";
        // This is: ^a^ followed by ^b^ (two superscripts)
        // Wait, that's not right. Let me trace:
        // Position 0: ^, check if double -> pos 1 is ^, YES -> skip both, i=2
        // Position 2: ^, check if double -> pos 3 is b, NO -> start superscript
        // Inner: b at pos 3, found_content=true
        // Inner: ^ at pos 4, check if double -> pos 5 is nothing, NO -> valid!
        // So we get one superscript: ^b^
        let spans = find_superscript_spans(line);
        // Actually wait, let me re-trace more carefully.
        // ^a^^b^
        // 012345
        // i=0: ch='^', is_double? chars[1]=a, NO. Start looking for close.
        //   j=1: 'a', found_content=true
        //   j=2: '^', is_double? chars[3]='^', YES. Break without adding span.
        // i=1: ch='a', not '^', continue
        // i=2: ch='^', is_double? chars[3]='^', YES. Skip, i becomes 3 after next()
        // Actually wait, after break from inner loop at j=2, outer while does next() which gives i=3
        // i=3: ch='^', is_double? chars[4]='b', NO. Start looking for close.
        //   j=4: 'b', found_content=true
        //   j=5: '^', is_double? no more chars, NO. Valid! Add span.
        // So we get ^b^ at positions 3-6
        assert_eq!(spans.len(), 1);
        assert_eq!(&line[spans[0].0..spans[0].1], "^b^");
    }

    #[test]
    fn test_triple_tilde() {
        // ~~~a~~~ should match ~~a~~ (strikethrough) with extra tildes as text
        let line = "~~~a~~~";
        let strike_spans = find_strikethrough_spans(line);
        // The regex ~~[^~]+(?:~[^~]+)*~~ on "~~~a~~~":
        // Try at pos 0: ~~ matches, then [^~]+ needs non-tilde at pos 2, which is ~. Fail.
        // Try at pos 1: ~~ matches (pos 1-2), [^~]+ matches 'a' at pos 3.
        // Then (?:~[^~]+)* tries at pos 4: ~ at 4, then [^~]+ needs non-tilde at 5, which is ~. Zero matches.
        // Then ~~ matches at pos 4-5.
        // So we get ~~a~~ at positions 1-6.
        assert_eq!(strike_spans.len(), 1);
        assert_eq!(&line[strike_spans[0].0..strike_spans[0].1], "~~a~~");
    }
}