rumdl 0.1.71

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
//!
//! Rule MD014: Commands should show output
//!
//! See [docs/md014.md](../../docs/md014.md) for full documentation, configuration, and examples.

use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
use crate::rule_config_serde::RuleConfig;
use crate::utils::range_utils::calculate_match_range;
use crate::utils::regex_cache::get_cached_regex;
use toml;

mod md014_config;
use md014_config::MD014Config;

// Command detection patterns
const COMMAND_PATTERN: &str = r"^\s*[$>]\s+\S+";
const SHELL_LANG_PATTERN: &str = r"^(?i)(bash|sh|shell|console|terminal)";
const DOLLAR_PROMPT_PATTERN: &str = r"^\s*([$>])";

#[derive(Clone, Default)]
pub struct MD014CommandsShowOutput {
    config: MD014Config,
}

impl MD014CommandsShowOutput {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_show_output(show_output: bool) -> Self {
        Self {
            config: MD014Config { show_output },
        }
    }

    pub fn from_config_struct(config: MD014Config) -> Self {
        Self { config }
    }

    fn is_command_line(&self, line: &str) -> bool {
        get_cached_regex(COMMAND_PATTERN)
            .map(|re| re.is_match(line))
            .unwrap_or(false)
    }

    fn is_shell_language(&self, lang: &str) -> bool {
        get_cached_regex(SHELL_LANG_PATTERN)
            .map(|re| re.is_match(lang))
            .unwrap_or(false)
    }

    fn is_output_line(&self, line: &str) -> bool {
        let trimmed = line.trim();
        !trimmed.is_empty() && !trimmed.starts_with('$') && !trimmed.starts_with('>') && !trimmed.starts_with('#')
    }

    fn is_no_output_command(&self, cmd: &str) -> bool {
        let cmd = cmd.trim().to_lowercase();

        // Only skip commands that produce NO output by design.
        // Commands that produce output (even if verbose) should NOT be skipped -
        // the rule's intent is to encourage showing output when using $ prompts.

        // Shell built-ins and commands that produce no terminal output
        cmd.starts_with("cd ")
            || cmd == "cd"
            || cmd.starts_with("mkdir ")
            || cmd.starts_with("touch ")
            || cmd.starts_with("rm ")
            || cmd.starts_with("mv ")
            || cmd.starts_with("cp ")
            || cmd.starts_with("export ")
            || cmd.starts_with("set ")
            || cmd.starts_with("alias ")
            || cmd.starts_with("unset ")
            || cmd.starts_with("source ")
            || cmd.starts_with(". ")
            || cmd == "true"
            || cmd == "false"
            || cmd.starts_with("sleep ")
            || cmd.starts_with("wait ")
            || cmd.starts_with("pushd ")
            || cmd.starts_with("popd")

            // Shell redirects (output goes to file, not terminal)
            || cmd.contains(" > ")
            || cmd.contains(" >> ")

            // Git commands that produce no output on success
            || cmd.starts_with("git add ")
            || cmd.starts_with("git checkout ")
            || cmd.starts_with("git stash")
            || cmd.starts_with("git reset ")
    }

    fn is_command_without_output(&self, block: &[&str], lang: &str) -> bool {
        if !self.config.show_output || !self.is_shell_language(lang) {
            return false;
        }

        // Check if block has any output
        let has_output = block.iter().any(|line| self.is_output_line(line));
        if has_output {
            return false; // Has output, don't flag
        }

        // Flag if there's at least one command that should produce output
        self.get_first_output_command(block).is_some()
    }

    /// Returns the first command in the block that should produce output.
    /// Skips no-output commands like cd, mkdir, etc.
    fn get_first_output_command(&self, block: &[&str]) -> Option<(usize, String)> {
        for (i, line) in block.iter().enumerate() {
            if self.is_command_line(line) {
                let cmd = line.trim()[1..].trim().to_string();
                if !self.is_no_output_command(&cmd) {
                    return Some((i, cmd));
                }
            }
        }
        None // All commands are no-output commands
    }

    fn fix_command_block(&self, block: &[&str]) -> String {
        block
            .iter()
            .map(|line| {
                let trimmed = line.trim_start();
                if self.is_command_line(line) {
                    let spaces = line.len() - line.trim_start().len();
                    let cmd = trimmed.chars().skip(1).collect::<String>().trim_start().to_string();
                    format!("{}{}", " ".repeat(spaces), cmd)
                } else {
                    line.to_string()
                }
            })
            .collect::<Vec<_>>()
            .join("\n")
    }

    fn get_code_block_language(block_start: &str) -> String {
        block_start
            .trim_start()
            .trim_start_matches("```")
            .split_whitespace()
            .next()
            .unwrap_or("")
            .to_string()
    }

    /// Find all command lines in the block that should produce output.
    /// Skips no-output commands (cd, mkdir, etc.).
    fn find_all_command_lines<'a>(&self, block: &[&'a str]) -> Vec<(usize, &'a str)> {
        let mut results = Vec::new();
        for (i, line) in block.iter().enumerate() {
            if self.is_command_line(line) {
                let cmd = line.trim()[1..].trim();
                if !self.is_no_output_command(cmd) {
                    results.push((i, *line));
                }
            }
        }
        results
    }
}

impl Rule for MD014CommandsShowOutput {
    fn name(&self) -> &'static str {
        "MD014"
    }

    fn description(&self) -> &'static str {
        "Commands in code blocks should show output"
    }

    fn category(&self) -> RuleCategory {
        RuleCategory::CodeBlock
    }

    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
        let content = ctx.content;
        let _line_index = &ctx.line_index;

        let mut warnings = Vec::new();

        let mut current_block = Vec::new();

        let mut in_code_block = false;

        let mut block_start_line = 0;

        let mut current_lang = String::new();

        for (line_num, line) in content.lines().enumerate() {
            if line.trim_start().starts_with("```") {
                if in_code_block {
                    // End of code block
                    if self.is_command_without_output(&current_block, &current_lang) {
                        // Find all command lines that should produce output
                        let command_lines = self.find_all_command_lines(&current_block);
                        let fix = Fix {
                            range: {
                                // Replace the content line(s) between the fences
                                let content_start_line = block_start_line + 1; // Line after opening fence (0-indexed)
                                let content_end_line = line_num - 1; // Line before closing fence (0-indexed)

                                // Calculate byte range for the content lines including their newlines
                                let start_byte = _line_index.get_line_start_byte(content_start_line + 1).unwrap_or(0); // +1 for 1-indexed
                                let end_byte = _line_index
                                    .get_line_start_byte(content_end_line + 2)
                                    .unwrap_or(start_byte); // +2 to include newline after last content line
                                start_byte..end_byte
                            },
                            replacement: format!("{}\n", self.fix_command_block(&current_block)),
                        };

                        for (cmd_line_idx, cmd_line) in &command_lines {
                            let cmd_line_num = block_start_line + 1 + cmd_line_idx + 1; // +1 for fence, +1 for 1-indexed

                            // Find and highlight the dollar sign or prompt
                            if let Ok(re) = get_cached_regex(DOLLAR_PROMPT_PATTERN)
                                && let Some(cap) = re.captures(cmd_line)
                            {
                                let match_obj = cap.get(1).unwrap(); // The $ or > character
                                let (start_line, start_col, end_line, end_col) =
                                    calculate_match_range(cmd_line_num, cmd_line, match_obj.start(), match_obj.len());

                                // Extract command text from this specific line
                                let cmd_text = cmd_line.trim()[1..].trim().to_string();
                                let message = if cmd_text.is_empty() {
                                    "Command should show output (add example output or remove $ prompt)".to_string()
                                } else {
                                    format!(
                                        "Command '{cmd_text}' should show output (add example output or remove $ prompt)"
                                    )
                                };

                                warnings.push(LintWarning {
                                    rule_name: Some(self.name().to_string()),
                                    line: start_line,
                                    column: start_col,
                                    end_line,
                                    end_column: end_col,
                                    message,
                                    severity: Severity::Warning,
                                    fix: Some(fix.clone()),
                                });
                            }
                        }
                    }
                    current_block.clear();
                } else {
                    // Start of code block
                    block_start_line = line_num;
                    current_lang = Self::get_code_block_language(line);
                }
                in_code_block = !in_code_block;
            } else if in_code_block {
                current_block.push(line);
            }
        }

        Ok(warnings)
    }

    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
        if self.should_skip(ctx) {
            return Ok(ctx.content.to_string());
        }
        let warnings = self.check(ctx)?;
        if warnings.is_empty() {
            return Ok(ctx.content.to_string());
        }
        let warnings =
            crate::utils::fix_utils::filter_warnings_by_inline_config(warnings, ctx.inline_config(), self.name());
        crate::utils::fix_utils::apply_warning_fixes(ctx.content, &warnings)
            .map_err(crate::rule::LintError::InvalidInput)
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
        // Skip if content is empty or has no code blocks
        ctx.content.is_empty() || !ctx.likely_has_code()
    }

    fn default_config_section(&self) -> Option<(String, toml::Value)> {
        let default_config = MD014Config::default();
        let json_value = serde_json::to_value(&default_config).ok()?;
        let toml_value = crate::rule_config_serde::json_to_toml_value(&json_value)?;

        if let toml::Value::Table(table) = toml_value {
            if !table.is_empty() {
                Some((MD014Config::RULE_NAME.to_string(), toml::Value::Table(table)))
            } else {
                None
            }
        } else {
            None
        }
    }

    fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
    where
        Self: Sized,
    {
        let rule_config = crate::rule_config_serde::load_rule_config::<MD014Config>(config);
        Box::new(Self::from_config_struct(rule_config))
    }
}

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

    #[test]
    fn test_is_command_line() {
        let rule = MD014CommandsShowOutput::new();
        assert!(rule.is_command_line("$ echo test"));
        assert!(rule.is_command_line("  $ ls -la"));
        assert!(rule.is_command_line("> pwd"));
        assert!(rule.is_command_line("   > cd /home"));
        assert!(!rule.is_command_line("echo test"));
        assert!(!rule.is_command_line("# comment"));
        assert!(!rule.is_command_line("output line"));
    }

    #[test]
    fn test_is_shell_language() {
        let rule = MD014CommandsShowOutput::new();
        assert!(rule.is_shell_language("bash"));
        assert!(rule.is_shell_language("BASH"));
        assert!(rule.is_shell_language("sh"));
        assert!(rule.is_shell_language("shell"));
        assert!(rule.is_shell_language("Shell"));
        assert!(rule.is_shell_language("console"));
        assert!(rule.is_shell_language("CONSOLE"));
        assert!(rule.is_shell_language("terminal"));
        assert!(rule.is_shell_language("Terminal"));
        assert!(!rule.is_shell_language("python"));
        assert!(!rule.is_shell_language("javascript"));
        assert!(!rule.is_shell_language(""));
    }

    #[test]
    fn test_is_output_line() {
        let rule = MD014CommandsShowOutput::new();
        assert!(rule.is_output_line("output text"));
        assert!(rule.is_output_line("   some output"));
        assert!(rule.is_output_line("file1 file2"));
        assert!(!rule.is_output_line(""));
        assert!(!rule.is_output_line("   "));
        assert!(!rule.is_output_line("$ command"));
        assert!(!rule.is_output_line("> prompt"));
        assert!(!rule.is_output_line("# comment"));
    }

    #[test]
    fn test_is_no_output_command() {
        let rule = MD014CommandsShowOutput::new();

        // Shell built-ins that produce no output
        assert!(rule.is_no_output_command("cd /home"));
        assert!(rule.is_no_output_command("cd"));
        assert!(rule.is_no_output_command("mkdir test"));
        assert!(rule.is_no_output_command("touch file.txt"));
        assert!(rule.is_no_output_command("rm -rf dir"));
        assert!(rule.is_no_output_command("mv old new"));
        assert!(rule.is_no_output_command("cp src dst"));
        assert!(rule.is_no_output_command("export VAR=value"));
        assert!(rule.is_no_output_command("set -e"));
        assert!(rule.is_no_output_command("source ~/.bashrc"));
        assert!(rule.is_no_output_command(". ~/.profile"));
        assert!(rule.is_no_output_command("alias ll='ls -la'"));
        assert!(rule.is_no_output_command("unset VAR"));
        assert!(rule.is_no_output_command("true"));
        assert!(rule.is_no_output_command("false"));
        assert!(rule.is_no_output_command("sleep 5"));
        assert!(rule.is_no_output_command("pushd /tmp"));
        assert!(rule.is_no_output_command("popd"));

        // Case insensitive (lowercased internally)
        assert!(rule.is_no_output_command("CD /HOME"));
        assert!(rule.is_no_output_command("MKDIR TEST"));

        // Shell redirects (output goes to file)
        assert!(rule.is_no_output_command("echo 'test' > file.txt"));
        assert!(rule.is_no_output_command("cat input.txt > output.txt"));
        assert!(rule.is_no_output_command("echo 'append' >> log.txt"));

        // Git commands that produce no output on success
        assert!(rule.is_no_output_command("git add ."));
        assert!(rule.is_no_output_command("git checkout main"));
        assert!(rule.is_no_output_command("git stash"));
        assert!(rule.is_no_output_command("git reset HEAD~1"));

        // Commands that PRODUCE output (should NOT be skipped)
        assert!(!rule.is_no_output_command("ls -la"));
        assert!(!rule.is_no_output_command("echo test")); // echo without redirect
        assert!(!rule.is_no_output_command("pwd"));
        assert!(!rule.is_no_output_command("cat file.txt")); // cat without redirect
        assert!(!rule.is_no_output_command("grep pattern file"));

        // Installation commands PRODUCE output (should NOT be skipped)
        assert!(!rule.is_no_output_command("pip install requests"));
        assert!(!rule.is_no_output_command("npm install express"));
        assert!(!rule.is_no_output_command("cargo install ripgrep"));
        assert!(!rule.is_no_output_command("brew install git"));

        // Build commands PRODUCE output (should NOT be skipped)
        assert!(!rule.is_no_output_command("cargo build"));
        assert!(!rule.is_no_output_command("npm run build"));
        assert!(!rule.is_no_output_command("make"));

        // Docker commands PRODUCE output (should NOT be skipped)
        assert!(!rule.is_no_output_command("docker ps"));
        assert!(!rule.is_no_output_command("docker compose up"));
        assert!(!rule.is_no_output_command("docker run myimage"));

        // Git commands that PRODUCE output (should NOT be skipped)
        assert!(!rule.is_no_output_command("git status"));
        assert!(!rule.is_no_output_command("git log"));
        assert!(!rule.is_no_output_command("git diff"));
    }

    #[test]
    fn test_fix_command_block() {
        let rule = MD014CommandsShowOutput::new();
        let block = vec!["$ echo test", "$ ls -la"];
        assert_eq!(rule.fix_command_block(&block), "echo test\nls -la");

        let indented = vec!["    $ echo test", "  $ pwd"];
        assert_eq!(rule.fix_command_block(&indented), "    echo test\n  pwd");

        let mixed = vec!["> cd /home", "$ mkdir test"];
        assert_eq!(rule.fix_command_block(&mixed), "cd /home\nmkdir test");
    }

    #[test]
    fn test_get_code_block_language() {
        assert_eq!(MD014CommandsShowOutput::get_code_block_language("```bash"), "bash");
        assert_eq!(MD014CommandsShowOutput::get_code_block_language("```shell"), "shell");
        assert_eq!(
            MD014CommandsShowOutput::get_code_block_language("   ```console"),
            "console"
        );
        assert_eq!(
            MD014CommandsShowOutput::get_code_block_language("```bash {.line-numbers}"),
            "bash"
        );
        assert_eq!(MD014CommandsShowOutput::get_code_block_language("```"), "");
    }

    #[test]
    fn test_find_all_command_lines() {
        let rule = MD014CommandsShowOutput::new();
        let block = vec!["# comment", "$ echo test", "output"];
        let result = rule.find_all_command_lines(&block);
        assert_eq!(result, vec![(1, "$ echo test")]);

        let no_commands = vec!["output1", "output2"];
        assert!(rule.find_all_command_lines(&no_commands).is_empty());

        let multiple = vec!["$ echo one", "$ echo two", "$ cd /tmp"];
        let result = rule.find_all_command_lines(&multiple);
        // cd is a no-output command, so only echo commands are returned
        assert_eq!(result, vec![(0, "$ echo one"), (1, "$ echo two")]);
    }

    #[test]
    fn test_is_command_without_output() {
        let rule = MD014CommandsShowOutput::with_show_output(true);

        // Commands without output should be flagged
        let block1 = vec!["$ echo test"];
        assert!(rule.is_command_without_output(&block1, "bash"));

        // Commands with output should not be flagged
        let block2 = vec!["$ echo test", "test"];
        assert!(!rule.is_command_without_output(&block2, "bash"));

        // No-output commands should not be flagged
        let block3 = vec!["$ cd /home"];
        assert!(!rule.is_command_without_output(&block3, "bash"));

        // Disabled rule should not flag
        let rule_disabled = MD014CommandsShowOutput::with_show_output(false);
        assert!(!rule_disabled.is_command_without_output(&block1, "bash"));

        // Non-shell language should not be flagged
        assert!(!rule.is_command_without_output(&block1, "python"));
    }

    #[test]
    fn test_edge_cases() {
        let rule = MD014CommandsShowOutput::new();
        // Bare $ doesn't match command pattern (needs a command after $)
        let content = "```bash\n$ \n```";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(
            result.is_empty(),
            "Bare $ with only space doesn't match command pattern"
        );

        // Test empty code block
        let empty_content = "```bash\n```";
        let ctx2 = LintContext::new(empty_content, crate::config::MarkdownFlavor::Standard, None);
        let result2 = rule.check(&ctx2).unwrap();
        assert!(result2.is_empty(), "Empty code block should not be flagged");

        // Test minimal command
        let minimal = "```bash\n$ a\n```";
        let ctx3 = LintContext::new(minimal, crate::config::MarkdownFlavor::Standard, None);
        let result3 = rule.check(&ctx3).unwrap();
        assert_eq!(result3.len(), 1, "Minimal command should be flagged");
    }

    #[test]
    fn test_mixed_silent_and_output_commands() {
        let rule = MD014CommandsShowOutput::new();

        // Block with only silent commands should NOT be flagged
        let silent_only = "```bash\n$ cd /home\n$ mkdir test\n```";
        let ctx1 = LintContext::new(silent_only, crate::config::MarkdownFlavor::Standard, None);
        let result1 = rule.check(&ctx1).unwrap();
        assert!(
            result1.is_empty(),
            "Block with only silent commands should not be flagged"
        );

        // Block with silent commands followed by output-producing command
        // should flag the output-producing command only
        let mixed_silent_first = "```bash\n$ cd /home\n$ ls -la\n```";
        let ctx2 = LintContext::new(mixed_silent_first, crate::config::MarkdownFlavor::Standard, None);
        let result2 = rule.check(&ctx2).unwrap();
        assert_eq!(result2.len(), 1, "Only output-producing commands should be flagged");
        assert!(
            result2[0].message.contains("ls -la"),
            "Message should mention 'ls -la', not 'cd /home'. Got: {}",
            result2[0].message
        );

        // Block with mkdir followed by cat (which produces output)
        let mixed_mkdir_cat = "```bash\n$ mkdir test\n$ cat file.txt\n```";
        let ctx3 = LintContext::new(mixed_mkdir_cat, crate::config::MarkdownFlavor::Standard, None);
        let result3 = rule.check(&ctx3).unwrap();
        assert_eq!(result3.len(), 1, "Only output-producing commands should be flagged");
        assert!(
            result3[0].message.contains("cat file.txt"),
            "Message should mention 'cat file.txt', not 'mkdir'. Got: {}",
            result3[0].message
        );

        // Block with silent command followed by pip install (which produces output)
        let mkdir_pip = "```bash\n$ mkdir test\n$ pip install something\n```";
        let ctx3b = LintContext::new(mkdir_pip, crate::config::MarkdownFlavor::Standard, None);
        let result3b = rule.check(&ctx3b).unwrap();
        assert_eq!(result3b.len(), 1, "Block with pip install should be flagged");
        assert!(
            result3b[0].message.contains("pip install"),
            "Message should mention 'pip install'. Got: {}",
            result3b[0].message
        );

        // Block with output-producing command followed by silent command
        let mixed_output_first = "```bash\n$ echo hello\n$ cd /home\n```";
        let ctx4 = LintContext::new(mixed_output_first, crate::config::MarkdownFlavor::Standard, None);
        let result4 = rule.check(&ctx4).unwrap();
        assert_eq!(result4.len(), 1, "Only output-producing commands should be flagged");
        assert!(
            result4[0].message.contains("echo hello"),
            "Message should mention 'echo hello'. Got: {}",
            result4[0].message
        );
    }

    #[test]
    fn test_multiple_commands_without_output_all_flagged() {
        let rule = MD014CommandsShowOutput::new();

        // Two identical commands without output should produce two warnings
        let content = "```shell\n# First invocation\n$ my_command\n\n# Second invocation\n$ my_command\n```";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2, "Both commands should be flagged. Got: {result:?}");
        assert!(result[0].message.contains("my_command"));
        assert!(result[1].message.contains("my_command"));
        // Verify they point to different lines
        assert_ne!(result[0].line, result[1].line, "Warnings should be on different lines");

        // Three different commands without output
        let content2 = "```bash\n$ echo hello\n$ ls -la\n$ pwd\n```";
        let ctx2 = LintContext::new(content2, crate::config::MarkdownFlavor::Standard, None);
        let result2 = rule.check(&ctx2).unwrap();
        assert_eq!(
            result2.len(),
            3,
            "All three commands should be flagged. Got: {result2:?}"
        );
        assert!(result2[0].message.contains("echo hello"));
        assert!(result2[1].message.contains("ls -la"));
        assert!(result2[2].message.contains("pwd"));

        // Two output-producing commands mixed with one silent command
        let content3 = "```bash\n$ echo hello\n$ cd /tmp\n$ ls -la\n```";
        let ctx3 = LintContext::new(content3, crate::config::MarkdownFlavor::Standard, None);
        let result3 = rule.check(&ctx3).unwrap();
        assert_eq!(
            result3.len(),
            2,
            "Only output-producing commands should be flagged. Got: {result3:?}"
        );
        assert!(result3[0].message.contains("echo hello"));
        assert!(result3[1].message.contains("ls -la"));
    }

    #[test]
    fn test_issue_516_exact_case() {
        let rule = MD014CommandsShowOutput::new();

        // Exact test case from GitHub issue #516
        let content = "---\ntitle: Heading\n---\n\nHere is a fenced code block:\n\n```shell\n# First invocation of my_command\n$ my_command\n\n# Second invocation of my_command\n$ my_command\n```\n";
        let ctx = LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(
            result.len(),
            2,
            "Both $ my_command lines should be flagged. Got: {result:?}"
        );
        assert_eq!(result[0].line, 9, "First warning should be on line 9");
        assert_eq!(result[1].line, 12, "Second warning should be on line 12");
    }

    #[test]
    fn test_default_config_section() {
        let rule = MD014CommandsShowOutput::new();
        let config_section = rule.default_config_section();
        assert!(config_section.is_some());
        let (name, _value) = config_section.unwrap();
        assert_eq!(name, "MD014");
    }
}