mdbook-lint-rulesets 0.16.1

Modular rulesets for mdbook-lint - standard and mdBook-specific linting rules
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
//! CONTENT002: Placeholder text detection
//!
//! Detects common placeholder text patterns like "Lorem ipsum", "TBD",
//! "coming soon", etc. that shouldn't appear in production documentation.

use mdbook_lint_core::Document;
use mdbook_lint_core::rule::{Rule, RuleCategory, RuleMetadata};
use mdbook_lint_core::violation::{Severity, Violation};
use regex::Regex;
use std::sync::LazyLock;

/// Regex patterns for placeholder text (case-insensitive)
static PLACEHOLDER_PATTERNS: LazyLock<Vec<(Regex, &'static str)>> = LazyLock::new(|| {
    vec![
        // Lorem ipsum variants
        (
            Regex::new(r"(?i)\blorem\s+ipsum\b").unwrap(),
            "Lorem ipsum placeholder text",
        ),
        (
            Regex::new(r"(?i)\bipsum\s+dolor\b").unwrap(),
            "Lorem ipsum placeholder text",
        ),
        // Status placeholders
        (
            Regex::new(r"(?i)\bTBD\b").unwrap(),
            "TBD (To Be Determined) placeholder",
        ),
        (
            Regex::new(r"(?i)\bTBA\b").unwrap(),
            "TBA (To Be Announced) placeholder",
        ),
        (
            Regex::new(r"(?i)\bTBC\b").unwrap(),
            "TBC (To Be Confirmed) placeholder",
        ),
        // Coming soon variants
        (
            Regex::new(r"(?i)\bcoming\s+soon\b").unwrap(),
            "Coming soon placeholder",
        ),
        (
            Regex::new(r"(?i)\bunder\s+construction\b").unwrap(),
            "Under construction placeholder",
        ),
        (
            Regex::new(r"(?i)\bwork\s+in\s+progress\b").unwrap(),
            "Work in progress placeholder",
        ),
        // Insert/add placeholders
        (
            Regex::new(r"(?i)\binsert\s+\w+\s+here\b").unwrap(),
            "Insert here placeholder",
        ),
        (
            Regex::new(r"(?i)\badd\s+\w+\s+here\b").unwrap(),
            "Add here placeholder",
        ),
        (
            Regex::new(r"(?i)\bput\s+\w+\s+here\b").unwrap(),
            "Put here placeholder",
        ),
        // N/A and placeholder
        (
            Regex::new(r"(?i)\bN/A\b").unwrap(),
            "N/A placeholder - consider removing or providing actual content",
        ),
        (
            Regex::new(r"(?i)\bplaceholder\b").unwrap(),
            "Placeholder text detected",
        ),
        // Draft/pending
        (Regex::new(r"(?i)\[draft\]").unwrap(), "Draft marker found"),
        (
            Regex::new(r"(?i)\[pending\]").unwrap(),
            "Pending marker found",
        ),
        // Example placeholders
        (
            Regex::new(r"(?i)\bexample\.com\b").unwrap(),
            "Example.com placeholder URL",
        ),
        (
            Regex::new(r"(?i)\bfoo\s*bar\s*baz\b").unwrap(),
            "Foo bar baz placeholder",
        ),
        // Empty section markers
        (
            Regex::new(r"(?i)\bthis\s+section\s+(is\s+)?(empty|blank|incomplete)\b").unwrap(),
            "Empty section marker",
        ),
        (
            Regex::new(r"(?i)\bcontent\s+goes\s+here\b").unwrap(),
            "Content placeholder",
        ),
        // Your/my name placeholders
        (
            Regex::new(r"(?i)\byour\s+name\s+here\b").unwrap(),
            "Name placeholder",
        ),
        (
            Regex::new(r"(?i)\b<your[_\s]name>\b").unwrap(),
            "Name placeholder",
        ),
        // XXX as content (not code comment - that's CONTENT001)
        (
            Regex::new(r"(?i)^XXX+$").unwrap(),
            "XXX placeholder content",
        ),
        // Ellipsis as placeholder content (standalone)
        (
            Regex::new(r"^\s*\.\.\.\s*$").unwrap(),
            "Ellipsis placeholder - provide actual content",
        ),
    ]
});

/// CONTENT002: Detects placeholder text
///
/// This rule flags common placeholder patterns that indicate
/// incomplete documentation. These should be replaced with
/// actual content before publishing.
pub struct CONTENT002 {
    /// Whether to check inside code blocks
    check_code_blocks: bool,
    /// Whether to allow example.com in examples
    allow_example_urls: bool,
}

impl Default for CONTENT002 {
    fn default() -> Self {
        Self {
            check_code_blocks: false,
            allow_example_urls: true, // example.com is acceptable in code examples
        }
    }
}

impl CONTENT002 {
    /// Create an instance from rule configuration.
    ///
    /// Recognized keys (both `snake_case` and `kebab-case` accepted):
    /// - `check_code_blocks`: scan inside code blocks (default false).
    /// - `allow_example_urls`: allow `example.com` URLs in examples (default true).
    pub fn from_config(config: &toml::Value) -> Self {
        let mut rule = Self::default();
        if let Some(b) = config
            .get("check_code_blocks")
            .or_else(|| config.get("check-code-blocks"))
            .and_then(|v| v.as_bool())
        {
            rule.check_code_blocks = b;
        }
        if let Some(b) = config
            .get("allow_example_urls")
            .or_else(|| config.get("allow-example-urls"))
            .and_then(|v| v.as_bool())
        {
            rule.allow_example_urls = b;
        }
        rule
    }

    /// Set whether to check inside code blocks
    #[allow(dead_code)]
    pub fn check_code_blocks(mut self, check: bool) -> Self {
        self.check_code_blocks = check;
        self
    }

    /// Set whether to allow example.com URLs
    #[allow(dead_code)]
    pub fn allow_example_urls(mut self, allow: bool) -> Self {
        self.allow_example_urls = allow;
        self
    }

    /// Check if a position is inside a code block
    fn is_in_code_block(&self, lines: &[String], line_idx: usize) -> bool {
        let mut in_fenced_block = false;

        for (idx, line) in lines.iter().enumerate() {
            let trimmed = line.trim();

            // Check for fenced code block markers
            if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
                in_fenced_block = !in_fenced_block;
            }

            if idx == line_idx {
                return in_fenced_block;
            }
        }

        false
    }

    /// Check if a position is inside inline code
    fn is_in_inline_code(&self, line: &str, col: usize) -> bool {
        let before = &line[..col.min(line.len())];

        // Count backticks before the position
        let backtick_count = before.chars().filter(|&c| c == '`').count();

        // Odd number of backticks means we're inside inline code
        backtick_count % 2 == 1
    }
}

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

    fn name(&self) -> &'static str {
        "no-placeholder-text"
    }

    fn description(&self) -> &'static str {
        "Placeholder text should be replaced with actual content"
    }

    fn metadata(&self) -> RuleMetadata {
        RuleMetadata::stable(RuleCategory::Content).introduced_in("mdbook-lint v0.12.0")
    }

    fn check_with_ast<'a>(
        &self,
        document: &Document,
        _ast: Option<&'a comrak::nodes::AstNode<'a>>,
    ) -> mdbook_lint_core::error::Result<Vec<Violation>> {
        let mut violations = Vec::new();

        for (line_idx, line) in document.lines.iter().enumerate() {
            let line_num = line_idx + 1; // 1-based

            // Skip code blocks unless configured to check them
            if !self.check_code_blocks && self.is_in_code_block(&document.lines, line_idx) {
                continue;
            }

            // Check each pattern
            for (pattern, description) in PLACEHOLDER_PATTERNS.iter() {
                // Skip example.com check if allowed
                if self.allow_example_urls && *description == "Example.com placeholder URL" {
                    // Only flag if not in a code context
                    if self.is_in_code_block(&document.lines, line_idx) {
                        continue;
                    }
                }

                for mat in pattern.find_iter(line) {
                    let col = mat.start() + 1; // 1-based

                    // Skip if inside inline code (unless checking code blocks)
                    if !self.check_code_blocks && self.is_in_inline_code(line, mat.start()) {
                        continue;
                    }

                    violations.push(self.create_violation(
                        format!("{} - replace with actual content", description),
                        line_num,
                        col,
                        Severity::Warning,
                    ));
                }
            }
        }

        Ok(violations)
    }
}

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

    fn create_test_document(content: &str) -> Document {
        Document::new(content.to_string(), PathBuf::from("test.md")).unwrap()
    }

    #[test]
    fn test_no_placeholders() {
        let content = "# Title\n\nThis is real, complete documentation.";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 0);
    }

    #[test]
    fn test_lorem_ipsum_detected() {
        let content = "# Title\n\nLorem ipsum dolor sit amet.";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert!(!violations.is_empty());
        assert!(violations[0].message.contains("Lorem ipsum"));
    }

    #[test]
    fn test_tbd_detected() {
        let content = "# Title\n\nThis feature is TBD.";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("TBD"));
    }

    #[test]
    fn test_coming_soon_detected() {
        let content = "# Title\n\nThis section is coming soon.";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("Coming soon"));
    }

    #[test]
    fn test_under_construction_detected() {
        let content = "# Title\n\nThis page is under construction.";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("Under construction"));
    }

    #[test]
    fn test_insert_here_detected() {
        let content = "# Title\n\nInsert content here.";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("Insert here"));
    }

    #[test]
    fn test_placeholder_word_detected() {
        let content = "# Title\n\nThis is placeholder text.";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("Placeholder"));
    }

    #[test]
    fn test_draft_marker_detected() {
        let content = "# Title [draft]\n\nContent here.";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("Draft"));
    }

    #[test]
    fn test_case_insensitive() {
        let content = "# Title\n\nCOMING SOON\ncoming Soon\nComing soon";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 3);
    }

    #[test]
    fn test_skip_code_blocks() {
        let content = "# Title\n\n```\nLorem ipsum in code\n```\n\nLorem ipsum outside";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].line, 7);
    }

    #[test]
    fn test_skip_inline_code() {
        let content = "# Title\n\nUse `TBD` as status.\n\nActual TBD here";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        // Only line 5 should be flagged - line 3 has TBD in inline code
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].line, 5);
        assert!(violations[0].message.contains("TBD"));
    }

    #[test]
    fn test_ellipsis_placeholder() {
        let content = "# Title\n\n...\n\nActual content.";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("Ellipsis"));
    }

    #[test]
    fn test_na_detected() {
        let content = "# Title\n\nDescription: N/A";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("N/A"));
    }

    #[test]
    fn test_content_goes_here() {
        let content = "# Title\n\nContent goes here.";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 1);
    }

    #[test]
    fn test_example_com_allowed_by_default() {
        let content = "# Title\n\nVisit https://example.com for more.";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        // Should still detect since it's not in code block
        assert_eq!(violations.len(), 1);
    }

    #[test]
    fn test_your_name_here() {
        let content = "# Title\n\nAuthor: Your name here";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("Name placeholder"));
    }

    #[test]
    fn test_work_in_progress() {
        let content = "# Title\n\nThis section is a work in progress.";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("Work in progress"));
    }

    #[test]
    fn test_multiple_placeholders() {
        let content = "# Title\n\nTBD\n\nLorem ipsum dolor\n\nComing soon";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert!(violations.len() >= 3);
    }

    #[test]
    fn test_empty_section_marker() {
        let content = "# Title\n\nThis section is empty.";
        let doc = create_test_document(content);
        let rule = CONTENT002::default();
        let violations = rule.check(&doc).unwrap();
        assert_eq!(violations.len(), 1);
        assert!(violations[0].message.contains("Empty section"));
    }
}