mdwright-lint 0.1.1

Lint diagnostics, rule execution, suppressions, and standard rules for mdwright
Documentation
//! A backtick in prose that pulldown-cmark could not pair.
//!
//! `CommonMark`'s parser pairs backtick runs greedily. If a literal
//! `` ` `` survives into a prose chunk, no matching closing fence
//! was found and the inline code span did not close. Renderers that
//! treat the unmatched run as prose tend to mangle nearby `_` or `*`
//! — flagging this early prevents that.

use crate::diagnostic::Diagnostic;
use crate::rule::LintRule;
use mdwright_document::Document;

pub struct UnbalancedBacktick;

impl LintRule for UnbalancedBacktick {
    fn name(&self) -> &str {
        "unbalanced-backtick"
    }

    fn description(&self) -> &str {
        "Backtick in prose that could not be paired with a closing fence."
    }

    fn explain(&self) -> &str {
        include_str!("explain/unbalanced_backtick.md")
    }

    fn check(&self, doc: &Document, out: &mut Vec<Diagnostic>) {
        for chunk in doc.prose_chunks() {
            for (idx, _) in chunk.text.match_indices('`') {
                let message = "unclosed inline code span — pulldown-cmark could not pair \
                     this backtick with a closing fence"
                    .to_owned();
                if let Some(d) = Diagnostic::at(doc, chunk.byte_offset, idx..idx.saturating_add(1), message, None) {
                    out.push(d);
                }
            }
        }
    }
}