rumdl 0.3.0

A fast Markdown linter and formatter written in Rust
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
//! Blockquote-related utilities for rumdl.
//!
//! Provides functions for working with blockquote-prefixed lines, including
//! calculating effective indentation within blockquote context.

/// Parsed blockquote prefix components.
///
/// This parser supports both compact (`>> text`) and spaced (`> > text`) nesting.
/// It preserves at most one optional space/tab after the final marker in `prefix`;
/// additional whitespace is left in `content`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParsedBlockquotePrefix<'a> {
    /// Leading indentation before the first marker.
    pub indent: &'a str,
    /// Prefix up to (and including) the optional single space/tab after final `>`.
    pub prefix: &'a str,
    /// Content after `prefix` (may begin with additional spaces/tabs).
    pub content: &'a str,
    /// Number of blockquote markers at the start of the line.
    pub nesting_level: usize,
    /// All whitespace after the final `>` (including the optional first space/tab).
    pub spaces_after_marker: &'a str,
}

#[inline]
fn is_space_or_tab(byte: u8) -> bool {
    byte == b' ' || byte == b'\t'
}

/// Parse a blockquote prefix from a line.
///
/// Returns `None` when the line does not start (after leading indentation) with
/// a blockquote marker.
#[inline]
pub fn parse_blockquote_prefix(line: &str) -> Option<ParsedBlockquotePrefix<'_>> {
    let bytes = line.as_bytes();
    let mut pos = 0;

    // Leading indentation before the first marker.
    while pos < bytes.len() && is_space_or_tab(bytes[pos]) {
        pos += 1;
    }
    let indent_end = pos;

    if pos >= bytes.len() || bytes[pos] != b'>' {
        return None;
    }

    let mut nesting_level = 0;
    let mut prefix_end = pos;
    let mut spaces_after_marker_start = pos;
    let mut spaces_after_marker_end = pos;

    loop {
        if pos >= bytes.len() || bytes[pos] != b'>' {
            break;
        }

        nesting_level += 1;
        pos += 1; // past '>'
        let marker_end = pos;

        // Include at most one optional space/tab in the preserved prefix.
        if pos < bytes.len() && is_space_or_tab(bytes[pos]) {
            pos += 1;
        }
        let content_start_candidate = pos;

        // Consume any additional whitespace.
        while pos < bytes.len() && is_space_or_tab(bytes[pos]) {
            pos += 1;
        }

        // If another marker follows, all consumed whitespace belongs to nesting.
        if pos < bytes.len() && bytes[pos] == b'>' {
            continue;
        }

        // Final marker.
        prefix_end = content_start_candidate;
        spaces_after_marker_start = marker_end;
        spaces_after_marker_end = pos;
        break;
    }

    Some(ParsedBlockquotePrefix {
        indent: &line[..indent_end],
        prefix: &line[..prefix_end],
        content: &line[prefix_end..],
        nesting_level,
        spaces_after_marker: &line[spaces_after_marker_start..spaces_after_marker_end],
    })
}

/// Calculate the effective indentation of a line within a blockquote context.
///
/// For lines inside blockquotes, the "raw" leading whitespace (before `>`) is always 0,
/// but the semantically meaningful indent is the whitespace *after* the blockquote markers.
///
/// # Arguments
///
/// * `line_content` - The full line content including any blockquote markers
/// * `expected_bq_level` - The blockquote nesting level to match (0 for no blockquote)
/// * `fallback_indent` - The indent to return if blockquote levels don't match or if
///   `expected_bq_level` is 0
///
/// # Returns
///
/// The effective indentation:
/// - If `expected_bq_level` is 0: returns `fallback_indent`
/// - If line's blockquote level matches `expected_bq_level`: returns indent after stripping markers
/// - If blockquote levels don't match: returns `fallback_indent`
///
/// # Examples
///
/// ```
/// use rumdl_lib::utils::blockquote::effective_indent_in_blockquote;
///
/// // Regular line (no blockquote context)
/// assert_eq!(effective_indent_in_blockquote("   text", 0, 3), 3);
///
/// // Blockquote with 2 spaces after marker (first space consumed as marker syntax)
/// assert_eq!(effective_indent_in_blockquote(">  text", 1, 0), 1);
///
/// // Nested blockquote with 3 spaces after last marker (first space consumed as marker syntax)
/// assert_eq!(effective_indent_in_blockquote("> >   text", 2, 0), 2);
///
/// // Mismatched blockquote level - returns fallback
/// assert_eq!(effective_indent_in_blockquote("> text", 2, 5), 5);
/// ```
pub fn effective_indent_in_blockquote(line_content: &str, expected_bq_level: usize, fallback_indent: usize) -> usize {
    if expected_bq_level == 0 {
        return fallback_indent;
    }

    // Count blockquote markers at the start of the line
    // Markers can be separated by whitespace: "> > text" or ">> text"
    let line_bq_level = line_content
        .chars()
        .take_while(|c| *c == '>' || c.is_whitespace())
        .filter(|&c| c == '>')
        .count();

    if line_bq_level != expected_bq_level {
        return fallback_indent;
    }

    // Strip blockquote markers and compute indent within the blockquote context
    let mut pos = 0;
    let mut found_markers = 0;
    for c in line_content.chars() {
        pos += c.len_utf8();
        if c == '>' {
            found_markers += 1;
            if found_markers == line_bq_level {
                // Skip optional space after final >
                if line_content.get(pos..pos + 1) == Some(" ") {
                    pos += 1;
                }
                break;
            }
        }
    }

    let after_bq = &line_content[pos..];
    after_bq.len() - after_bq.trim_start().len()
}

/// Count the number of blockquote markers (`>`) at the start of a line.
///
/// Handles both compact (`>>text`) and spaced (`> > text`) blockquote syntax.
fn count_blockquote_level(line_content: &str) -> usize {
    line_content
        .chars()
        .take_while(|c| *c == '>' || c.is_whitespace())
        .filter(|&c| c == '>')
        .count()
}

/// Extract the content after blockquote markers.
///
/// Returns the portion of the line after all blockquote markers and the
/// optional space following the last marker.
///
/// # Examples
///
/// ```
/// use rumdl_lib::utils::blockquote::content_after_blockquote;
///
/// assert_eq!(content_after_blockquote("> text", 1), "text");
/// assert_eq!(content_after_blockquote(">  indented", 1), " indented");
/// assert_eq!(content_after_blockquote("> > nested", 2), "nested");
/// assert_eq!(content_after_blockquote("no quote", 0), "no quote");
/// ```
pub fn content_after_blockquote(line_content: &str, expected_bq_level: usize) -> &str {
    if expected_bq_level == 0 {
        return line_content;
    }

    // First, verify the line has the expected blockquote level
    let actual_level = count_blockquote_level(line_content);
    if actual_level != expected_bq_level {
        return line_content;
    }

    let mut pos = 0;
    let mut found_markers = 0;
    for c in line_content.chars() {
        pos += c.len_utf8();
        if c == '>' {
            found_markers += 1;
            if found_markers == expected_bq_level {
                // Skip optional space after final >
                if line_content.get(pos..pos + 1) == Some(" ") {
                    pos += 1;
                }
                break;
            }
        }
    }

    &line_content[pos..]
}

/// Strip all blockquote markers from a line, returning the content after them.
///
/// Unlike [`content_after_blockquote`], this does not require knowing the
/// expected nesting level — it strips all `>` markers and their trailing spaces.
/// Returns the original line unchanged if it has no blockquote markers.
///
/// # Examples
///
/// ```
/// use rumdl_lib::utils::blockquote::strip_blockquote_prefix;
///
/// assert_eq!(strip_blockquote_prefix("> text"), "text");
/// assert_eq!(strip_blockquote_prefix("> > nested"), "nested");
/// assert_eq!(strip_blockquote_prefix(">>compact"), "compact");
/// assert_eq!(strip_blockquote_prefix("  > indented"), "indented");
/// assert_eq!(strip_blockquote_prefix("no quote"), "no quote");
/// ```
pub fn strip_blockquote_prefix(line: &str) -> &str {
    match parse_blockquote_prefix(line) {
        Some(parsed) => parsed.content,
        None => line,
    }
}

/// The columns of whitespace `line` starts with, a tab running to the next multiple of four.
fn indent_columns(line: &str) -> usize {
    let mut column = 0;
    for c in line.chars() {
        match c {
            ' ' => column += 1,
            '\t' => column += 4 - column % 4,
            _ => break,
        }
    }
    column
}

/// The length of the list marker `text` opens with (`-`, `+`, `*`, or up to nine digits
/// then `.` or `)`), when a space, a tab or the end of the text follows it.
fn list_marker_len(text: &str) -> Option<usize> {
    let token = text.split([' ', '\t']).next().unwrap_or("");
    let digits = token.trim_end_matches(['.', ')']);
    let is_marker = matches!(token, "-" | "+" | "*")
        || (token.len() == digits.len() + 1
            && (1..=9).contains(&digits.len())
            && digits.bytes().all(|b| b.is_ascii_digit()));
    is_marker.then_some(token.len())
}

/// The visual column of byte `offset` in `line`, a tab running to the next multiple of four.
fn column_at(line: &str, offset: usize) -> usize {
    line[..offset].chars().fold(0, |column, c| match c {
        '\t' => column + 4 - column % 4,
        _ => column + 1,
    })
}

/// For a line that opens list items from `from` on (the byte offset after any quote
/// markers), the byte offset where the text of a quote opened inside them begins
/// (`- > text`, `1. - > text`, after the marker and its optional space), or `None` when
/// the items' content is not a quote. A list marker followed by five or more columns of
/// whitespace holds indented code, and a quote marker written four columns past the
/// marker before it is text.
pub fn quote_text_start_in_items(line: &str, from: usize) -> Option<usize> {
    let mut offset = from;
    let mut opened_item = false;
    // The visual column of `offset`, carried forward so tabs are measured at their real
    // tab stops without recounting the line for every nested marker.
    let mut column = column_at(line, from);
    loop {
        let rest = &line[offset..];
        let text = rest.trim_start_matches([' ', '\t']);
        let text_column = rest[..rest.len() - text.len()].chars().fold(column, |column, c| {
            if c == '\t' { column + 4 - column % 4 } else { column + 1 }
        });
        // The first marker may be indented up to three columns; the content of an item
        // starts within four columns after its marker, or is code.
        if text_column - column >= if opened_item { 5 } else { 4 } {
            return None;
        }
        if let Some(marker_len) = list_marker_len(text).filter(|&len| text.len() > len) {
            offset += rest.len() - text.len() + marker_len;
            column = text_column + marker_len;
            opened_item = true;
            continue;
        }
        if !opened_item {
            return None;
        }
        let parsed = parse_blockquote_prefix(rest)?;
        if !quote_markers_are_real(rest) {
            return None;
        }
        return Some(line.len() - parsed.content.len());
    }
}

/// The content columns of the list items an item line opens when `line` carries no
/// chain from the container pass (an item opened in a quote): the item's own, then
/// each item written on the same line inside it (`> - - x`).
fn written_item_columns(line: &str, content_column: usize, bare: bool) -> Vec<usize> {
    let mut columns = vec![content_column + usize::from(bare)];
    if bare {
        return columns;
    }
    // One forward pass: the visual column is carried along instead of recounted from the
    // start of the line for every offset.
    let mut position = 0;
    let mut column = 0;
    let mut column_at_forward = |target: usize| -> usize {
        for c in line[position..target].chars() {
            column += if c == '\t' { 4 - column % 4 } else { 1 };
        }
        position = target;
        column
    };
    let Some(mut offset) = line
        .char_indices()
        .map(|(index, _)| index)
        .chain(std::iter::once(line.len()))
        .find(|&index| column_at_forward(index) >= content_column)
    else {
        return columns;
    };
    while let Some(marker_len) =
        list_marker_len(&line[offset..]).filter(|_| !crate::utils::thematic_break::is_thematic_break(&line[offset..]))
    {
        let after_marker = offset + marker_len;
        let spaces = line[after_marker..].len() - line[after_marker..].trim_start_matches([' ', '\t']).len();
        let marker_end = column_at_forward(after_marker);
        let rest_blank = line[after_marker + spaces..].trim().is_empty();
        let spaces_columns = column_at_forward(after_marker + spaces) - marker_end;
        if rest_blank || spaces_columns >= 5 {
            columns.push(marker_end + 1);
            break;
        }
        columns.push(marker_end + spaces_columns);
        offset = after_marker + spaces;
    }
    columns
}

/// Whether the quote markers of `line` (zero-based `line_num`) are text continuing the
/// paragraph above rather than markers: the line sits four columns or more past the edge
/// of the container it is written in (`edges`, from `container_edges`), straight under a
/// line of text.
pub fn quote_markers_are_paragraph_text(
    ctx: &crate::lint_context::LintContext,
    edges: &[usize],
    line_num: usize,
    line: &str,
) -> bool {
    line_num > 0 && !ctx.lines[line_num - 1].is_blank && indent_columns(line) >= edges[line_num] + 4
}

/// The column each line's container starts its content at, indexed by zero-based line:
/// zero outside a list, and the content column of the innermost list item still open at
/// the line inside one. An item stays open through blank lines and ends at the first line
/// written left of its content column, or at a sibling or outer item marker. For a line
/// that opens an item, the edge is that of the container the item is written in.
///
/// An item opened in a quote is measured against the text after the quote markers on
/// quoted lines, and ends at the first unquoted line.
pub fn container_edges(ctx: &crate::lint_context::LintContext) -> Vec<usize> {
    container_edges_with_margins(ctx)
        .into_iter()
        .map(|(edge, _)| edge)
        .collect()
}

/// `container_edges`, with for each line the width of the quote markers (and the optional
/// space after them) in front of the line that opened the innermost open item. The edge
/// less this margin is the item's content column measured from the text after the
/// markers, which is how a quoted line is compared when its markers are written with a
/// different width (`>> - x` then `> > y`).
///
/// The margin is zero, meaning the edge is not to be trusted past the quote markers, for
/// an item opened outside a quote or in front of a tab, for a line inside a quote nested
/// deeper than the item's (its markers already consume the item's indentation), and after
/// a quoted line that ended inner items while continuing a paragraph (a possible lazy
/// continuation).
pub fn container_edges_with_margins(ctx: &crate::lint_context::LintContext) -> Vec<(usize, usize)> {
    // Open items, outermost first: content column, whether the item opened in a quote
    // the width of the quote markers in front of it and how many quotes deep it opened.
    let mut open: Vec<(usize, bool, usize, usize)> = Vec::new();
    let mut edges = Vec::with_capacity(ctx.lines.len());
    // Set when a quoted line ended inner items while continuing a paragraph: it may be a lazy
    // continuation of one of them, so the surviving edge no longer says where later lines belong.
    let mut lazy_taint = false;
    for (index, info) in ctx.lines.iter().enumerate() {
        let depth = info.blockquote.as_ref().map_or(0, |quote| quote.nesting_level);
        if !info.is_blank {
            let line = info.content(ctx.content);
            let quoted = info.blockquote.is_some();
            let margin = match parse_blockquote_prefix(line) {
                // A tab on the opener line makes its columns depend on the tab stop, so the
                // margin is not known and the item is not measured past its quote.
                Some(parsed) if quoted && !line.contains('\t') => column_at(line, parsed.prefix.len()),
                _ => 0,
            };
            // The column a line is measured at against an open item: past the quote
            // markers when both the item and the line are quoted.
            let measured = |opened_quoted: bool,
                            opened_margin: usize,
                            opened_depth: usize,
                            content: usize,
                            column: usize,
                            raw_column: usize|
             -> bool {
                if opened_quoted && quoted {
                    // Leaving the quote an item was opened in ends the item.
                    depth < opened_depth || content.saturating_sub(opened_margin) > column.saturating_sub(margin)
                } else {
                    content > raw_column
                }
            };
            match info.list_item.as_deref() {
                Some(item) => {
                    while open.last().is_some_and(|&(content, oq, om, od)| {
                        measured(oq, om, od, content, item.marker_column, item.marker_column)
                    }) {
                        open.pop();
                    }
                    edges.push(open.last().map_or((0, 0), |&(content, _, om, od)| {
                        (content, if depth > od || lazy_taint { 0 } else { om })
                    }));
                    // An item with nothing after its marker takes its content from the
                    // lines below, which have to clear the marker and the space after it.
                    let bare = line.trim_end().chars().count() <= item.content_column;
                    let columns = if item.commonmark_items.is_empty() {
                        written_item_columns(line, item.content_column, bare)
                    } else {
                        item.commonmark_items.iter().map(|item| item.content_column).collect()
                    };
                    // An item inside an unquoted item is compared by raw indentation: its quote
                    // markers sit in the outer item's content, so margins do not line up.
                    let relative = quoted && open.iter().all(|&(_, oq, _, _)| oq);
                    open.extend(columns.into_iter().map(|content| (content, relative, margin, depth)));
                    lazy_taint = false;
                    continue;
                }
                None => {
                    let column = match parse_blockquote_prefix(line) {
                        Some(parsed) if quoted => column_at(line, parsed.prefix.len()) + indent_columns(parsed.content),
                        _ => indent_columns(line),
                    };
                    let before = open.len();
                    while open.last().is_some_and(|&(content, oq, om, od)| {
                        measured(oq, om, od, content, column, indent_columns(line))
                    }) {
                        open.pop();
                    }
                    if index > 0 && !ctx.lines[index - 1].is_blank && open.len() < before {
                        lazy_taint = true;
                    }
                    if open.is_empty() {
                        lazy_taint = false;
                    }
                }
            }
        }
        edges.push(open.last().map_or((0, 0), |&(content, _, om, od)| {
            (content, if depth > od || lazy_taint { 0 } else { om })
        }));
    }
    edges
}

/// Whether the unquoted `line` at zero-based `line_num` is indented four columns or more
/// past the edge of its container (`container_edges`), which makes it indented code rather
/// than a heading. `10. x` followed by a heading at column 6 is still a heading.
pub fn is_indented_past_edge(edges: &[usize], line_num: usize, line: &str) -> bool {
    indent_columns(line) >= edges[line_num] + 4
}

/// Whether every quote marker opening `line` opens a quote: a marker written
/// four columns or more past the optional space after the one before it is text
/// of the quote around it. Tabs advance to the next multiple of four.
pub fn quote_markers_are_real(line: &str) -> bool {
    let mut column = 0;
    // Column just past the previous marker, once a marker has been seen.
    let mut after_marker: Option<usize> = None;
    for c in line.chars() {
        match c {
            ' ' => column += 1,
            '\t' => column += 4 - column % 4,
            '>' => {
                if after_marker.is_some_and(|end| column >= end + 1 + 4) {
                    return false;
                }
                column += 1;
                after_marker = Some(column);
            }
            _ => break,
        }
    }
    true
}

/// The length of the quote markers opening `line`, up to `depth` of them, with
/// the one optional space or tab after the last. Markers past `depth` are text
/// of the heading (`>     > x` in a setext paragraph), not quotes. `None` for a
/// line without any marker.
pub fn quote_margin_len(line: &str, depth: usize) -> Option<usize> {
    let mut pos = 0;
    let mut markers = 0;
    while markers < depth {
        let rest = &line[pos..];
        let next = rest.trim_start_matches([' ', '\t']);
        if !next.starts_with('>') {
            break;
        }
        pos += rest.len() - next.len() + 1;
        markers += 1;
    }
    if markers == 0 {
        return None;
    }
    Some(pos + usize::from(line[pos..].starts_with([' ', '\t'])))
}

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

    // ==========================================================================
    // parse_blockquote_prefix tests
    // ==========================================================================

    #[test]
    fn test_parse_blockquote_prefix_compact_nested() {
        let parsed = parse_blockquote_prefix(">> text").expect("should parse compact nested blockquote");
        assert_eq!(parsed.indent, "");
        assert_eq!(parsed.prefix, ">> ");
        assert_eq!(parsed.content, "text");
        assert_eq!(parsed.nesting_level, 2);
        assert_eq!(parsed.spaces_after_marker, " ");
    }

    #[test]
    fn test_parse_blockquote_prefix_spaced_nested() {
        let parsed = parse_blockquote_prefix("> >  text").expect("should parse spaced nested blockquote");
        assert_eq!(parsed.indent, "");
        assert_eq!(parsed.prefix, "> > ");
        assert_eq!(parsed.content, " text");
        assert_eq!(parsed.nesting_level, 2);
        assert_eq!(parsed.spaces_after_marker, "  ");
    }

    #[test]
    fn test_parse_blockquote_prefix_with_indent() {
        let parsed = parse_blockquote_prefix("   > quote").expect("should parse indented blockquote");
        assert_eq!(parsed.indent, "   ");
        assert_eq!(parsed.prefix, "   > ");
        assert_eq!(parsed.content, "quote");
        assert_eq!(parsed.nesting_level, 1);
        assert_eq!(parsed.spaces_after_marker, " ");
    }

    #[test]
    fn test_parse_blockquote_prefix_non_blockquote() {
        assert!(parse_blockquote_prefix("plain text").is_none());
        assert!(parse_blockquote_prefix("  plain text").is_none());
    }

    // ==========================================================================
    // effective_indent_in_blockquote tests
    // ==========================================================================

    #[test]
    fn test_effective_indent_no_blockquote_context() {
        // When expected_bq_level is 0, always return fallback
        assert_eq!(effective_indent_in_blockquote("text", 0, 0), 0);
        assert_eq!(effective_indent_in_blockquote("   text", 0, 3), 3);
        assert_eq!(effective_indent_in_blockquote("> text", 0, 5), 5);
    }

    #[test]
    fn test_effective_indent_single_level_blockquote() {
        // Single > with various indents after
        assert_eq!(effective_indent_in_blockquote("> text", 1, 99), 0);
        assert_eq!(effective_indent_in_blockquote(">  text", 1, 99), 1);
        assert_eq!(effective_indent_in_blockquote(">   text", 1, 99), 2);
        assert_eq!(effective_indent_in_blockquote(">    text", 1, 99), 3);
    }

    #[test]
    fn test_effective_indent_no_space_after_marker() {
        // >text (no space after >) - should have 0 effective indent
        assert_eq!(effective_indent_in_blockquote(">text", 1, 99), 0);
        assert_eq!(effective_indent_in_blockquote(">>text", 2, 99), 0);
    }

    #[test]
    fn test_effective_indent_nested_blockquote_compact() {
        // Compact nested: >>text, >> text, >>  text
        assert_eq!(effective_indent_in_blockquote(">> text", 2, 99), 0);
        assert_eq!(effective_indent_in_blockquote(">>  text", 2, 99), 1);
        assert_eq!(effective_indent_in_blockquote(">>   text", 2, 99), 2);
    }

    #[test]
    fn test_effective_indent_nested_blockquote_spaced() {
        // Spaced nested: > > text, > >  text
        assert_eq!(effective_indent_in_blockquote("> > text", 2, 99), 0);
        assert_eq!(effective_indent_in_blockquote("> >  text", 2, 99), 1);
        assert_eq!(effective_indent_in_blockquote("> >   text", 2, 99), 2);
    }

    #[test]
    fn test_effective_indent_mismatched_level() {
        // Line has different blockquote level than expected - return fallback
        assert_eq!(effective_indent_in_blockquote("> text", 2, 42), 42);
        assert_eq!(effective_indent_in_blockquote(">> text", 1, 42), 42);
        assert_eq!(effective_indent_in_blockquote("text", 1, 42), 42);
    }

    #[test]
    fn test_effective_indent_empty_blockquote() {
        // Empty blockquote lines
        assert_eq!(effective_indent_in_blockquote(">", 1, 99), 0);
        assert_eq!(effective_indent_in_blockquote("> ", 1, 99), 0);
        assert_eq!(effective_indent_in_blockquote(">  ", 1, 99), 1);
    }

    #[test]
    fn test_effective_indent_issue_268_case() {
        // The exact pattern from issue #268:
        // ">   text" where we expect 2 spaces of indent (list continuation)
        assert_eq!(effective_indent_in_blockquote(">   Opening the app", 1, 0), 2);
        assert_eq!(
            effective_indent_in_blockquote(">   [**See preview here!**](https://example.com)", 1, 0),
            2
        );
    }

    #[test]
    fn test_effective_indent_triple_nested() {
        // Triple nested blockquotes
        assert_eq!(effective_indent_in_blockquote("> > > text", 3, 99), 0);
        assert_eq!(effective_indent_in_blockquote("> > >  text", 3, 99), 1);
        assert_eq!(effective_indent_in_blockquote(">>> text", 3, 99), 0);
        assert_eq!(effective_indent_in_blockquote(">>>  text", 3, 99), 1);
    }

    // ==========================================================================
    // count_blockquote_level tests
    // ==========================================================================

    #[test]
    fn test_count_blockquote_level_none() {
        assert_eq!(count_blockquote_level("regular text"), 0);
        assert_eq!(count_blockquote_level("   indented text"), 0);
        assert_eq!(count_blockquote_level(""), 0);
    }

    #[test]
    fn test_count_blockquote_level_single() {
        assert_eq!(count_blockquote_level("> text"), 1);
        assert_eq!(count_blockquote_level(">text"), 1);
        assert_eq!(count_blockquote_level(">"), 1);
    }

    #[test]
    fn test_count_blockquote_level_nested() {
        assert_eq!(count_blockquote_level(">> text"), 2);
        assert_eq!(count_blockquote_level("> > text"), 2);
        assert_eq!(count_blockquote_level(">>> text"), 3);
        assert_eq!(count_blockquote_level("> > > text"), 3);
    }

    // ==========================================================================
    // content_after_blockquote tests
    // ==========================================================================

    #[test]
    fn test_content_after_blockquote_no_quote() {
        assert_eq!(content_after_blockquote("text", 0), "text");
        assert_eq!(content_after_blockquote("   indented", 0), "   indented");
    }

    #[test]
    fn test_content_after_blockquote_single() {
        assert_eq!(content_after_blockquote("> text", 1), "text");
        assert_eq!(content_after_blockquote(">text", 1), "text");
        assert_eq!(content_after_blockquote(">  indented", 1), " indented");
    }

    #[test]
    fn test_content_after_blockquote_nested() {
        assert_eq!(content_after_blockquote(">> text", 2), "text");
        assert_eq!(content_after_blockquote("> > text", 2), "text");
        assert_eq!(content_after_blockquote("> >  indented", 2), " indented");
    }

    #[test]
    fn test_content_after_blockquote_mismatched_level() {
        // If level doesn't match, return original
        assert_eq!(content_after_blockquote("> text", 2), "> text");
        assert_eq!(content_after_blockquote(">> text", 1), ">> text");
    }
}