rumdl_lib/rules/
md027_multiple_spaces_blockquote.rs

1use crate::utils::range_utils::calculate_match_range;
2
3use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, Severity};
4use regex::Regex;
5use std::sync::LazyLock;
6
7// New patterns for detecting malformed blockquote attempts where user intent is clear
8static MALFORMED_BLOCKQUOTE_PATTERNS: LazyLock<Vec<(Regex, &'static str)>> = LazyLock::new(|| {
9    vec![
10        // Double > without space: >>text (looks like nested but missing spaces)
11        (
12            Regex::new(r"^(\s*)>>([^\s>].*|$)").unwrap(),
13            "missing spaces in nested blockquote",
14        ),
15        // Triple > without space: >>>text
16        (
17            Regex::new(r"^(\s*)>>>([^\s>].*|$)").unwrap(),
18            "missing spaces in deeply nested blockquote",
19        ),
20        // Space then > then text: > >text (extra > by mistake)
21        (
22            Regex::new(r"^(\s*)>\s+>([^\s>].*|$)").unwrap(),
23            "extra blockquote marker",
24        ),
25        // Multiple spaces then >: (spaces)>text (indented blockquote without space)
26        (
27            Regex::new(r"^(\s{4,})>([^\s].*|$)").unwrap(),
28            "indented blockquote missing space",
29        ),
30    ]
31});
32
33// Cached regex for blockquote validation
34static BLOCKQUOTE_PATTERN: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^\s*>").unwrap());
35
36/// Rule MD027: No multiple spaces after blockquote symbol
37///
38/// See [docs/md027.md](../../docs/md027.md) for full documentation, configuration, and examples.
39
40#[derive(Debug, Default, Clone)]
41pub struct MD027MultipleSpacesBlockquote;
42
43impl Rule for MD027MultipleSpacesBlockquote {
44    fn name(&self) -> &'static str {
45        "MD027"
46    }
47
48    fn description(&self) -> &'static str {
49        "Multiple spaces after quote marker (>)"
50    }
51
52    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
53        let mut warnings = Vec::new();
54
55        for (line_idx, line_info) in ctx.lines.iter().enumerate() {
56            let line_num = line_idx + 1;
57
58            // Skip lines in code blocks and HTML blocks
59            if line_info.in_code_block || line_info.in_html_block {
60                continue;
61            }
62
63            // Check if this line is a blockquote using cached info
64            if let Some(blockquote) = &line_info.blockquote {
65                // Part 1: Check for multiple spaces after the blockquote marker
66                // Skip if line is in a list block - extra spaces may be list continuation indent
67                // Also skip if previous line in same blockquote context had a list item
68                // (covers cases where list block detection doesn't catch all continuation lines)
69                let is_likely_list_continuation =
70                    ctx.is_in_list_block(line_num) || self.previous_blockquote_line_had_list(ctx, line_idx);
71                if blockquote.has_multiple_spaces_after_marker && !is_likely_list_continuation {
72                    // Find where the extra spaces start in the line
73                    // We need to find the position after the markers and first space/tab
74                    let mut byte_pos = 0;
75                    let mut found_markers = 0;
76                    let mut found_first_space = false;
77
78                    for (i, ch) in line_info.content(ctx.content).char_indices() {
79                        if found_markers < blockquote.nesting_level {
80                            if ch == '>' {
81                                found_markers += 1;
82                            }
83                        } else if !found_first_space && (ch == ' ' || ch == '\t') {
84                            // This is the first space/tab after markers
85                            found_first_space = true;
86                        } else if found_first_space && (ch == ' ' || ch == '\t') {
87                            // This is where extra spaces start
88                            byte_pos = i;
89                            break;
90                        }
91                    }
92
93                    // Count how many extra spaces/tabs there are
94                    let extra_spaces_bytes = line_info.content(ctx.content)[byte_pos..]
95                        .chars()
96                        .take_while(|&c| c == ' ' || c == '\t')
97                        .fold(0, |acc, ch| acc + ch.len_utf8());
98
99                    if extra_spaces_bytes > 0 {
100                        let (start_line, start_col, end_line, end_col) = calculate_match_range(
101                            line_num,
102                            line_info.content(ctx.content),
103                            byte_pos,
104                            extra_spaces_bytes,
105                        );
106
107                        warnings.push(LintWarning {
108                            rule_name: Some(self.name().to_string()),
109                            line: start_line,
110                            column: start_col,
111                            end_line,
112                            end_column: end_col,
113                            message: "Multiple spaces after quote marker (>)".to_string(),
114                            severity: Severity::Warning,
115                            fix: Some(Fix {
116                                range: {
117                                    let start_byte = ctx.line_index.line_col_to_byte_range(line_num, start_col).start;
118                                    let end_byte = ctx.line_index.line_col_to_byte_range(line_num, end_col).start;
119                                    start_byte..end_byte
120                                },
121                                replacement: "".to_string(), // Remove the extra spaces
122                            }),
123                        });
124                    }
125                }
126            } else {
127                // Part 2: Check for malformed blockquote attempts on non-blockquote lines
128                let malformed_attempts = self.detect_malformed_blockquote_attempts(line_info.content(ctx.content));
129                for (start, len, fixed_line, description) in malformed_attempts {
130                    let (start_line, start_col, end_line, end_col) =
131                        calculate_match_range(line_num, line_info.content(ctx.content), start, len);
132
133                    warnings.push(LintWarning {
134                        rule_name: Some(self.name().to_string()),
135                        line: start_line,
136                        column: start_col,
137                        end_line,
138                        end_column: end_col,
139                        message: format!("Malformed quote: {description}"),
140                        severity: Severity::Warning,
141                        fix: Some(Fix {
142                            range: ctx.line_index.line_col_to_byte_range(line_num, 1),
143                            replacement: fixed_line,
144                        }),
145                    });
146                }
147            }
148        }
149
150        Ok(warnings)
151    }
152
153    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
154        let mut result = Vec::with_capacity(ctx.lines.len());
155
156        for (line_idx, line_info) in ctx.lines.iter().enumerate() {
157            let line_num = line_idx + 1;
158            if let Some(blockquote) = &line_info.blockquote {
159                // Fix blockquotes with multiple spaces after the marker
160                // Skip if line is in a list block - extra spaces are list continuation indent
161                let is_likely_list_continuation =
162                    ctx.is_in_list_block(line_num) || self.previous_blockquote_line_had_list(ctx, line_idx);
163                if blockquote.has_multiple_spaces_after_marker && !is_likely_list_continuation {
164                    // Rebuild the line with exactly one space after the markers
165                    // But don't add a space if the content is empty to avoid MD009 conflicts
166                    let fixed_line = if blockquote.content.is_empty() {
167                        format!("{}{}", blockquote.indent, ">".repeat(blockquote.nesting_level))
168                    } else {
169                        format!(
170                            "{}{} {}",
171                            blockquote.indent,
172                            ">".repeat(blockquote.nesting_level),
173                            blockquote.content
174                        )
175                    };
176                    result.push(fixed_line);
177                } else {
178                    result.push(line_info.content(ctx.content).to_string());
179                }
180            } else {
181                // Check for malformed blockquote attempts
182                let malformed_attempts = self.detect_malformed_blockquote_attempts(line_info.content(ctx.content));
183                if !malformed_attempts.is_empty() {
184                    // Use the first fix (there should only be one per line)
185                    let (_, _, fixed_line, _) = &malformed_attempts[0];
186                    result.push(fixed_line.clone());
187                } else {
188                    result.push(line_info.content(ctx.content).to_string());
189                }
190            }
191        }
192
193        // Preserve trailing newline if original content had one
194        Ok(result.join("\n") + if ctx.content.ends_with('\n') { "\n" } else { "" })
195    }
196
197    fn as_any(&self) -> &dyn std::any::Any {
198        self
199    }
200
201    fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
202    where
203        Self: Sized,
204    {
205        Box::new(MD027MultipleSpacesBlockquote)
206    }
207
208    /// Check if this rule should be skipped
209    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
210        ctx.content.is_empty() || !ctx.likely_has_blockquotes()
211    }
212}
213
214impl MD027MultipleSpacesBlockquote {
215    /// Check if a previous line in the same blockquote context had a list item
216    /// This helps identify list continuation lines even when list block detection
217    /// doesn't catch all continuation lines
218    fn previous_blockquote_line_had_list(&self, ctx: &crate::lint_context::LintContext, line_idx: usize) -> bool {
219        // Look backwards for a blockquote line with a list item
220        // Stop when we hit a non-blockquote line or find a list item
221        for prev_idx in (0..line_idx).rev() {
222            let prev_line = &ctx.lines[prev_idx];
223
224            // If previous line is not a blockquote, stop searching
225            if prev_line.blockquote.is_none() {
226                return false;
227            }
228
229            // If previous line has a list item, this could be list continuation
230            if prev_line.list_item.is_some() {
231                return true;
232            }
233
234            // If it's in a list block, that's also good enough
235            if ctx.is_in_list_block(prev_idx + 1) {
236                return true;
237            }
238        }
239        false
240    }
241
242    /// Detect malformed blockquote attempts where user intent is clear
243    fn detect_malformed_blockquote_attempts(&self, line: &str) -> Vec<(usize, usize, String, String)> {
244        let mut results = Vec::new();
245
246        for (pattern, issue_type) in MALFORMED_BLOCKQUOTE_PATTERNS.iter() {
247            if let Some(cap) = pattern.captures(line) {
248                let match_obj = cap.get(0).unwrap();
249                let start = match_obj.start();
250                let len = match_obj.len();
251
252                // Extract potential blockquote components
253                if let Some((fixed_line, description)) = self.extract_blockquote_fix_from_match(&cap, issue_type, line)
254                {
255                    // Only proceed if this looks like a genuine blockquote attempt
256                    if self.looks_like_blockquote_attempt(line, &fixed_line) {
257                        results.push((start, len, fixed_line, description));
258                    }
259                }
260            }
261        }
262
263        results
264    }
265
266    /// Extract the proper blockquote format from a malformed match
267    fn extract_blockquote_fix_from_match(
268        &self,
269        cap: &regex::Captures,
270        issue_type: &str,
271        _original_line: &str,
272    ) -> Option<(String, String)> {
273        match issue_type {
274            "missing spaces in nested blockquote" => {
275                // >>text -> > > text
276                let indent = cap.get(1).map_or("", |m| m.as_str());
277                let content = cap.get(2).map_or("", |m| m.as_str());
278                Some((
279                    format!("{}> > {}", indent, content.trim()),
280                    "Missing spaces in nested blockquote".to_string(),
281                ))
282            }
283            "missing spaces in deeply nested blockquote" => {
284                // >>>text -> > > > text
285                let indent = cap.get(1).map_or("", |m| m.as_str());
286                let content = cap.get(2).map_or("", |m| m.as_str());
287                Some((
288                    format!("{}> > > {}", indent, content.trim()),
289                    "Missing spaces in deeply nested blockquote".to_string(),
290                ))
291            }
292            "extra blockquote marker" => {
293                // > >text -> > text
294                let indent = cap.get(1).map_or("", |m| m.as_str());
295                let content = cap.get(2).map_or("", |m| m.as_str());
296                Some((
297                    format!("{}> {}", indent, content.trim()),
298                    "Extra blockquote marker".to_string(),
299                ))
300            }
301            "indented blockquote missing space" => {
302                // (spaces)>text -> (spaces)> text
303                let indent = cap.get(1).map_or("", |m| m.as_str());
304                let content = cap.get(2).map_or("", |m| m.as_str());
305                Some((
306                    format!("{}> {}", indent, content.trim()),
307                    "Indented blockquote missing space".to_string(),
308                ))
309            }
310            _ => None,
311        }
312    }
313
314    /// Check if the pattern looks like a genuine blockquote attempt
315    fn looks_like_blockquote_attempt(&self, original: &str, fixed: &str) -> bool {
316        // Basic heuristics to avoid false positives
317
318        // 1. Content should not be too short (avoid flagging things like ">>>" alone)
319        let trimmed_original = original.trim();
320        if trimmed_original.len() < 5 {
321            // More restrictive
322            return false;
323        }
324
325        // 2. Should contain some text content after the markers
326        let content_after_markers = trimmed_original.trim_start_matches('>').trim_start_matches(' ');
327        if content_after_markers.is_empty() || content_after_markers.len() < 3 {
328            // More restrictive
329            return false;
330        }
331
332        // 3. Content should contain some alphabetic characters (not just symbols)
333        if !content_after_markers.chars().any(|c| c.is_alphabetic()) {
334            return false;
335        }
336
337        // 4. Fixed version should actually be a valid blockquote
338        // Check if it starts with optional whitespace followed by >
339        if !BLOCKQUOTE_PATTERN.is_match(fixed) {
340            return false;
341        }
342
343        // 5. Avoid flagging things that might be code or special syntax
344        if content_after_markers.starts_with('#') // Headers
345            || content_after_markers.starts_with('[') // Links
346            || content_after_markers.starts_with('`') // Code
347            || content_after_markers.starts_with("http") // URLs
348            || content_after_markers.starts_with("www.") // URLs
349            || content_after_markers.starts_with("ftp")
350        // URLs
351        {
352            return false;
353        }
354
355        // 6. Content should look like prose, not code or markup
356        let word_count = content_after_markers.split_whitespace().count();
357        if word_count < 3 {
358            // Should be at least 3 words to look like prose
359            return false;
360        }
361
362        true
363    }
364}
365
366#[cfg(test)]
367mod tests {
368    use super::*;
369    use crate::lint_context::LintContext;
370
371    #[test]
372    fn test_valid_blockquote() {
373        let rule = MD027MultipleSpacesBlockquote;
374        let content = "> This is a blockquote\n> > Nested quote";
375        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
376        let result = rule.check(&ctx).unwrap();
377        assert!(result.is_empty(), "Valid blockquotes should not be flagged");
378    }
379
380    #[test]
381    fn test_multiple_spaces_after_marker() {
382        let rule = MD027MultipleSpacesBlockquote;
383        let content = ">  This has two spaces\n>   This has three spaces";
384        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
385        let result = rule.check(&ctx).unwrap();
386        assert_eq!(result.len(), 2);
387        assert_eq!(result[0].line, 1);
388        assert_eq!(result[0].column, 3); // Points to the extra space (after > and first space)
389        assert_eq!(result[0].message, "Multiple spaces after quote marker (>)");
390        assert_eq!(result[1].line, 2);
391        assert_eq!(result[1].column, 3);
392    }
393
394    #[test]
395    fn test_nested_multiple_spaces() {
396        let rule = MD027MultipleSpacesBlockquote;
397        // LintContext sees these as single-level blockquotes because of the space between markers
398        let content = ">  Two spaces after marker\n>>  Two spaces in nested blockquote";
399        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
400        let result = rule.check(&ctx).unwrap();
401        assert_eq!(result.len(), 2);
402        assert!(result[0].message.contains("Multiple spaces"));
403        assert!(result[1].message.contains("Multiple spaces"));
404    }
405
406    #[test]
407    fn test_malformed_nested_quote() {
408        let rule = MD027MultipleSpacesBlockquote;
409        // LintContext sees >>text as a valid nested blockquote with no space after marker
410        // MD027 doesn't flag this as malformed, only as missing space after marker
411        let content = ">>This is a nested blockquote without space after markers";
412        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
413        let result = rule.check(&ctx).unwrap();
414        // This should not be flagged at all since >>text is valid CommonMark
415        assert_eq!(result.len(), 0);
416    }
417
418    #[test]
419    fn test_malformed_deeply_nested() {
420        let rule = MD027MultipleSpacesBlockquote;
421        // LintContext sees >>>text as a valid triple-nested blockquote
422        let content = ">>>This is deeply nested without spaces after markers";
423        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
424        let result = rule.check(&ctx).unwrap();
425        // This should not be flagged - >>>text is valid CommonMark
426        assert_eq!(result.len(), 0);
427    }
428
429    #[test]
430    fn test_extra_quote_marker() {
431        let rule = MD027MultipleSpacesBlockquote;
432        // "> >text" is parsed as single-level blockquote with ">text" as content
433        // This is valid CommonMark and not detected as malformed
434        let content = "> >This looks like nested but is actually single level with >This as content";
435        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
436        let result = rule.check(&ctx).unwrap();
437        assert_eq!(result.len(), 0);
438    }
439
440    #[test]
441    fn test_indented_missing_space() {
442        let rule = MD027MultipleSpacesBlockquote;
443        // 4+ spaces makes this a code block, not a blockquote
444        let content = "   >This has 3 spaces indent and no space after marker";
445        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
446        let result = rule.check(&ctx).unwrap();
447        // LintContext sees this as a blockquote with no space after marker
448        // MD027 doesn't flag this as malformed
449        assert_eq!(result.len(), 0);
450    }
451
452    #[test]
453    fn test_fix_multiple_spaces() {
454        let rule = MD027MultipleSpacesBlockquote;
455        let content = ">  Two spaces\n>   Three spaces";
456        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
457        let fixed = rule.fix(&ctx).unwrap();
458        assert_eq!(fixed, "> Two spaces\n> Three spaces");
459    }
460
461    #[test]
462    fn test_fix_malformed_quotes() {
463        let rule = MD027MultipleSpacesBlockquote;
464        // These are valid nested blockquotes, not malformed
465        let content = ">>Nested without spaces\n>>>Deeply nested without spaces";
466        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
467        let fixed = rule.fix(&ctx).unwrap();
468        // No fix needed - these are valid
469        assert_eq!(fixed, content);
470    }
471
472    #[test]
473    fn test_fix_extra_marker() {
474        let rule = MD027MultipleSpacesBlockquote;
475        // This is valid - single blockquote with >Extra as content
476        let content = "> >Extra marker here";
477        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
478        let fixed = rule.fix(&ctx).unwrap();
479        // No fix needed
480        assert_eq!(fixed, content);
481    }
482
483    #[test]
484    fn test_code_block_ignored() {
485        let rule = MD027MultipleSpacesBlockquote;
486        let content = "```\n>  This is in a code block\n```";
487        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
488        let result = rule.check(&ctx).unwrap();
489        assert!(result.is_empty(), "Code blocks should be ignored");
490    }
491
492    #[test]
493    fn test_short_content_not_flagged() {
494        let rule = MD027MultipleSpacesBlockquote;
495        let content = ">>>\n>>";
496        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
497        let result = rule.check(&ctx).unwrap();
498        assert!(result.is_empty(), "Very short content should not be flagged");
499    }
500
501    #[test]
502    fn test_non_prose_not_flagged() {
503        let rule = MD027MultipleSpacesBlockquote;
504        let content = ">>#header\n>>[link]\n>>`code`\n>>http://example.com";
505        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
506        let result = rule.check(&ctx).unwrap();
507        assert!(result.is_empty(), "Non-prose content should not be flagged");
508    }
509
510    #[test]
511    fn test_preserve_trailing_newline() {
512        let rule = MD027MultipleSpacesBlockquote;
513        let content = ">  Two spaces\n";
514        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
515        let fixed = rule.fix(&ctx).unwrap();
516        assert_eq!(fixed, "> Two spaces\n");
517
518        let content_no_newline = ">  Two spaces";
519        let ctx2 = LintContext::new(content_no_newline, crate::config::MarkdownFlavor::Standard, None);
520        let fixed2 = rule.fix(&ctx2).unwrap();
521        assert_eq!(fixed2, "> Two spaces");
522    }
523
524    #[test]
525    fn test_mixed_issues() {
526        let rule = MD027MultipleSpacesBlockquote;
527        let content = ">  Multiple spaces here\n>>Normal nested quote\n> Normal quote";
528        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
529        let result = rule.check(&ctx).unwrap();
530        assert_eq!(result.len(), 1, "Should only flag the multiple spaces");
531        assert_eq!(result[0].line, 1);
532    }
533
534    #[test]
535    fn test_looks_like_blockquote_attempt() {
536        let rule = MD027MultipleSpacesBlockquote;
537
538        // Should return true for genuine attempts
539        assert!(rule.looks_like_blockquote_attempt(
540            ">>This is a real blockquote attempt with text",
541            "> > This is a real blockquote attempt with text"
542        ));
543
544        // Should return false for too short
545        assert!(!rule.looks_like_blockquote_attempt(">>>", "> > >"));
546
547        // Should return false for no alphabetic content
548        assert!(!rule.looks_like_blockquote_attempt(">>123", "> > 123"));
549
550        // Should return false for code-like content
551        assert!(!rule.looks_like_blockquote_attempt(">>#header", "> > #header"));
552    }
553
554    #[test]
555    fn test_extract_blockquote_fix() {
556        let rule = MD027MultipleSpacesBlockquote;
557        let regex = Regex::new(r"^(\s*)>>([^\s>].*|$)").unwrap();
558        let cap = regex.captures(">>content").unwrap();
559
560        let result = rule.extract_blockquote_fix_from_match(&cap, "missing spaces in nested blockquote", ">>content");
561        assert!(result.is_some());
562        let (fixed, desc) = result.unwrap();
563        assert_eq!(fixed, "> > content");
564        assert!(desc.contains("Missing spaces"));
565    }
566
567    #[test]
568    fn test_empty_blockquote() {
569        let rule = MD027MultipleSpacesBlockquote;
570        let content = ">\n>  \n> content";
571        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
572        let result = rule.check(&ctx).unwrap();
573        // Empty blockquotes with multiple spaces should still be flagged
574        assert_eq!(result.len(), 1);
575        assert_eq!(result[0].line, 2);
576    }
577
578    #[test]
579    fn test_fix_preserves_indentation() {
580        let rule = MD027MultipleSpacesBlockquote;
581        let content = "  >  Indented with multiple spaces";
582        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
583        let fixed = rule.fix(&ctx).unwrap();
584        assert_eq!(fixed, "  > Indented with multiple spaces");
585    }
586
587    #[test]
588    fn test_tabs_after_marker_not_flagged() {
589        // MD027 only flags multiple SPACES, not tabs
590        // Tabs after blockquote markers are handled by MD010 (no-hard-tabs)
591        // This matches markdownlint reference behavior
592        let rule = MD027MultipleSpacesBlockquote;
593
594        // Tab after marker - NOT flagged by MD027 (that's MD010's job)
595        let content = ">\tTab after marker";
596        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
597        let result = rule.check(&ctx).unwrap();
598        assert_eq!(result.len(), 0, "Single tab should not be flagged by MD027");
599
600        // Two tabs after marker - NOT flagged by MD027
601        let content2 = ">\t\tTwo tabs";
602        let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
603        let result2 = rule.check(&ctx2).unwrap();
604        assert_eq!(result2.len(), 0, "Tabs should not be flagged by MD027");
605    }
606
607    #[test]
608    fn test_mixed_spaces_and_tabs() {
609        let rule = MD027MultipleSpacesBlockquote;
610        // Space then tab - only flags if there are multiple spaces
611        // The tab itself is MD010's domain
612        let content = ">  Space Space";
613        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
614        let result = rule.check(&ctx).unwrap();
615        assert_eq!(result.len(), 1);
616        assert_eq!(result[0].column, 3); // Points to the extra space
617
618        // Three spaces should be flagged
619        let content2 = ">   Three spaces";
620        let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
621        let result2 = rule.check(&ctx2).unwrap();
622        assert_eq!(result2.len(), 1);
623    }
624
625    #[test]
626    fn test_fix_multiple_spaces_various() {
627        let rule = MD027MultipleSpacesBlockquote;
628        // Fix should remove extra spaces
629        let content = ">   Three spaces";
630        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
631        let fixed = rule.fix(&ctx).unwrap();
632        assert_eq!(fixed, "> Three spaces");
633
634        // Fix multiple spaces
635        let content2 = ">    Four spaces";
636        let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
637        let fixed2 = rule.fix(&ctx2).unwrap();
638        assert_eq!(fixed2, "> Four spaces");
639    }
640
641    #[test]
642    fn test_list_continuation_inside_blockquote_not_flagged() {
643        // List continuation indentation inside blockquotes should NOT be flagged
644        // This matches markdownlint-cli behavior
645        let rule = MD027MultipleSpacesBlockquote;
646
647        // List with continuation inside blockquote
648        let content = "> - Item starts here\n>   This continues the item\n> - Another item";
649        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
650        let result = rule.check(&ctx).unwrap();
651        assert!(
652            result.is_empty(),
653            "List continuation inside blockquote should not be flagged, got: {result:?}"
654        );
655
656        // Multiple list items with continuations
657        let content2 = "> * First item\n>   First item continuation\n>   Still continuing\n> * Second item";
658        let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
659        let result2 = rule.check(&ctx2).unwrap();
660        assert!(
661            result2.is_empty(),
662            "List continuations should not be flagged, got: {result2:?}"
663        );
664    }
665
666    #[test]
667    fn test_list_continuation_fix_preserves_indentation() {
668        // Ensure fix doesn't break list continuation indentation
669        let rule = MD027MultipleSpacesBlockquote;
670
671        let content = "> - Item\n>   continuation";
672        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
673        let fixed = rule.fix(&ctx).unwrap();
674        // Should preserve the list continuation indentation
675        assert_eq!(fixed, "> - Item\n>   continuation");
676    }
677
678    #[test]
679    fn test_non_list_multiple_spaces_still_flagged() {
680        // Non-list lines with multiple spaces should still be flagged
681        let rule = MD027MultipleSpacesBlockquote;
682
683        // Just extra spaces, not a list
684        let content = ">  This has extra spaces";
685        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
686        let result = rule.check(&ctx).unwrap();
687        assert_eq!(result.len(), 1, "Non-list line should be flagged");
688    }
689}