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
use rumdl_lib::lint_context::LintContext;
use rumdl_lib::rule::Rule;
use rumdl_lib::rules::MD053LinkImageReferenceDefinitions;

#[test]
fn test_all_references_used() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[link1][id1]\n[link2][id2]\n![image][id3]\n\n[id1]: http://example.com/1\n[id2]: http://example.com/2\n[id3]: http://example.com/3";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty());
}

#[test]
fn test_unused_reference() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[link1][id1]\n\n[id1]: http://example.com/1\n[id2]: http://example.com/2";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 1); // Should detect id2 as unused
}

#[test]
fn test_shortcut_reference() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[example]\n\n[example]: http://example.com";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty());
}

#[test]
fn test_multiple_unused_references() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content =
        "[link1][id1]\n\n[id1]: http://example.com/1\n[id2]: http://example.com/2\n[id3]: http://example.com/3";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 2); // Should detect id2 and id3 as unused
}

#[test]
fn test_case_insensitive() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[example][ID]\n\n[id]: http://example.com";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty());
}

#[test]
fn test_empty_content() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty());
}

#[test]
fn test_no_references() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "# Just a heading\n\nSome regular text\n\n> A blockquote";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty());
}

#[test]
fn test_only_unused_references() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[id1]: http://example.com/1\n[id2]: http://example.com/2";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 2); // All references are unused
}

#[test]
fn test_mixed_used_unused_references() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[link][used]\nSome text\n\n[used]: http://example.com/used\n[unused]: http://example.com/unused";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 1); // Should detect unused reference
}

#[test]
fn test_valid_reference_definitions() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[ref]: https://example.com\n[ref] is a link";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty());
}

#[test]
fn test_unused_reference_definition() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[unused]: https://example.com\nThis has no references";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(!result.is_empty());
}

#[test]
fn test_multiple_references() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[ref1]: https://example1.com\n[ref2]: https://example2.com\n[ref1] and [ref2] are links";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty());
}

#[test]
fn test_image_references() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[img]: image.png\n![Image][img]";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty());
}

#[test]
fn test_mixed_references() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[ref]: https://example.com\n[img]: image.png\n[ref] is a link and ![Image][img] is an image";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty());
}

#[test]
fn test_ignored_definitions() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[ignored]: https://example.com\nNo references here";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 1);
}

#[test]
fn test_case_sensitivity() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[REF]: https://example.com\n[ref] is a link";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty());
}

// test_fix_unused_references removed - MD053 no longer provides fixes

#[test]
fn test_with_document_structure() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    let content = "[link1][id1]\n[link2][id2]\n![image][id3]\n\n[id1]: http://example.com/1\n[id2]: http://example.com/2\n[id3]: http://example.com/3";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty());
}

#[test]
fn test_case_insensitive_with_backticks() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test case mismatch with backticks
    let content = "# Test\n\nThis is [`Example`].\n\n[`example`]: https://example.com\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(
        result.is_empty(),
        "Case-insensitive matching should work with backticks"
    );
}

#[test]
fn test_case_insensitive_dotted_references() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test case mismatch with dots in backticks (like dataclasses.InitVar from ruff repo)
    let content = "From the Python documentation on [`dataclasses.InitVar`]:\n\n[`dataclasses.initvar`]: https://docs.python.org/3/library/dataclasses.html#dataclasses.InitVar\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(
        result.is_empty(),
        "Case-insensitive matching should work for dotted references in backticks"
    );
}

#[test]
fn test_references_with_apostrophes() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test case mismatch with apostrophes (like De Morgan's)
    let content = "The [De Morgan's Laws] are important.\n\n[de morgan's laws]: https://example.com\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(
        result.is_empty(),
        "Case-insensitive matching should work with apostrophes"
    );
}

#[test]
fn test_references_with_dots_not_filtered() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test references with dots (previously incorrectly filtered as config sections)
    let content = "See [tool.ruff] for details.\n\n[tool.ruff]: https://example.com\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty(), "References with dots should be recognized");
}

#[test]
fn test_references_with_slashes_not_filtered() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test references with forward slashes (previously incorrectly filtered as file paths)
    let content = "See [docs/api] for details.\n\n[docs/api]: https://example.com\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(
        result.is_empty(),
        "References with forward slashes should be recognized"
    );
}

#[test]
fn test_single_letter_references() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test single letter references (previously incorrectly filtered)
    let content = "See [T] for type parameter.\n\n[T]: https://example.com\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty(), "Single letter references should be recognized");
}

#[test]
fn test_common_type_name_references() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test common type name references (previously incorrectly filtered)
    let content = "The [str] type in Python.\n\n[str]: https://docs.python.org/3/library/stdtypes.html#str\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(
        result.is_empty(),
        "Common type names as references should be recognized"
    );
}

#[test]
fn test_shortcut_reference_with_colon() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test shortcut reference followed by colon (common in documentation)
    let content = "As stated in [`reference`]:\n\n[`reference`]: https://example.com\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(
        result.is_empty(),
        "Shortcut reference followed by colon should be recognized"
    );
}

#[test]
fn test_shortcut_reference_with_period() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test shortcut reference followed by period
    let content = "See [reference].\n\n[reference]: https://example.com\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(
        result.is_empty(),
        "Shortcut reference followed by period should be recognized"
    );
}

#[test]
fn test_numeric_footnote_references() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test numeric references (could be footnotes)
    let content = "See note [1] for details.\n\n[1]: https://example.com\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(result.is_empty(), "Numeric references should be recognized");
}

#[test]
fn test_patterns_still_skipped() {
    let rule = MD053LinkImageReferenceDefinitions::default();

    // Alert patterns (GitHub alerts) should still be skipped
    let content = "[!NOTE]\nThis is a note.\n\n[other]: https://example.com\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 1, "Alert patterns should be skipped");

    // Pure punctuation should still be skipped
    let content = "Array[...] notation.\n\n[other]: https://example.com\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert_eq!(result.len(), 1, "Pure punctuation patterns should be skipped");
}

#[test]
fn test_complex_real_world_case() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test a complex case combining multiple features
    let content = r#"# Documentation

## Python Types

The [`typing.Optional`] type is equivalent to [`Union[T, None]`].
See also [`dataclasses.InitVar`]: it marks init-only fields.

For file paths, use [`pathlib.Path`] or [os.path] module.

Single type parameters like [T] are common.

[`typing.optional`]: https://docs.python.org/3/library/typing.html#typing.Optional
[`union[t, none]`]: https://docs.python.org/3/library/typing.html#typing.Union
[`dataclasses.initvar`]: https://docs.python.org/3/library/dataclasses.html#dataclasses.InitVar
[`pathlib.path`]: https://docs.python.org/3/library/pathlib.html
[os.path]: https://docs.python.org/3/library/os.path.html
[t]: https://docs.python.org/3/library/typing.html#type-variables
"#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(
        result.is_empty(),
        "All references in complex real-world case should be recognized: {result:?}"
    );
}

#[test]
fn debug_github_issue_77_case() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Reproduce the exact case reported in GitHub issue #77
    let content = r#"# Test

## Case that reproduces the issue
This is about the [type annotation grammar].

From the Python documentation on [`dataclasses.InitVar`]:

## Definitions
[type annotation grammar]: https://docs.python.org/3/reference/grammar.html
[`dataclasses.InitVar`]: https://docs.python.org/3/library/dataclasses.html#dataclasses.InitVar
"#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);

    println!("\n=== PARSED LINKS ===");
    for (i, link) in ctx.links.iter().enumerate() {
        println!(
            "Link {}: line {}, text='{}', is_reference={}, reference_id={:?}",
            i, link.line, link.text, link.is_reference, link.reference_id
        );
    }

    println!("\n=== REFERENCE DEFINITIONS ===");
    for (i, ref_def) in ctx.reference_defs.iter().enumerate() {
        println!(
            "RefDef {}: line {}, id='{}', url='{}'",
            i, ref_def.line, ref_def.id, ref_def.url
        );
    }

    println!("\n=== MD053 CHECK RESULTS ===");
    let warnings = rule.check(&ctx).unwrap();
    if warnings.is_empty() {
        println!("No unused reference warnings (all references found correctly)");
    } else {
        for warning in &warnings {
            println!("Line {}: {}", warning.line, warning.message);
        }
    }

    // Both references should be found as used
    assert!(
        warnings.is_empty(),
        "Expected no unused references, but found: {:?}",
        warnings.iter().map(|w| &w.message).collect::<Vec<_>>()
    );
}

#[test]
fn test_case_sensitivity_with_backticks() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test case sensitivity issues with backticks
    let content = r#"# Case Sensitivity Test

From the Python documentation on [`dataclasses.InitVar`]:

[`dataclasses.initvar`]: https://docs.python.org/3/library/dataclasses.html#dataclasses.InitVar
"#;

    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let warnings = rule.check(&ctx).unwrap();

    // This should work due to case-insensitive matching
    assert!(
        warnings.is_empty(),
        "Case-insensitive matching should work for backtick references"
    );
}

#[test]
fn test_backtick_reference_with_double_colon_and_comma() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test case from GitHub issue #128: backtick reference with `::` and `, `
    // Previously filtered out because it contains both `:` and space
    let content = "See [`Bound<'_, PyAny>::is_callable`] function.\n\n[`Bound<'_, PyAny>::is_callable`]: foo\n";
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(
        result.is_empty(),
        "Backtick references with :: and comma should not be filtered out (GitHub issue #128)"
    );
}

#[test]
fn test_backtick_reference_in_list_continuation() {
    let rule = MD053LinkImageReferenceDefinitions::default();
    // Test case from GitHub issue #128 follow-up: backtick reference in list item continuation
    // The reference usage is in indented list content (4 spaces)
    let content = r#"- `__richcmp__(<self>, object, pyo3::basic::CompareOp) -> object`

    Implements Python comparison operations.
    You can use [`CompareOp::matches`] to adapt.

[`CompareOp::matches`]: https://example.com
"#;
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = rule.check(&ctx).unwrap();
    assert!(
        result.is_empty(),
        "Backtick references in list item continuations should be detected (GitHub issue #128 follow-up)"
    );
}