rumdl 0.1.51

A fast Markdown linter written in Rust (Ru(st) MarkDown Linter)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
use crate::utils::regex_cache::get_cached_regex;

// Regex patterns
const ALL_WHITESPACE_STR: &str = r"^\s*$";

/// Rule MD039: No space inside link text
///
/// See [docs/md039.md](../../docs/md039.md) for full documentation, configuration, and examples.
///
/// This rule is triggered when link text has leading or trailing spaces which can cause
/// unexpected rendering in some Markdown parsers.
#[derive(Debug, Default, Clone)]
pub struct MD039NoSpaceInLinks;

// Static definition for the warning message
const WARNING_MESSAGE: &str = "Remove spaces inside link text";

impl MD039NoSpaceInLinks {
    pub fn new() -> Self {
        Self
    }

    #[inline]
    fn trim_link_text_preserve_escapes(text: &str) -> &str {
        // Optimized trimming that preserves escapes
        let start = text
            .char_indices()
            .find(|&(_, c)| !c.is_whitespace())
            .map(|(i, _)| i)
            .unwrap_or(text.len());
        let end = text
            .char_indices()
            .rev()
            .find(|&(_, c)| !c.is_whitespace())
            .map(|(i, c)| i + c.len_utf8())
            .unwrap_or(0);
        if start >= end { "" } else { &text[start..end] }
    }

    /// Optimized whitespace checking for link text
    #[inline]
    fn needs_trimming(&self, text: &str) -> bool {
        // Simple and fast check: compare with trimmed version
        text != text.trim_matches(|c: char| c.is_whitespace())
    }

    /// Optimized unescaping for performance-critical path
    #[inline]
    fn unescape_fast(&self, text: &str) -> String {
        if !text.contains('\\') {
            return text.to_string();
        }

        let mut result = String::with_capacity(text.len());
        let mut chars = text.chars().peekable();

        while let Some(c) = chars.next() {
            if c == '\\' {
                if let Some(&next) = chars.peek() {
                    result.push(next);
                    chars.next();
                } else {
                    result.push(c);
                }
            } else {
                result.push(c);
            }
        }
        result
    }
}

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

    fn description(&self) -> &'static str {
        "Spaces inside link text"
    }

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

    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
        ctx.content.is_empty() || !ctx.likely_has_links_or_images()
    }

    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
        let mut warnings = Vec::new();

        // Use centralized link parsing from LintContext
        for link in &ctx.links {
            // Skip reference links (markdownlint doesn't check these)
            if link.is_reference {
                continue;
            }

            // Skip links inside Jinja templates
            if ctx.is_in_jinja_range(link.byte_offset) {
                continue;
            }

            // Skip links inside JSX expressions or MDX comments
            if ctx.is_in_jsx_expression(link.byte_offset) || ctx.is_in_mdx_comment(link.byte_offset) {
                continue;
            }

            // Fast check if trimming is needed
            if !self.needs_trimming(&link.text) {
                continue;
            }

            // Optimized unescaping for whitespace check
            let unescaped = self.unescape_fast(&link.text);

            let needs_warning = if get_cached_regex(ALL_WHITESPACE_STR)
                .map(|re| re.is_match(&unescaped))
                .unwrap_or(false)
            {
                true
            } else {
                let trimmed = link.text.trim_matches(|c: char| c.is_whitespace());
                link.text.as_ref() != trimmed
            };

            if needs_warning {
                let url = if link.is_reference {
                    if let Some(ref_id) = &link.reference_id {
                        format!("[{ref_id}]")
                    } else {
                        "[]".to_string()
                    }
                } else {
                    format!("({})", link.url)
                };

                let fixed = if get_cached_regex(ALL_WHITESPACE_STR)
                    .map(|re| re.is_match(&unescaped))
                    .unwrap_or(false)
                {
                    format!("[]{url}")
                } else {
                    let trimmed = Self::trim_link_text_preserve_escapes(&link.text);
                    format!("[{trimmed}]{url}")
                };

                warnings.push(LintWarning {
                    rule_name: Some(self.name().to_string()),
                    line: link.line,
                    column: link.start_col + 1, // Convert to 1-indexed
                    end_line: link.line,
                    end_column: link.end_col + 1, // Convert to 1-indexed
                    message: WARNING_MESSAGE.to_string(),
                    severity: Severity::Warning,
                    fix: Some(Fix {
                        range: link.byte_offset..link.byte_end,
                        replacement: fixed,
                    }),
                });
            }
        }

        // Also check images
        for image in &ctx.images {
            // Skip reference images (markdownlint doesn't check these)
            if image.is_reference {
                continue;
            }

            // Skip images inside JSX expressions or MDX comments
            if ctx.is_in_jsx_expression(image.byte_offset) || ctx.is_in_mdx_comment(image.byte_offset) {
                continue;
            }

            // Skip images inside Jinja templates
            if ctx.is_in_jinja_range(image.byte_offset) {
                continue;
            }

            // Fast check if trimming is needed
            if !self.needs_trimming(&image.alt_text) {
                continue;
            }

            // Optimized unescaping for whitespace check
            let unescaped = self.unescape_fast(&image.alt_text);

            let needs_warning = if get_cached_regex(ALL_WHITESPACE_STR)
                .map(|re| re.is_match(&unescaped))
                .unwrap_or(false)
            {
                true
            } else {
                let trimmed = image.alt_text.trim_matches(|c: char| c.is_whitespace());
                image.alt_text.as_ref() != trimmed
            };

            if needs_warning {
                let url = if image.is_reference {
                    if let Some(ref_id) = &image.reference_id {
                        format!("[{ref_id}]")
                    } else {
                        "[]".to_string()
                    }
                } else {
                    format!("({})", image.url)
                };

                let fixed = if get_cached_regex(ALL_WHITESPACE_STR)
                    .map(|re| re.is_match(&unescaped))
                    .unwrap_or(false)
                {
                    format!("![]{url}")
                } else {
                    let trimmed = Self::trim_link_text_preserve_escapes(&image.alt_text);
                    format!("![{trimmed}]{url}")
                };

                warnings.push(LintWarning {
                    rule_name: Some(self.name().to_string()),
                    line: image.line,
                    column: image.start_col + 1, // Convert to 1-indexed
                    end_line: image.line,
                    end_column: image.end_col + 1, // Convert to 1-indexed
                    message: WARNING_MESSAGE.to_string(),
                    severity: Severity::Warning,
                    fix: Some(Fix {
                        range: image.byte_offset..image.byte_end,
                        replacement: fixed,
                    }),
                });
            }
        }

        Ok(warnings)
    }

    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
        let content = ctx.content;
        let mut fixes = Vec::new();

        // Process links
        for link in &ctx.links {
            // Skip reference links (markdownlint doesn't check these)
            if link.is_reference {
                continue;
            }

            // Skip links where this rule is disabled by inline config
            if ctx.inline_config().is_rule_disabled(self.name(), link.line) {
                continue;
            }

            // Skip links inside Jinja templates
            if ctx.is_in_jinja_range(link.byte_offset) {
                continue;
            }

            if !self.needs_trimming(&link.text) {
                continue;
            }

            let unescaped = self.unescape_fast(&link.text);

            let needs_fix = if get_cached_regex(ALL_WHITESPACE_STR)
                .map(|re| re.is_match(&unescaped))
                .unwrap_or(false)
            {
                true
            } else {
                let trimmed = link.text.trim_matches(|c: char| c.is_whitespace());
                link.text.as_ref() != trimmed
            };

            if needs_fix {
                let url_part = if link.is_reference {
                    if let Some(ref_id) = &link.reference_id {
                        format!("[{ref_id}]")
                    } else {
                        "[]".to_string()
                    }
                } else {
                    format!("({})", link.url)
                };

                let replacement = if get_cached_regex(ALL_WHITESPACE_STR)
                    .map(|re| re.is_match(&unescaped))
                    .unwrap_or(false)
                {
                    format!("[]{url_part}")
                } else {
                    let trimmed = Self::trim_link_text_preserve_escapes(&link.text);
                    format!("[{trimmed}]{url_part}")
                };

                fixes.push((link.byte_offset, link.byte_end, replacement));
            }
        }

        // Process images
        for image in &ctx.images {
            // Skip reference images (markdownlint doesn't check these)
            if image.is_reference {
                continue;
            }

            // Skip images where this rule is disabled by inline config
            if ctx.inline_config().is_rule_disabled(self.name(), image.line) {
                continue;
            }

            // Skip images inside Jinja templates
            if ctx.is_in_jinja_range(image.byte_offset) {
                continue;
            }

            if !self.needs_trimming(&image.alt_text) {
                continue;
            }

            let unescaped = self.unescape_fast(&image.alt_text);

            let needs_fix = if get_cached_regex(ALL_WHITESPACE_STR)
                .map(|re| re.is_match(&unescaped))
                .unwrap_or(false)
            {
                true
            } else {
                let trimmed = image.alt_text.trim_matches(|c: char| c.is_whitespace());
                image.alt_text.as_ref() != trimmed
            };

            if needs_fix {
                let url_part = if image.is_reference {
                    if let Some(ref_id) = &image.reference_id {
                        format!("[{ref_id}]")
                    } else {
                        "[]".to_string()
                    }
                } else {
                    format!("({})", image.url)
                };

                let replacement = if get_cached_regex(ALL_WHITESPACE_STR)
                    .map(|re| re.is_match(&unescaped))
                    .unwrap_or(false)
                {
                    format!("![]{url_part}")
                } else {
                    let trimmed = Self::trim_link_text_preserve_escapes(&image.alt_text);
                    format!("![{trimmed}]{url_part}")
                };

                fixes.push((image.byte_offset, image.byte_end, replacement));
            }
        }

        if fixes.is_empty() {
            return Ok(content.to_string());
        }

        // Sort fixes by position to apply them in order
        fixes.sort_by_key(|&(start, _, _)| start);

        // Apply fixes efficiently
        let mut result = String::with_capacity(content.len());
        let mut last_pos = 0;

        for (start, end, replacement) in fixes {
            if start < last_pos {
                // This should not happen if fixes are properly sorted and non-overlapping
                return Err(LintError::FixFailed(format!(
                    "Overlapping fixes detected: last_pos={last_pos}, start={start}"
                )));
            }
            result.push_str(&content[last_pos..start]);
            result.push_str(&replacement);
            last_pos = end;
        }
        result.push_str(&content[last_pos..]);

        Ok(result)
    }

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

    fn from_config(_config: &crate::config::Config) -> Box<dyn Rule>
    where
        Self: Sized,
    {
        Box::new(Self)
    }
}

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

    #[test]
    fn test_valid_links() {
        let rule = MD039NoSpaceInLinks::new();
        let content = "[link](url) and [another link](url) here";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_spaces_both_ends() {
        let rule = MD039NoSpaceInLinks::new();
        let content = "[ link ](url) and [ another link ](url) here";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "[link](url) and [another link](url) here");
    }

    #[test]
    fn test_space_at_start() {
        let rule = MD039NoSpaceInLinks::new();
        let content = "[ link](url) and [ another link](url) here";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "[link](url) and [another link](url) here");
    }

    #[test]
    fn test_space_at_end() {
        let rule = MD039NoSpaceInLinks::new();
        let content = "[link ](url) and [another link ](url) here";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "[link](url) and [another link](url) here");
    }

    #[test]
    fn test_link_in_code_block() {
        let rule = MD039NoSpaceInLinks::new();
        let content = "```
[ link ](url)
```
[ link ](url)";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(
            fixed,
            "```
[ link ](url)
```
[link](url)"
        );
    }

    #[test]
    fn test_multiple_links() {
        let rule = MD039NoSpaceInLinks::new();
        let content = "[ link ](url) and [ another ](url) in one line";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "[link](url) and [another](url) in one line");
    }

    #[test]
    fn test_link_with_internal_spaces() {
        let rule = MD039NoSpaceInLinks::new();
        let content = "[this is link](url) and [ this is also link ](url)";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 1);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "[this is link](url) and [this is also link](url)");
    }

    #[test]
    fn test_link_with_punctuation() {
        let rule = MD039NoSpaceInLinks::new();
        let content = "[ link! ](url) and [ link? ](url) here";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let result = rule.check(&ctx).unwrap();
        assert_eq!(result.len(), 2);
        let fixed = rule.fix(&ctx).unwrap();
        assert_eq!(fixed, "[link!](url) and [link?](url) here");
    }

    #[test]
    fn test_parity_only_whitespace_and_newlines_minimal() {
        let rule = MD039NoSpaceInLinks::new();
        let content = "[   \n  ](url) and [\t\n\t](url)";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        // markdownlint removes all whitespace, resulting in empty link text
        assert_eq!(fixed, "[](url) and [](url)");
    }

    #[test]
    fn test_parity_internal_newlines_minimal() {
        let rule = MD039NoSpaceInLinks::new();
        let content = "[link\ntext](url) and [ another\nlink ](url)";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        // markdownlint trims only leading/trailing whitespace, preserves internal newlines
        assert_eq!(fixed, "[link\ntext](url) and [another\nlink](url)");
    }

    #[test]
    fn test_parity_escaped_brackets_minimal() {
        let rule = MD039NoSpaceInLinks::new();
        let content = "[link\\]](url) and [link\\[]](url)";
        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
        let fixed = rule.fix(&ctx).unwrap();
        // markdownlint does not trim or remove escapes, so output should be unchanged
        assert_eq!(fixed, "[link\\]](url) and [link\\[]](url)");
    }

    #[test]
    fn test_performance_md039() {
        use std::time::Instant;

        let rule = MD039NoSpaceInLinks::new();

        // Generate test content with many links
        let mut content = String::with_capacity(100_000);

        // Add links with spaces (should be detected and fixed)
        for i in 0..500 {
            content.push_str(&format!("Line {i} with [ spaced link {i} ](url{i}) and text.\n"));
        }

        // Add valid links (should be fast to skip)
        for i in 0..500 {
            content.push_str(&format!(
                "Line {} with [valid link {}](url{}) and text.\n",
                i + 500,
                i,
                i
            ));
        }

        println!(
            "MD039 Performance Test - Content: {} bytes, {} lines",
            content.len(),
            content.lines().count()
        );

        let ctx = crate::lint_context::LintContext::new(&content, crate::config::MarkdownFlavor::Standard, None);

        // Warm up
        let _ = rule.check(&ctx).unwrap();

        // Measure check performance
        let mut total_duration = std::time::Duration::ZERO;
        let runs = 5;
        let mut warnings_count = 0;

        for _ in 0..runs {
            let start = Instant::now();
            let warnings = rule.check(&ctx).unwrap();
            total_duration += start.elapsed();
            warnings_count = warnings.len();
        }

        let avg_check_duration = total_duration / runs;

        println!("MD039 Optimized Performance:");
        println!(
            "- Average check time: {:?} ({:.2} ms)",
            avg_check_duration,
            avg_check_duration.as_secs_f64() * 1000.0
        );
        println!("- Found {warnings_count} warnings");
        println!(
            "- Lines per second: {:.0}",
            content.lines().count() as f64 / avg_check_duration.as_secs_f64()
        );
        println!(
            "- Microseconds per line: {:.2}",
            avg_check_duration.as_micros() as f64 / content.lines().count() as f64
        );

        // Performance assertion - should complete reasonably fast
        assert!(
            avg_check_duration.as_millis() < 200,
            "MD039 check should complete in under 200ms, took {}ms",
            avg_check_duration.as_millis()
        );

        // Verify we're finding the expected number of warnings (500 links with spaces)
        assert_eq!(warnings_count, 500, "Should find 500 warnings for links with spaces");
    }
}