Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ categories = ["command-line-utilities", "development-tools"]
[dependencies]
clap = { version = "4.5.47", features = ["derive"] }
colored = "3.0.0"
dunce = "1.0.5"
pathdiff = "0.2.3"
pulldown-cmark = "0.13.0"
regex = "1.11.2"
Expand Down
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
# mdrefcheck

[![PyPI version](https://img.shields.io/pypi/v/mdrefcheck.svg?logo=pypi&logoColor=white)](https://pypi.org/project/mdrefcheck/)
[![crates.io version](https://img.shields.io/crates/v/mdrefcheck.svg?logo=rust&logoColor=white)](https://crates.io/crates/mdrefcheck)
[![Build Status](https://github.com/gospodima/mdrefcheck/actions/workflows/ci.yml/badge.svg)](https://github.com/gospodima/mdrefcheck/actions/workflows/ci.yml)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)

A CLI tool to validate references and links in Markdown files (CommonMark spec).
It helps to ensure that your documentation is free from broken section links, missing images or files.

## Features

- Validate local file paths in image and section references
- Check section links (`#heading-link`) match existing headings according to [GitHub Flavored Markdown (GFM)](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#section-links) rules
- Identify broken reference-style links
- Email validation
- Validate local file paths in image and file references
- Check section links against actual headings, following [GitHub Flavored Markdown (GFM)](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#section-links) rules, including cross-file references (e.g. `./subfolder/another-file.md#heading-link`)
- Detect broken reference-style links
- Basic email validation

## Installation

### Cargo

`mdrefcheck` is also published on [crates.io](https://crates.io/) and can be installed
with cargo:

```bash
cargo install mdrefcheck
```

### PyPI

`mdrefcheck` can be installed with

```bash
pip install mdrefcheck
```

It also can be used as a tool in an isolated environment, e.g., with `uvx`:
or run it directly in an isolated environment, e.g., with `uvx`:

```bash
uvx mdrefcheck .
```

### Pre-commit integration

You can use `mdrefcheck` as a pre-commit hook.
## Pre-commit integration

Add this to your `.pre-commit-config.yaml`:

Expand Down
3 changes: 1 addition & 2 deletions src/checks/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ pub fn validate_email(email: &str) -> Result<(), String> {
}
}


/// Email validation according to https://spec.commonmark.org/0.31.2/#email-address
fn is_valid_email(s: &str) -> bool {
static EMAIL_RE: &str = r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";
Regex::new(EMAIL_RE).unwrap().is_match(s)
}
}
18 changes: 15 additions & 3 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::relative_path;
use std::path::Path;
use colored::Colorize;
use std::path::Path;

/// Represents a markdown validation issue (Ruff-compatible output)
pub struct ValidationError {
Expand All @@ -11,7 +11,12 @@ pub struct ValidationError {
}

impl ValidationError {
pub fn new(path: &Path, line: usize, col: usize, message: impl Into<String>) -> Self {
pub fn new(
path: &Path,
line: usize,
col: usize,
message: impl Into<String>,
) -> Self {
Self {
path: relative_path(path),
line,
Expand All @@ -23,6 +28,13 @@ impl ValidationError {

impl std::fmt::Display for ValidationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}:{}: {}", self.path.bold(), self.line, self.col, self.message)
write!(
f,
"{}:{}:{}: {}",
self.path.bold(),
self.line,
self.col,
self.message
)
}
}
13 changes: 9 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ pub fn create_options() -> Options {
Options::ENABLE_FOOTNOTES | Options::ENABLE_WIKILINKS
}

/// Create HashSet of canonicalized paths from vector of paths
/// Create HashSet of canonicalized paths from vector of paths
pub fn create_file_set(vec_files: &Vec<PathBuf>) -> HashSet<PathBuf> {
vec_files
.iter()
.filter_map(|s| fs::canonicalize(s).ok())
.collect()
}

/// Return a path relative to current working directory
/// Return a path relative to the current working directory
pub fn relative_path(target: &Path) -> String {
let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
pathdiff::diff_paths(target, cwd)
.unwrap_or_else(|| target.to_path_buf())

// Normalize target path first (fixes Windows \\?\ prefixes)
let normalized =
dunce::canonicalize(target).unwrap_or_else(|_| target.to_path_buf());

pathdiff::diff_paths(&normalized, cwd)
.unwrap_or(normalized)
.display()
.to_string()
}
Expand Down
Loading