mdbook-lint 0.2.0

A fast markdown linter for mdBook
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
//! MD059 - Link text should be descriptive
//!
//! This rule is triggered when a link has generic text that doesn't describe
//! the purpose of the link.
//!
//! ## Correct
//!
//! ```markdown
//! \[Download the budget document\](document.pdf)
//! \[CommonMark Specification\](https://spec.commonmark.org/)
//! ```
//!
//! ## Incorrect
//!
//! ```markdown
//! \[click here\](document.pdf)
//! \[here\](https://example.com)
//! \[link\](https://example.com)
//! \[more\](https://example.com)
//! ```

use crate::error::Result;
use crate::{
    Document, Violation,
    rule::{Rule, RuleCategory, RuleMetadata},
    violation::Severity,
};
use comrak::nodes::{AstNode, NodeValue};

/// MD059 - Link text should be descriptive
pub struct MD059 {
    prohibited_texts: Vec<String>,
}

impl Default for MD059 {
    fn default() -> Self {
        Self::new()
    }
}

impl MD059 {
    /// Create a new MD059 rule instance
    pub fn new() -> Self {
        Self {
            prohibited_texts: vec![
                "click here".to_string(),
                "here".to_string(),
                "link".to_string(),
                "more".to_string(),
            ],
        }
    }

    /// Set the list of prohibited link texts
    #[allow(dead_code)]
    pub fn prohibited_texts(mut self, texts: Vec<String>) -> Self {
        self.prohibited_texts = texts;
        self
    }

    /// Extract text content from a link node
    fn extract_link_text<'a>(node: &'a AstNode<'a>) -> String {
        let mut text = String::new();
        for child in node.children() {
            match &child.data.borrow().value {
                NodeValue::Text(t) => text.push_str(t),
                NodeValue::Code(code) => text.push_str(&code.literal),
                NodeValue::Emph | NodeValue::Strong => {
                    text.push_str(&Self::extract_link_text(child));
                }
                _ => {}
            }
        }
        text.trim().to_string()
    }

    /// Check if link text is prohibited
    fn is_prohibited_text(&self, text: &str) -> bool {
        let normalized_text = text.to_lowercase();
        self.prohibited_texts
            .iter()
            .any(|prohibited| prohibited.to_lowercase() == normalized_text)
    }

    /// Check for non-descriptive link text
    fn check_link_text<'a>(&self, ast: &'a AstNode<'a>) -> Vec<Violation> {
        let mut violations = Vec::new();
        self.traverse_for_links(ast, &mut violations);
        violations
    }

    /// Traverse AST to find links
    fn traverse_for_links<'a>(&self, node: &'a AstNode<'a>, violations: &mut Vec<Violation>) {
        if let NodeValue::Link(link) = &node.data.borrow().value {
            // Skip autolinks and reference definitions
            if !link.url.is_empty() {
                let link_text = Self::extract_link_text(node);

                // Skip empty link text
                if !link_text.is_empty() && self.is_prohibited_text(&link_text) {
                    let pos = node.data.borrow().sourcepos;
                    let line = pos.start.line;
                    let column = pos.start.column;
                    violations.push(self.create_violation(
                        format!(
                            "Link text '{link_text}' is not descriptive. Use descriptive text that explains the purpose of the link"
                        ),
                        line,
                        column,
                        Severity::Warning,
                    ));
                }
            }
        }

        for child in node.children() {
            self.traverse_for_links(child, violations);
        }
    }

    /// Fallback method using manual parsing when no AST is available
    fn check_link_text_fallback(&self, document: &Document) -> Vec<Violation> {
        let mut violations = Vec::new();

        for (line_num, line) in document.content.lines().enumerate() {
            let line_number = line_num + 1;
            let mut chars = line.char_indices().peekable();
            let mut in_backticks = false;

            while let Some((i, ch)) = chars.next() {
                match ch {
                    '`' => {
                        in_backticks = !in_backticks;
                    }
                    '[' if !in_backticks => {
                        // Try to parse any kind of link: [text](url) or [text][ref]
                        if let Some((link_text, text_start, text_end)) =
                            self.parse_any_link_at(&line[i..])
                        {
                            let cleaned_text = Self::strip_emphasis_markers(link_text);
                            let trimmed_text = cleaned_text.trim();

                            if !trimmed_text.is_empty() && self.is_prohibited_text(trimmed_text) {
                                violations.push(self.create_violation(
                                    format!(
                                        "Link text '{trimmed_text}' is not descriptive. Use descriptive text that explains the purpose of the link"
                                    ),
                                    line_number,
                                    i + text_start + 2, // +1 for 1-based indexing, +1 for opening bracket
                                    Severity::Warning,
                                ));
                            }

                            // Skip past the entire link
                            for _ in 0..text_end - 1 {
                                chars.next();
                            }
                        }
                    }
                    _ => {}
                }
            }
        }

        violations
    }

    /// Parse any link (inline or reference) starting at the given position
    /// Returns (link_text, text_start_offset, total_length) if found
    fn parse_any_link_at<'a>(&self, text: &'a str) -> Option<(&'a str, usize, usize)> {
        if !text.starts_with('[') {
            return None;
        }

        // Find the closing bracket
        let mut bracket_count = 0;
        let mut closing_bracket_pos = None;

        for (i, ch) in text.char_indices() {
            match ch {
                '[' => bracket_count += 1,
                ']' => {
                    bracket_count -= 1;
                    if bracket_count == 0 {
                        closing_bracket_pos = Some(i);
                        break;
                    }
                }
                _ => {}
            }
        }

        let closing_bracket_pos = closing_bracket_pos?;
        let link_text = &text[1..closing_bracket_pos];
        let remaining = &text[closing_bracket_pos + 1..];

        // Check if this is followed by (url) - inline link
        if remaining.starts_with('(') {
            if let Some(closing_paren) = remaining.find(')') {
                let total_length = closing_bracket_pos + 1 + closing_paren + 1;
                return Some((link_text, 0, total_length));
            }
        }
        // Check if this is followed by [ref] - reference link
        else if remaining.starts_with('[') {
            if let Some(ref_end) = remaining.find(']') {
                let total_length = closing_bracket_pos + 1 + ref_end + 1;
                return Some((link_text, 0, total_length));
            }
        }

        None
    }

    /// Strip emphasis markers from link text (similar to AST extract_link_text)
    fn strip_emphasis_markers(text: &str) -> String {
        let mut result = String::new();
        let mut chars = text.chars().peekable();

        while let Some(ch) = chars.next() {
            match ch {
                '*' => {
                    // Check for ** (strong) or * (emphasis)
                    if chars.peek() == Some(&'*') {
                        chars.next(); // consume second *
                        // Find closing **
                        let mut temp = String::new();
                        let mut found_closing = false;
                        while let Some(inner_ch) = chars.next() {
                            if inner_ch == '*' && chars.peek() == Some(&'*') {
                                chars.next(); // consume second *
                                found_closing = true;
                                break;
                            }
                            temp.push(inner_ch);
                        }
                        if found_closing {
                            result.push_str(&Self::strip_emphasis_markers(&temp));
                        } else {
                            result.push_str("**");
                            result.push_str(&temp);
                        }
                    } else {
                        // Find closing *
                        let mut temp = String::new();
                        let mut found_closing = false;
                        for inner_ch in chars.by_ref() {
                            if inner_ch == '*' {
                                found_closing = true;
                                break;
                            }
                            temp.push(inner_ch);
                        }
                        if found_closing {
                            result.push_str(&Self::strip_emphasis_markers(&temp));
                        } else {
                            result.push('*');
                            result.push_str(&temp);
                        }
                    }
                }
                '`' => {
                    // Find closing `
                    let mut temp = String::new();
                    let mut found_closing = false;
                    for inner_ch in chars.by_ref() {
                        if inner_ch == '`' {
                            found_closing = true;
                            break;
                        }
                        temp.push(inner_ch);
                    }
                    if found_closing {
                        result.push_str(&temp); // Code content as-is
                    } else {
                        result.push('`');
                        result.push_str(&temp);
                    }
                }
                _ => result.push(ch),
            }
        }

        result
    }
}

impl Rule for MD059 {
    fn id(&self) -> &'static str {
        "MD059"
    }

    fn name(&self) -> &'static str {
        "descriptive-link-text"
    }

    fn description(&self) -> &'static str {
        "Link text should be descriptive"
    }

    fn metadata(&self) -> RuleMetadata {
        RuleMetadata::stable(RuleCategory::Accessibility)
    }

    fn check_with_ast<'a>(
        &self,
        document: &Document,
        ast: Option<&'a AstNode<'a>>,
    ) -> Result<Vec<Violation>> {
        if let Some(ast) = ast {
            let violations = self.check_link_text(ast);
            Ok(violations)
        } else {
            // Simplified regex-based fallback when no AST is available
            Ok(self.check_link_text_fallback(document))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_helpers::{
        assert_no_violations, assert_single_violation, assert_violation_count,
    };

    #[test]
    fn test_descriptive_link_text() {
        let content = r#"[Download the budget document](document.pdf)
[CommonMark Specification](https://spec.commonmark.org/)
[View the installation guide](install.md)
"#;

        assert_no_violations(MD059::new(), content);
    }

    #[test]
    fn test_prohibited_link_text() {
        let content = r#"[click here](document.pdf)
[here](https://example.com)
[link](https://example.com)
[more](info.html)
"#;

        let violations = assert_violation_count(MD059::new(), content, 4);

        assert_eq!(violations[0].line, 1);
        assert!(violations[0].message.contains("click here"));

        assert_eq!(violations[1].line, 2);
        assert!(violations[1].message.contains("here"));

        assert_eq!(violations[2].line, 3);
        assert!(violations[2].message.contains("link"));

        assert_eq!(violations[3].line, 4);
        assert!(violations[3].message.contains("more"));
    }

    #[test]
    fn test_case_insensitive_matching() {
        let content = r#"[CLICK HERE](document.pdf)
[Here](https://example.com)
[Link](https://example.com)
[MORE](info.html)
"#;

        let violations = assert_violation_count(MD059::new(), content, 4);
        assert_eq!(violations[0].line, 1);
        assert_eq!(violations[1].line, 2);
        assert_eq!(violations[2].line, 3);
        assert_eq!(violations[3].line, 4);
    }

    #[test]
    fn test_custom_prohibited_texts() {
        let content = r#"[read more](document.pdf)
[see details](https://example.com)
"#;

        let rule =
            MD059::new().prohibited_texts(vec!["read more".to_string(), "see details".to_string()]);
        let violations = assert_violation_count(rule, content, 2);
        assert_eq!(violations[0].line, 1);
        assert_eq!(violations[1].line, 2);
    }

    #[test]
    fn test_autolinks_ignored() {
        let content = r#"<https://example.com>
<mailto:user@example.com>
"#;

        assert_no_violations(MD059::new(), content);
    }

    #[test]
    fn test_reference_links() {
        let content = r#"[click here][ref]
[descriptive text][ref2]

[ref]: https://example.com
[ref2]: https://example.com
"#;

        let violation = assert_single_violation(MD059::new(), content);
        assert_eq!(violation.line, 1);
        assert!(violation.message.contains("click here"));
    }

    #[test]
    fn test_links_with_emphasis() {
        let content = r#"[**click here**](document.pdf)
[*here*](https://example.com)
[`code link`](https://example.com)
"#;

        let violations = assert_violation_count(MD059::new(), content, 2);

        assert_eq!(violations[0].line, 1);
        assert!(violations[0].message.contains("click here"));

        assert_eq!(violations[1].line, 2);
        assert!(violations[1].message.contains("here"));
    }

    #[test]
    fn test_empty_link_text_ignored() {
        let content = r#"[](https://example.com)
"#;

        assert_no_violations(MD059::new(), content);
    }

    #[test]
    fn test_mixed_content() {
        let content = r#"[Download guide](guide.pdf) contains useful information.
You can [click here](more.html) for additional details.
See the [API documentation](api.md) for technical details.
"#;

        let violation = assert_single_violation(MD059::new(), content);
        assert_eq!(violation.line, 2);
        assert!(violation.message.contains("click here"));
    }
}