mdbook-lint 0.2.0

A fast markdown linter for mdBook
Documentation
use crate::Document;
use crate::error::Result;
use crate::rule::{Rule, RuleCategory, RuleMetadata};
use crate::violation::{Severity, Violation};

/// MD038 - Spaces inside code span elements
pub struct MD038;

impl MD038 {
    fn find_code_span_violations(&self, line: &str, line_number: usize) -> Vec<Violation> {
        let mut violations = Vec::new();
        let chars: Vec<char> = line.chars().collect();
        let len = chars.len();

        let mut i = 0;
        while i < len {
            if chars[i] == '`' {
                // Count consecutive backticks
                let mut backtick_count = 0;
                let start = i;
                while i < len && chars[i] == '`' {
                    backtick_count += 1;
                    i += 1;
                }

                // Find the closing backticks
                if let Some(end_start) = self.find_closing_backticks(&chars, i, backtick_count) {
                    let content_start = start + backtick_count;
                    let content_end = end_start;

                    if content_start < content_end {
                        let content = &chars[content_start..content_end];

                        // Check for violations
                        if self.has_unnecessary_spaces(content) {
                            violations.push(self.create_violation(
                                "Spaces inside code span elements".to_string(),
                                line_number,
                                start + 1, // Convert to 1-based column
                                Severity::Warning,
                            ));
                        }
                    }

                    i = end_start + backtick_count;
                } else {
                    // No matching closing backticks found, move on
                    break;
                }
            } else {
                i += 1;
            }
        }

        violations
    }

    fn find_closing_backticks(&self, chars: &[char], start: usize, count: usize) -> Option<usize> {
        let mut i = start;
        while i + count <= chars.len() {
            if chars[i] == '`' {
                let mut consecutive = 0;
                let mut j = i;
                while j < chars.len() && chars[j] == '`' {
                    consecutive += 1;
                    j += 1;
                }

                if consecutive == count {
                    return Some(i);
                }

                i = j;
            } else {
                i += 1;
            }
        }
        None
    }

    fn has_unnecessary_spaces(&self, content: &[char]) -> bool {
        if content.is_empty() {
            return false;
        }

        // Check for spaces-only content (this is allowed)
        if content.iter().all(|&c| c.is_whitespace()) {
            return false;
        }

        // Check for special case: content that contains backticks
        // In this case, single leading and trailing spaces are allowed and required
        let content_str: String = content.iter().collect();
        if content_str.contains('`') {
            // For backtick-containing content, spaces are required and allowed
            return false;
        }

        // Check for unnecessary leading space
        let has_leading_space = content[0].is_whitespace();

        // Check for unnecessary trailing space
        let has_trailing_space = content[content.len() - 1].is_whitespace();

        // If there are multiple leading or trailing spaces, that's definitely wrong
        if content.len() >= 2 {
            let has_multiple_leading = has_leading_space && content[1].is_whitespace();
            let has_multiple_trailing =
                has_trailing_space && content[content.len() - 2].is_whitespace();

            if has_multiple_leading || has_multiple_trailing {
                return true;
            }
        }

        // For normal content, any leading or trailing space is unnecessary
        has_leading_space || has_trailing_space
    }

    /// Get code block ranges to exclude from checking
    fn get_code_block_ranges(&self, lines: &[&str]) -> Vec<bool> {
        let mut in_code_block = vec![false; lines.len()];
        let mut in_fenced_block = false;

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

            // Check for fenced code blocks
            if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
                in_fenced_block = !in_fenced_block;
                in_code_block[i] = true;
                continue;
            }

            if in_fenced_block {
                in_code_block[i] = true;
                continue;
            }
        }

        in_code_block
    }
}

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

    fn name(&self) -> &'static str {
        "no-space-in-code"
    }

    fn description(&self) -> &'static str {
        "Spaces inside code span elements"
    }

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

    fn check_with_ast<'a>(
        &self,
        document: &Document,
        _ast: Option<&'a comrak::nodes::AstNode<'a>>,
    ) -> Result<Vec<Violation>> {
        let mut violations = Vec::new();
        let lines: Vec<&str> = document.content.lines().collect();
        let in_code_block = self.get_code_block_ranges(&lines);

        for (line_number, line) in lines.iter().enumerate() {
            let line_number = line_number + 1;

            // Skip lines inside code blocks
            if in_code_block[line_number - 1] {
                continue;
            }

            violations.extend(self.find_code_span_violations(line, line_number));
        }

        Ok(violations)
    }
}

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

    #[test]
    fn test_md038_no_violations() {
        let content = r#"Here is some `code` text.

More text with `another code span` here.

Complex code: `some.method()` works.

Multiple backticks: ``code with `backticks` inside``.
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD038;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0);
    }

    #[test]
    fn test_md038_leading_space() {
        let content = r#"Here is some ` code` with leading space.

Another example: ` another` here.
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD038;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].line, 1);
        assert_eq!(violations[1].line, 3);
    }

    #[test]
    fn test_md038_trailing_space() {
        let content = r#"Here is some `code ` with trailing space.

Another example: `another ` here.
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD038;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].line, 1);
        assert_eq!(violations[1].line, 3);
    }

    #[test]
    fn test_md038_both_spaces() {
        let content = r#"Here is some ` code ` with both spaces.

Multiple spaces: `   code   ` is also wrong.
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD038;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 2);
        assert_eq!(violations[0].line, 1);
        assert_eq!(violations[1].line, 3);
    }

    #[test]
    fn test_md038_backtick_escaping_allowed() {
        let content = r#"To show a backtick: `` ` ``.

To show backticks: `` `backticks` ``.

Another way: `` backtick` ``.
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD038;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0); // These should be allowed
    }

    #[test]
    fn test_md038_spaces_only_allowed() {
        let content = r#"Single space: ` `.

Multiple spaces: `   `.

Tab character: `	`.
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD038;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0); // Spaces-only content is allowed
    }

    #[test]
    fn test_md038_multiple_code_spans() {
        let content = r#"Good: `code1` and `code2` and `code3`.

Bad: ` code1` and `code2 ` and ` code3 `.

Mixed: `good` and ` bad` and `also good`.
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD038;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 4);
        assert_eq!(violations[0].line, 3); // ` code1`
        assert_eq!(violations[1].line, 3); // `code2 `
        assert_eq!(violations[2].line, 3); // ` code3 `
        assert_eq!(violations[3].line, 5); // ` bad`
    }

    #[test]
    fn test_md038_triple_backticks_ignored() {
        let content = r#"```
This is a code block, not a code span.
` spaces here` should not be flagged.
```

But this `code span ` should be flagged.
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD038;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 1);
        assert_eq!(violations[0].line, 6);
    }

    #[test]
    fn test_md038_unmatched_backticks() {
        let content = r#"This line has ` unmatched backtick.

This line has normal `code` and then ` another unmatched.

Normal content here.
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD038;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0); // The `code` span has no spaces, so no violations
    }

    #[test]
    fn test_md038_empty_code_spans() {
        let content = r#"Empty code span: ``.

Another empty: ``.

With spaces only: ` `.
"#;

        let document = Document::new(content.to_string(), PathBuf::from("test.md")).unwrap();
        let rule = MD038;
        let violations = rule.check(&document).unwrap();
        assert_eq!(violations.len(), 0); // Empty spans are not violations
    }
}