rumdl 0.2.44

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
/// Integration tests for skip context detection across MD011, MD037, and MD052
///
/// These tests verify that the rules properly skip various markdown contexts
/// including HTML comments, math blocks, inline math, tables, and front matter.
use rumdl_lib::lint_context::LintContext;
use rumdl_lib::rule::Rule;
use rumdl_lib::rules::{MD011NoReversedLinks, MD037NoSpaceInEmphasis, MD052ReferenceLinkImages};

#[test]
fn test_md037_skips_html_comments() {
    let rule = MD037NoSpaceInEmphasis;

    // Test that emphasis markers inside HTML comments are not flagged
    let content = r#"# Test MD037 with HTML Comments

Regular text with * spaces * that should be flagged.

<!-- This has * spaces * inside a comment and should NOT be flagged -->

More text with * another issue * here.

<!--
Multi-line comment with
* spaced emphasis *
should also be ignored
-->

Final * test * outside comments."#;

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

    // Should flag exactly 3 issues (outside HTML comments)
    assert_eq!(result.len(), 3, "Expected 3 warnings for emphasis outside comments");

    // Verify the warnings are for the correct lines
    let lines_with_issues: Vec<usize> = result.iter().map(|w| w.line).collect();
    assert!(lines_with_issues.contains(&3), "Should flag line 3");
    assert!(lines_with_issues.contains(&7), "Should flag line 7");
    assert!(lines_with_issues.contains(&15), "Should flag line 15");
}

#[test]
fn test_md037_skips_math_contexts() {
    let rule = MD037NoSpaceInEmphasis;

    // Test that emphasis markers inside math blocks and inline math are not flagged
    let content = r#"# Test MD037 with Math Contexts

Regular text with * spaces * that should be flagged.

$$
This is a math block with * asterisks * that should NOT be flagged.
They might represent multiplication: a * b * c
$$

Inline math $a * b * c$ should also not be flagged.

Double dollar inline math $$x * y * z$$ should not be flagged.

But this * spaced emphasis * outside math should be flagged."#;

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

    // Should flag exactly 2 issues (outside math contexts)
    assert_eq!(result.len(), 2, "Expected 2 warnings for emphasis outside math");

    // Verify the warnings are for the correct lines
    let lines_with_issues: Vec<usize> = result.iter().map(|w| w.line).collect();
    assert!(lines_with_issues.contains(&3), "Should flag line 3");
    assert!(lines_with_issues.contains(&14), "Should flag line 14");
}

#[test]
fn test_md052_skips_html_comments() {
    let rule = MD052ReferenceLinkImages::new();

    // Test that reference links inside HTML comments are not flagged
    let content = r#"# Test MD052 with HTML Comments

Regular [undefined][ref1] reference that should be flagged.

<!-- This [hidden][ref2] reference should NOT be flagged -->

Another [missing][ref3] reference outside comments.

<!--
Multi-line comment with
[ignored][ref4] reference
and [another][ref5] one
-->

<!-- Complex patterns like [1:] from issue #20 should not be flagged -->"#;

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

    // Should flag exactly 2 undefined references (outside HTML comments)
    assert_eq!(result.len(), 2, "Expected 2 warnings for references outside comments");

    // Verify the correct references are flagged
    let messages: Vec<String> = result.iter().map(|w| w.message.clone()).collect();
    assert!(messages.iter().any(|m| m.contains("ref1")), "Should flag ref1");
    assert!(messages.iter().any(|m| m.contains("ref3")), "Should flag ref3");

    // Should NOT flag references inside comments
    assert!(
        !messages.iter().any(|m| m.contains("ref2")),
        "Should not flag ref2 in comment"
    );
    assert!(
        !messages.iter().any(|m| m.contains("ref4")),
        "Should not flag ref4 in comment"
    );
    assert!(
        !messages.iter().any(|m| m.contains("ref5")),
        "Should not flag ref5 in comment"
    );
}

#[test]
fn test_md052_skips_math_contexts() {
    let rule = MD052ReferenceLinkImages::new();

    // Test that reference-like patterns in math are not flagged
    // Using full reference syntax [text][ref] since shortcut_syntax is false by default
    let content = r#"# Test MD052 with Math

Regular [text][undefined_ref] reference that should be flagged.

$$
This is a math block with array notation [0] and [1] that should NOT be flagged.
Matrix element M[i][j] should also be ignored.
$$

Inline math with array $a[0]$ and matrix $M[i][j]$ should not be flagged.

Double dollar inline $$f[x]$$ should not be flagged.

But this [link][missing_ref] reference outside math should be flagged."#;

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

    // Should flag exactly 2 undefined references (outside math contexts)
    assert_eq!(result.len(), 2, "Expected 2 warnings for references outside math");

    // Verify the correct references are flagged
    let messages: Vec<String> = result.iter().map(|w| w.message.clone()).collect();
    assert!(
        messages.iter().any(|m| m.contains("undefined_ref")),
        "Should flag 'undefined_ref'"
    );
    assert!(
        messages.iter().any(|m| m.contains("missing_ref")),
        "Should flag 'missing_ref'"
    );
}

#[test]
fn test_md052_checks_references_inside_tables() {
    let rule = MD052ReferenceLinkImages::new();

    // An undefined reference is undefined wherever it appears; a table cell is an
    // ordinary inline context. markdownlint reports every one of these too.
    // Using full reference syntax [text][ref] since shortcut_syntax is false by default
    let content = r#"# Test MD052 with Tables

Regular [link][undefined_ref] reference that should be flagged.

| Header | Column |
|--------|--------|
| Cell with [ref1][alpha] | Another [ref2][beta] |
| More [ref3][gamma] data | Final [ref4][delta] cell |

This [link2][missing_ref] reference outside the table should be flagged.

Another table:

| Col 1 | Col 2 | Col 3 |
|-------|-------|-------|
| [a][eps] | [b][zeta] | [c][eta] |

Final [link3][broken_ref] reference should be flagged."#;

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

    let messages: Vec<String> = result.iter().map(|w| w.message.clone()).collect();
    for label in [
        "undefined_ref",
        "alpha",
        "beta",
        "gamma",
        "delta",
        "missing_ref",
        "eps",
        "zeta",
        "eta",
        "broken_ref",
    ] {
        assert!(
            messages.iter().any(|m| m.contains(&format!("'{label}'"))),
            "Should flag '{label}', got: {messages:?}"
        );
    }
    assert_eq!(result.len(), 10, "Expected 10 warnings, got: {messages:?}");
}

#[test]
fn test_md011_skips_html_comments() {
    let rule = MD011NoReversedLinks;

    // Test that reversed link patterns inside HTML comments are not flagged
    let content = r#"# Test MD011 with HTML Comments

Regular (https://example.com)[reversed link] that should be flagged.

<!-- This (https://hidden.com)[in comment] should NOT be flagged -->

Another (https://test.com)[reversed] link outside comments.

<!--
Multi-line comment with
(https://ignored.com)[reversed syntax]
should also be ignored
-->"#;

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

    // Should flag exactly 2 reversed links (outside HTML comments)
    assert_eq!(
        result.len(),
        2,
        "Expected 2 warnings for reversed links outside comments"
    );

    // Verify the warnings are for the correct lines
    let lines_with_issues: Vec<usize> = result.iter().map(|w| w.line).collect();
    assert!(lines_with_issues.contains(&3), "Should flag line 3");
    assert!(lines_with_issues.contains(&7), "Should flag line 7");
}

#[test]
fn test_md011_skips_math_contexts() {
    let rule = MD011NoReversedLinks;

    // A line-start `$$ ... $$` block and single-`$` inline math are math, so
    // reversed-link-looking patterns inside them are skipped. A mid-line
    // `$$...$$` is a literal under the shared math model (math-ness of `$$`
    // is decided solely by line-start position, matching `math_block_ranges`),
    // so its `(y)[j]` is flagged like any other reversed link.
    let content = r#"# Test MD011 with Math

Regular (https://example.com)[reversed link] that should be flagged.

$$
Function notation f(x)[0] should NOT be flagged.
Array access pattern (arr)[index] should be ignored.
$$

Inline math $f(x)[i]$ should not be flagged.

Double dollar inline $$g(y)[j]$$ is a mid-line literal and IS flagged.

But this (https://test.com)[reversed] outside math should be flagged."#;

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

    // Line-start block (5-8) and inline `$...$` (10) are math; the mid-line
    // `$$g(y)[j]$$` (12) is not, so three reversed links are flagged.
    assert_eq!(
        result.len(),
        3,
        "Expected 3 warnings: lines 3, 12, 14 (mid-line $$...$$ is not math): {result:?}"
    );

    let lines_with_issues: Vec<usize> = result.iter().map(|w| w.line).collect();
    assert!(lines_with_issues.contains(&3), "Should flag line 3");
    assert!(
        lines_with_issues.contains(&12),
        "Should flag the mid-line $$...$$ literal on line 12"
    );
    assert!(lines_with_issues.contains(&14), "Should flag line 14");
}

#[test]
fn test_md011_skips_front_matter() {
    let rule = MD011NoReversedLinks;

    // Test that patterns in front matter are not flagged
    let content = r#"---
title: "My Post"
tags: ["test", "example"]
description: "Pattern (like)[this] in frontmatter"
---

# Content

Regular (https://example.com)[reversed link] that should be flagged.

+++
title = "TOML frontmatter"
tags = ["more", "tags"]
pattern = "(toml)[pattern]"
+++

# More Content

Another (https://test.com)[reversed] link should be flagged."#;

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

    // Should flag exactly 3 reversed links (outside front matter)
    // Note: The TOML block at lines 11-15 is NOT front matter (not at beginning),
    // so (toml)[pattern] on line 14 should be flagged
    assert_eq!(
        result.len(),
        3,
        "Expected 3 warnings for reversed links outside front matter"
    );

    // Verify the warnings are for the correct lines
    let lines_with_issues: Vec<usize> = result.iter().map(|w| w.line).collect();
    assert!(lines_with_issues.contains(&9), "Should flag line 9");
    assert!(
        lines_with_issues.contains(&14),
        "Should flag line 14 (TOML block is not front matter)"
    );
    assert!(lines_with_issues.contains(&19), "Should flag line 19");
}

#[test]
fn test_combined_skip_contexts() {
    // Test that multiple skip contexts work together correctly
    // Using full reference syntax [text][ref] since shortcut_syntax is false by default
    let content = r#"---
frontmatter: "with (pattern)[like] this"
---

# Test Document

Regular * emphasis with spaces * should be flagged.

<!-- HTML comment with * spaces * and [link][undefined] reference -->

$$
Math block with * asterisks * and [array][notation]
$$

Inline math $f(x) * g(x)$ and $a[i]$ should be skipped.

| Table | Header |
|-------|--------|
| * spaces * | [ref][x] |

Outside contexts: * spaced * emphasis and [link][missing_ref] reference and (https://example.com)[reversed] link."#;

    // Test MD037
    let md037 = MD037NoSpaceInEmphasis;
    let ctx = LintContext::new(content, rumdl_lib::config::MarkdownFlavor::Standard, None);
    let result = md037.check(&ctx).unwrap();
    // Front matter, the HTML comment and both math contexts are skipped; the table
    // row is not a skip context, so its `* spaces *` cell is flagged like any other.
    assert_eq!(result.len(), 3, "MD037: Expected 3 warnings outside skip contexts");

    // Test MD052
    let md052 = MD052ReferenceLinkImages::new();
    let result = md052.check(&ctx).unwrap();
    assert_eq!(
        result.len(),
        2,
        "MD052: Expected warnings for 'x' (in the table) and 'missing_ref'"
    );

    // Test MD011
    let md011 = MD011NoReversedLinks;
    let result = md011.check(&ctx).unwrap();
    assert_eq!(result.len(), 1, "MD011: Expected 1 warning for reversed link");
}

#[test]
fn test_nested_contexts() {
    // Test that nested contexts (e.g., inline code in HTML comments) work correctly
    let content = r#"# Nested Contexts Test

<!-- Comment with `inline code containing * spaces *` should be skipped entirely -->

Math with inline code: $$`array[0]` is inline code in math$$

Regular * spaces * outside all contexts should be flagged."#;

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

    // Should only flag the last line
    assert_eq!(
        result.len(),
        1,
        "Expected only 1 warning for emphasis outside all contexts"
    );
    assert_eq!(result[0].line, 7, "Should flag line 7");
}