use crate::rule::{
CrossFileScope, Fix, FixCapability, LintError, LintResult, LintWarning, Rule, RuleCategory, Severity,
};
use crate::workspace_index::{FileIndex, extract_cross_file_links};
use regex::Regex;
use std::collections::HashMap;
use std::env;
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use std::sync::{Arc, Mutex};
mod md057_config;
use crate::rule_config_serde::RuleConfig;
use crate::utils::mkdocs_config::resolve_docs_dir;
pub use md057_config::{AbsoluteLinksOption, MD057Config};
static FILE_EXISTENCE_CACHE: LazyLock<Arc<Mutex<HashMap<PathBuf, bool>>>> =
LazyLock::new(|| Arc::new(Mutex::new(HashMap::new())));
fn reset_file_existence_cache() {
if let Ok(mut cache) = FILE_EXISTENCE_CACHE.lock() {
cache.clear();
}
}
fn file_exists_with_cache(path: &Path) -> bool {
match FILE_EXISTENCE_CACHE.lock() {
Ok(mut cache) => *cache.entry(path.to_path_buf()).or_insert_with(|| path.exists()),
Err(_) => path.exists(), }
}
fn file_exists_or_markdown_extension(path: &Path) -> bool {
if file_exists_with_cache(path) {
return true;
}
if path.extension().is_none() {
for ext in MARKDOWN_EXTENSIONS {
let path_with_ext = path.with_extension(&ext[1..]);
if file_exists_with_cache(&path_with_ext) {
return true;
}
}
}
false
}
static LINK_START_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"!?\[[^\]]*\]").unwrap());
static URL_EXTRACT_ANGLE_BRACKET_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#"\]\(\s*<([^>]+)>(#[^\)\s]*)?\s*(?:"[^"]*")?\s*\)"#).unwrap());
static URL_EXTRACT_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new("\\]\\(\\s*([^>\\)\\s#]+)(#[^)\\s]*)?\\s*(?:\"[^\"]*\")?\\s*\\)").unwrap());
static PROTOCOL_DOMAIN_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"^([a-zA-Z][a-zA-Z0-9+.-]*://|[a-zA-Z][a-zA-Z0-9+.-]*:|www\.)").unwrap());
static CURRENT_DIR: LazyLock<PathBuf> = LazyLock::new(|| env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
#[inline]
fn hex_digit_to_value(byte: u8) -> Option<u8> {
match byte {
b'0'..=b'9' => Some(byte - b'0'),
b'a'..=b'f' => Some(byte - b'a' + 10),
b'A'..=b'F' => Some(byte - b'A' + 10),
_ => None,
}
}
const MARKDOWN_EXTENSIONS: &[&str] = &[
".md",
".markdown",
".mdx",
".mkd",
".mkdn",
".mdown",
".mdwn",
".qmd",
".rmd",
];
#[derive(Debug, Clone)]
pub struct MD057ExistingRelativeLinks {
base_path: Arc<Mutex<Option<PathBuf>>>,
config: MD057Config,
}
impl Default for MD057ExistingRelativeLinks {
fn default() -> Self {
Self {
base_path: Arc::new(Mutex::new(None)),
config: MD057Config::default(),
}
}
}
impl MD057ExistingRelativeLinks {
pub fn new() -> Self {
Self::default()
}
pub fn with_path<P: AsRef<Path>>(self, path: P) -> Self {
let path = path.as_ref();
let dir_path = if path.is_file() {
path.parent().map(|p| p.to_path_buf())
} else {
Some(path.to_path_buf())
};
if let Ok(mut guard) = self.base_path.lock() {
*guard = dir_path;
}
self
}
pub fn from_config_struct(config: MD057Config) -> Self {
Self {
base_path: Arc::new(Mutex::new(None)),
config,
}
}
#[inline]
fn is_external_url(&self, url: &str) -> bool {
if url.is_empty() {
return false;
}
if PROTOCOL_DOMAIN_REGEX.is_match(url) || url.starts_with("www.") {
return true;
}
if url.starts_with("{{") || url.starts_with("{%") {
return true;
}
if url.contains('@') {
return true; }
if url.ends_with(".com") {
return true;
}
if url.starts_with('~') || url.starts_with('@') {
return true;
}
false
}
#[inline]
fn is_fragment_only_link(&self, url: &str) -> bool {
url.starts_with('#')
}
#[inline]
fn is_absolute_path(url: &str) -> bool {
url.starts_with('/')
}
fn url_decode(path: &str) -> String {
if !path.contains('%') {
return path.to_string();
}
let bytes = path.as_bytes();
let mut result = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'%' && i + 2 < bytes.len() {
let hex1 = bytes[i + 1];
let hex2 = bytes[i + 2];
if let (Some(d1), Some(d2)) = (hex_digit_to_value(hex1), hex_digit_to_value(hex2)) {
result.push(d1 * 16 + d2);
i += 3;
continue;
}
}
result.push(bytes[i]);
i += 1;
}
String::from_utf8(result).unwrap_or_else(|_| path.to_string())
}
fn strip_query_and_fragment(url: &str) -> &str {
let query_pos = url.find('?');
let fragment_pos = url.find('#');
match (query_pos, fragment_pos) {
(Some(q), Some(f)) => {
&url[..q.min(f)]
}
(Some(q), None) => &url[..q],
(None, Some(f)) => &url[..f],
(None, None) => url,
}
}
fn resolve_link_path_with_base(link: &str, base_path: &Path) -> PathBuf {
base_path.join(link)
}
fn compact_path_suggestion(&self, url: &str, base_path: &Path) -> Option<String> {
if !self.config.compact_paths {
return None;
}
let path_end = url
.find('?')
.unwrap_or(url.len())
.min(url.find('#').unwrap_or(url.len()));
let path_part = &url[..path_end];
let suffix = &url[path_end..];
let decoded_path = Self::url_decode(path_part);
compute_compact_path(base_path, &decoded_path).map(|compact| format!("{compact}{suffix}"))
}
fn validate_absolute_link_via_docs_dir(url: &str, source_path: &Path) -> Option<String> {
let Some(docs_dir) = resolve_docs_dir(source_path) else {
return Some(format!(
"Absolute link '{url}' cannot be validated locally (no mkdocs.yml found)"
));
};
let relative_url = url.trim_start_matches('/');
let file_path = Self::strip_query_and_fragment(relative_url);
let decoded = Self::url_decode(file_path);
let resolved_path = docs_dir.join(&decoded);
let is_directory_link = url.ends_with('/') || decoded.is_empty();
if is_directory_link || resolved_path.is_dir() {
let index_path = resolved_path.join("index.md");
if file_exists_with_cache(&index_path) {
return None; }
if resolved_path.is_dir() {
return Some(format!(
"Absolute link '{url}' resolves to directory '{}' which has no index.md",
resolved_path.display()
));
}
}
if file_exists_or_markdown_extension(&resolved_path) {
return None; }
if let Some(ext) = resolved_path.extension().and_then(|e| e.to_str())
&& (ext.eq_ignore_ascii_case("html") || ext.eq_ignore_ascii_case("htm"))
&& let (Some(stem), Some(parent)) = (
resolved_path.file_stem().and_then(|s| s.to_str()),
resolved_path.parent(),
)
{
let has_md_source = MARKDOWN_EXTENSIONS.iter().any(|md_ext| {
let source_path = parent.join(format!("{stem}{md_ext}"));
file_exists_with_cache(&source_path)
});
if has_md_source {
return None; }
}
Some(format!(
"Absolute link '{url}' resolves to '{}' which does not exist",
resolved_path.display()
))
}
}
impl Rule for MD057ExistingRelativeLinks {
fn name(&self) -> &'static str {
"MD057"
}
fn description(&self) -> &'static str {
"Relative links should point to existing files"
}
fn category(&self) -> RuleCategory {
RuleCategory::Link
}
fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
ctx.content.is_empty() || !ctx.likely_has_links_or_images()
}
fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
let content = ctx.content;
if content.is_empty() || !content.contains('[') {
return Ok(Vec::new());
}
if !content.contains("](") && !content.contains("]:") {
return Ok(Vec::new());
}
reset_file_existence_cache();
let mut warnings = Vec::new();
let base_path: Option<PathBuf> = {
let explicit_base = self.base_path.lock().ok().and_then(|g| g.clone());
if explicit_base.is_some() {
explicit_base
} else if let Some(ref source_file) = ctx.source_file {
let resolved_file = source_file.canonicalize().unwrap_or_else(|_| source_file.clone());
resolved_file
.parent()
.map(|p| p.to_path_buf())
.or_else(|| Some(CURRENT_DIR.clone()))
} else {
None
}
};
let Some(base_path) = base_path else {
return Ok(warnings);
};
if !ctx.links.is_empty() {
let line_index = &ctx.line_index;
let lines = ctx.raw_lines();
let mut processed_lines = std::collections::HashSet::new();
for link in &ctx.links {
let line_idx = link.line - 1;
if line_idx >= lines.len() {
continue;
}
if ctx.line_info(link.line).is_some_and(|info| info.in_pymdown_block) {
continue;
}
if !processed_lines.insert(line_idx) {
continue;
}
let line = lines[line_idx];
if !line.contains("](") {
continue;
}
for link_match in LINK_START_REGEX.find_iter(line) {
let start_pos = link_match.start();
let end_pos = link_match.end();
let line_start_byte = line_index.get_line_start_byte(line_idx + 1).unwrap_or(0);
let absolute_start_pos = line_start_byte + start_pos;
if ctx.is_in_code_span_byte(absolute_start_pos) {
continue;
}
if ctx.is_in_math_span(absolute_start_pos) {
continue;
}
let caps_and_url = URL_EXTRACT_ANGLE_BRACKET_REGEX
.captures_at(line, end_pos - 1)
.and_then(|caps| caps.get(1).map(|g| (caps, g)))
.or_else(|| {
URL_EXTRACT_REGEX
.captures_at(line, end_pos - 1)
.and_then(|caps| caps.get(1).map(|g| (caps, g)))
});
if let Some((caps, url_group)) = caps_and_url {
let url = url_group.as_str().trim();
if url.is_empty() {
continue;
}
if url.starts_with('`') && url.ends_with('`') {
continue;
}
if self.is_external_url(url) || self.is_fragment_only_link(url) {
continue;
}
if Self::is_absolute_path(url) {
match self.config.absolute_links {
AbsoluteLinksOption::Warn => {
let url_start = url_group.start();
let url_end = url_group.end();
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: link.line,
column: url_start + 1,
end_line: link.line,
end_column: url_end + 1,
message: format!("Absolute link '{url}' cannot be validated locally"),
severity: Severity::Warning,
fix: None,
});
}
AbsoluteLinksOption::RelativeToDocs => {
if let Some(msg) = Self::validate_absolute_link_via_docs_dir(url, &base_path) {
let url_start = url_group.start();
let url_end = url_group.end();
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: link.line,
column: url_start + 1,
end_line: link.line,
end_column: url_end + 1,
message: msg,
severity: Severity::Warning,
fix: None,
});
}
}
AbsoluteLinksOption::Ignore => {}
}
continue;
}
let full_url_for_compact = if let Some(frag) = caps.get(2) {
format!("{url}{}", frag.as_str())
} else {
url.to_string()
};
if let Some(suggestion) = self.compact_path_suggestion(&full_url_for_compact, &base_path) {
let url_start = url_group.start();
let url_end = caps.get(2).map_or(url_group.end(), |frag| frag.end());
let fix_byte_start = line_start_byte + url_start;
let fix_byte_end = line_start_byte + url_end;
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: link.line,
column: url_start + 1,
end_line: link.line,
end_column: url_end + 1,
message: format!(
"Relative link '{full_url_for_compact}' can be simplified to '{suggestion}'"
),
severity: Severity::Warning,
fix: Some(Fix {
range: fix_byte_start..fix_byte_end,
replacement: suggestion,
}),
});
}
let file_path = Self::strip_query_and_fragment(url);
let decoded_path = Self::url_decode(file_path);
let resolved_path = Self::resolve_link_path_with_base(&decoded_path, &base_path);
if file_exists_or_markdown_extension(&resolved_path) {
continue; }
let has_md_source = if let Some(ext) = resolved_path.extension().and_then(|e| e.to_str())
&& (ext.eq_ignore_ascii_case("html") || ext.eq_ignore_ascii_case("htm"))
&& let (Some(stem), Some(parent)) = (
resolved_path.file_stem().and_then(|s| s.to_str()),
resolved_path.parent(),
) {
MARKDOWN_EXTENSIONS.iter().any(|md_ext| {
let source_path = parent.join(format!("{stem}{md_ext}"));
file_exists_with_cache(&source_path)
})
} else {
false
};
if has_md_source {
continue; }
let url_start = url_group.start();
let url_end = url_group.end();
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: link.line,
column: url_start + 1, end_line: link.line,
end_column: url_end + 1, message: format!("Relative link '{url}' does not exist"),
severity: Severity::Error,
fix: None,
});
}
}
}
}
for image in &ctx.images {
if ctx.line_info(image.line).is_some_and(|info| info.in_pymdown_block) {
continue;
}
let url = image.url.as_ref();
if url.is_empty() {
continue;
}
if self.is_external_url(url) || self.is_fragment_only_link(url) {
continue;
}
if Self::is_absolute_path(url) {
match self.config.absolute_links {
AbsoluteLinksOption::Warn => {
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: image.line,
column: image.start_col + 1,
end_line: image.line,
end_column: image.start_col + 1 + url.len(),
message: format!("Absolute link '{url}' cannot be validated locally"),
severity: Severity::Warning,
fix: None,
});
}
AbsoluteLinksOption::RelativeToDocs => {
if let Some(msg) = Self::validate_absolute_link_via_docs_dir(url, &base_path) {
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: image.line,
column: image.start_col + 1,
end_line: image.line,
end_column: image.start_col + 1 + url.len(),
message: msg,
severity: Severity::Warning,
fix: None,
});
}
}
AbsoluteLinksOption::Ignore => {}
}
continue;
}
if let Some(suggestion) = self.compact_path_suggestion(url, &base_path) {
let fix = content[image.byte_offset..image.byte_end].find(url).map(|url_offset| {
let fix_byte_start = image.byte_offset + url_offset;
let fix_byte_end = fix_byte_start + url.len();
Fix {
range: fix_byte_start..fix_byte_end,
replacement: suggestion.clone(),
}
});
let img_line_start_byte = ctx.line_index.get_line_start_byte(image.line).unwrap_or(0);
let url_col = fix
.as_ref()
.map_or(image.start_col + 1, |f| f.range.start - img_line_start_byte + 1);
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: image.line,
column: url_col,
end_line: image.line,
end_column: url_col + url.len(),
message: format!("Relative link '{url}' can be simplified to '{suggestion}'"),
severity: Severity::Warning,
fix,
});
}
let file_path = Self::strip_query_and_fragment(url);
let decoded_path = Self::url_decode(file_path);
let resolved_path = Self::resolve_link_path_with_base(&decoded_path, &base_path);
if file_exists_or_markdown_extension(&resolved_path) {
continue; }
let has_md_source = if let Some(ext) = resolved_path.extension().and_then(|e| e.to_str())
&& (ext.eq_ignore_ascii_case("html") || ext.eq_ignore_ascii_case("htm"))
&& let (Some(stem), Some(parent)) = (
resolved_path.file_stem().and_then(|s| s.to_str()),
resolved_path.parent(),
) {
MARKDOWN_EXTENSIONS.iter().any(|md_ext| {
let source_path = parent.join(format!("{stem}{md_ext}"));
file_exists_with_cache(&source_path)
})
} else {
false
};
if has_md_source {
continue; }
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: image.line,
column: image.start_col + 1,
end_line: image.line,
end_column: image.start_col + 1 + url.len(),
message: format!("Relative link '{url}' does not exist"),
severity: Severity::Error,
fix: None,
});
}
for ref_def in &ctx.reference_defs {
let url = &ref_def.url;
if url.is_empty() {
continue;
}
if self.is_external_url(url) || self.is_fragment_only_link(url) {
continue;
}
if Self::is_absolute_path(url) {
match self.config.absolute_links {
AbsoluteLinksOption::Warn => {
let line_idx = ref_def.line - 1;
let column = content.lines().nth(line_idx).map_or(1, |line_content| {
line_content.find(url.as_str()).map_or(1, |url_pos| url_pos + 1)
});
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: ref_def.line,
column,
end_line: ref_def.line,
end_column: column + url.len(),
message: format!("Absolute link '{url}' cannot be validated locally"),
severity: Severity::Warning,
fix: None,
});
}
AbsoluteLinksOption::RelativeToDocs => {
if let Some(msg) = Self::validate_absolute_link_via_docs_dir(url, &base_path) {
let line_idx = ref_def.line - 1;
let column = content.lines().nth(line_idx).map_or(1, |line_content| {
line_content.find(url.as_str()).map_or(1, |url_pos| url_pos + 1)
});
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: ref_def.line,
column,
end_line: ref_def.line,
end_column: column + url.len(),
message: msg,
severity: Severity::Warning,
fix: None,
});
}
}
AbsoluteLinksOption::Ignore => {}
}
continue;
}
if let Some(suggestion) = self.compact_path_suggestion(url, &base_path) {
let ref_line_idx = ref_def.line - 1;
let col = content.lines().nth(ref_line_idx).map_or(1, |line_content| {
line_content.find(url.as_str()).map_or(1, |url_pos| url_pos + 1)
});
let ref_line_start_byte = ctx.line_index.get_line_start_byte(ref_def.line).unwrap_or(0);
let fix_byte_start = ref_line_start_byte + col - 1;
let fix_byte_end = fix_byte_start + url.len();
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: ref_def.line,
column: col,
end_line: ref_def.line,
end_column: col + url.len(),
message: format!("Relative link '{url}' can be simplified to '{suggestion}'"),
severity: Severity::Warning,
fix: Some(Fix {
range: fix_byte_start..fix_byte_end,
replacement: suggestion,
}),
});
}
let file_path = Self::strip_query_and_fragment(url);
let decoded_path = Self::url_decode(file_path);
let resolved_path = Self::resolve_link_path_with_base(&decoded_path, &base_path);
if file_exists_or_markdown_extension(&resolved_path) {
continue; }
let has_md_source = if let Some(ext) = resolved_path.extension().and_then(|e| e.to_str())
&& (ext.eq_ignore_ascii_case("html") || ext.eq_ignore_ascii_case("htm"))
&& let (Some(stem), Some(parent)) = (
resolved_path.file_stem().and_then(|s| s.to_str()),
resolved_path.parent(),
) {
MARKDOWN_EXTENSIONS.iter().any(|md_ext| {
let source_path = parent.join(format!("{stem}{md_ext}"));
file_exists_with_cache(&source_path)
})
} else {
false
};
if has_md_source {
continue; }
let line_idx = ref_def.line - 1;
let column = content.lines().nth(line_idx).map_or(1, |line_content| {
line_content.find(url.as_str()).map_or(1, |url_pos| url_pos + 1)
});
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: ref_def.line,
column,
end_line: ref_def.line,
end_column: column + url.len(),
message: format!("Relative link '{url}' does not exist"),
severity: Severity::Error,
fix: None,
});
}
Ok(warnings)
}
fn fix_capability(&self) -> FixCapability {
if self.config.compact_paths {
FixCapability::ConditionallyFixable
} else {
FixCapability::Unfixable
}
}
fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
if !self.config.compact_paths {
return Ok(ctx.content.to_string());
}
let warnings = self.check(ctx)?;
let warnings =
crate::utils::fix_utils::filter_warnings_by_inline_config(warnings, ctx.inline_config(), self.name());
let mut content = ctx.content.to_string();
let mut fixes: Vec<_> = warnings.iter().filter_map(|w| w.fix.as_ref()).collect();
fixes.sort_by(|a, b| b.range.start.cmp(&a.range.start));
for fix in fixes {
if fix.range.end <= content.len() {
content.replace_range(fix.range.clone(), &fix.replacement);
}
}
Ok(content)
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn default_config_section(&self) -> Option<(String, toml::Value)> {
let default_config = MD057Config::default();
let json_value = serde_json::to_value(&default_config).ok()?;
let toml_value = crate::rule_config_serde::json_to_toml_value(&json_value)?;
if let toml::Value::Table(table) = toml_value {
if !table.is_empty() {
Some((MD057Config::RULE_NAME.to_string(), toml::Value::Table(table)))
} else {
None
}
} else {
None
}
}
fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
where
Self: Sized,
{
let rule_config = crate::rule_config_serde::load_rule_config::<MD057Config>(config);
Box::new(Self::from_config_struct(rule_config))
}
fn cross_file_scope(&self) -> CrossFileScope {
CrossFileScope::Workspace
}
fn contribute_to_index(&self, ctx: &crate::lint_context::LintContext, index: &mut FileIndex) {
for link in extract_cross_file_links(ctx) {
index.add_cross_file_link(link);
}
}
fn cross_file_check(
&self,
file_path: &Path,
file_index: &FileIndex,
workspace_index: &crate::workspace_index::WorkspaceIndex,
) -> LintResult {
let mut warnings = Vec::new();
let file_dir = file_path.parent();
for cross_link in &file_index.cross_file_links {
let decoded_target = Self::url_decode(&cross_link.target_path);
if decoded_target.starts_with('/') {
continue;
}
let target_path = if let Some(dir) = file_dir {
dir.join(&decoded_target)
} else {
Path::new(&decoded_target).to_path_buf()
};
let target_path = normalize_path(&target_path);
let file_exists =
workspace_index.contains_file(&target_path) || file_exists_or_markdown_extension(&target_path);
if !file_exists {
let has_md_source = if let Some(ext) = target_path.extension().and_then(|e| e.to_str())
&& (ext.eq_ignore_ascii_case("html") || ext.eq_ignore_ascii_case("htm"))
&& let (Some(stem), Some(parent)) =
(target_path.file_stem().and_then(|s| s.to_str()), target_path.parent())
{
MARKDOWN_EXTENSIONS.iter().any(|md_ext| {
let source_path = parent.join(format!("{stem}{md_ext}"));
workspace_index.contains_file(&source_path) || source_path.exists()
})
} else {
false
};
if !has_md_source {
warnings.push(LintWarning {
rule_name: Some(self.name().to_string()),
line: cross_link.line,
column: cross_link.column,
end_line: cross_link.line,
end_column: cross_link.column + cross_link.target_path.len(),
message: format!("Relative link '{}' does not exist", cross_link.target_path),
severity: Severity::Error,
fix: None,
});
}
}
}
Ok(warnings)
}
}
fn shortest_relative_path(from_dir: &Path, to_path: &Path) -> PathBuf {
let from_components: Vec<_> = from_dir.components().collect();
let to_components: Vec<_> = to_path.components().collect();
let common_len = from_components
.iter()
.zip(to_components.iter())
.take_while(|(a, b)| a == b)
.count();
let mut result = PathBuf::new();
for _ in common_len..from_components.len() {
result.push("..");
}
for component in &to_components[common_len..] {
result.push(component);
}
result
}
fn compute_compact_path(source_dir: &Path, raw_link_path: &str) -> Option<String> {
let link_path = Path::new(raw_link_path);
let has_traversal = link_path
.components()
.any(|c| matches!(c, std::path::Component::ParentDir | std::path::Component::CurDir));
if !has_traversal {
return None;
}
let combined = source_dir.join(link_path);
let normalized_target = normalize_path(&combined);
let normalized_source = normalize_path(source_dir);
let shortest = shortest_relative_path(&normalized_source, &normalized_target);
if shortest != link_path {
let compact = shortest.to_string_lossy().to_string();
if compact.is_empty() {
return None;
}
Some(compact.replace('\\', "/"))
} else {
None
}
}
fn normalize_path(path: &Path) -> PathBuf {
let mut components = Vec::new();
for component in path.components() {
match component {
std::path::Component::ParentDir => {
if !components.is_empty() {
components.pop();
}
}
std::path::Component::CurDir => {
}
_ => {
components.push(component);
}
}
}
components.iter().collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::workspace_index::CrossFileLinkIndex;
use std::fs::File;
use std::io::Write;
use tempfile::tempdir;
#[test]
fn test_strip_query_and_fragment() {
assert_eq!(
MD057ExistingRelativeLinks::strip_query_and_fragment("file.png?raw=true"),
"file.png"
);
assert_eq!(
MD057ExistingRelativeLinks::strip_query_and_fragment("file.png?raw=true&version=1"),
"file.png"
);
assert_eq!(
MD057ExistingRelativeLinks::strip_query_and_fragment("file.png?"),
"file.png"
);
assert_eq!(
MD057ExistingRelativeLinks::strip_query_and_fragment("file.md#section"),
"file.md"
);
assert_eq!(
MD057ExistingRelativeLinks::strip_query_and_fragment("file.md#"),
"file.md"
);
assert_eq!(
MD057ExistingRelativeLinks::strip_query_and_fragment("file.md?raw=true#section"),
"file.md"
);
assert_eq!(
MD057ExistingRelativeLinks::strip_query_and_fragment("file.png"),
"file.png"
);
assert_eq!(
MD057ExistingRelativeLinks::strip_query_and_fragment("path/to/image.png?raw=true"),
"path/to/image.png"
);
assert_eq!(
MD057ExistingRelativeLinks::strip_query_and_fragment("path/to/image.png?raw=true#anchor"),
"path/to/image.png"
);
assert_eq!(
MD057ExistingRelativeLinks::strip_query_and_fragment("file.md#section?query"),
"file.md"
);
}
#[test]
fn test_url_decode() {
assert_eq!(
MD057ExistingRelativeLinks::url_decode("penguin%20with%20space.jpg"),
"penguin with space.jpg"
);
assert_eq!(
MD057ExistingRelativeLinks::url_decode("assets/my%20file%20name.png"),
"assets/my file name.png"
);
assert_eq!(
MD057ExistingRelativeLinks::url_decode("hello%20world%21.md"),
"hello world!.md"
);
assert_eq!(MD057ExistingRelativeLinks::url_decode("%2f%2e%2e"), "/..");
assert_eq!(MD057ExistingRelativeLinks::url_decode("%2F%2E%2E"), "/..");
assert_eq!(MD057ExistingRelativeLinks::url_decode("%2f%2E%2e"), "/..");
assert_eq!(
MD057ExistingRelativeLinks::url_decode("normal-file.md"),
"normal-file.md"
);
assert_eq!(MD057ExistingRelativeLinks::url_decode("file%2.txt"), "file%2.txt");
assert_eq!(MD057ExistingRelativeLinks::url_decode("file%"), "file%");
assert_eq!(MD057ExistingRelativeLinks::url_decode("file%GG.txt"), "file%GG.txt");
assert_eq!(MD057ExistingRelativeLinks::url_decode("file+name.txt"), "file+name.txt");
assert_eq!(MD057ExistingRelativeLinks::url_decode(""), "");
assert_eq!(MD057ExistingRelativeLinks::url_decode("caf%C3%A9.md"), "café.md");
assert_eq!(MD057ExistingRelativeLinks::url_decode("%20%20%20"), " ");
assert_eq!(
MD057ExistingRelativeLinks::url_decode("path%2Fto%2Ffile.md"),
"path/to/file.md"
);
assert_eq!(
MD057ExistingRelativeLinks::url_decode("hello%20world/foo%20bar.md"),
"hello world/foo bar.md"
);
assert_eq!(MD057ExistingRelativeLinks::url_decode("file%5B1%5D.md"), "file[1].md");
assert_eq!(MD057ExistingRelativeLinks::url_decode("100%pure.md"), "100%pure.md");
}
#[test]
fn test_url_encoded_filenames() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let file_with_spaces = base_path.join("penguin with space.jpg");
File::create(&file_with_spaces)
.unwrap()
.write_all(b"image data")
.unwrap();
let subdir = base_path.join("my images");
std::fs::create_dir(&subdir).unwrap();
let nested_file = subdir.join("photo 1.png");
File::create(&nested_file).unwrap().write_all(b"photo data").unwrap();
let content = r#"
# Test Document with URL-Encoded Links



"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should only warn about missing%20file.jpg. Got: {result:?}"
);
assert!(
result[0].message.contains("missing%20file.jpg"),
"Warning should mention the URL-encoded filename"
);
}
#[test]
fn test_external_urls() {
let rule = MD057ExistingRelativeLinks::new();
assert!(rule.is_external_url("https://example.com"));
assert!(rule.is_external_url("http://example.com"));
assert!(rule.is_external_url("ftp://example.com"));
assert!(rule.is_external_url("www.example.com"));
assert!(rule.is_external_url("example.com"));
assert!(rule.is_external_url("file:///path/to/file"));
assert!(rule.is_external_url("smb://server/share"));
assert!(rule.is_external_url("macappstores://apps.apple.com/"));
assert!(rule.is_external_url("mailto:user@example.com"));
assert!(rule.is_external_url("tel:+1234567890"));
assert!(rule.is_external_url("data:text/plain;base64,SGVsbG8="));
assert!(rule.is_external_url("javascript:void(0)"));
assert!(rule.is_external_url("ssh://git@github.com/repo"));
assert!(rule.is_external_url("git://github.com/repo.git"));
assert!(rule.is_external_url("user@example.com"));
assert!(rule.is_external_url("steering@kubernetes.io"));
assert!(rule.is_external_url("john.doe+filter@company.co.uk"));
assert!(rule.is_external_url("user_name@sub.domain.com"));
assert!(rule.is_external_url("firstname.lastname+tag@really.long.domain.example.org"));
assert!(rule.is_external_url("{{URL}}")); assert!(rule.is_external_url("{{#URL}}")); assert!(rule.is_external_url("{{> partial}}")); assert!(rule.is_external_url("{{ variable }}")); assert!(rule.is_external_url("{{% include %}}")); assert!(rule.is_external_url("{{"));
assert!(!rule.is_external_url("/api/v1/users"));
assert!(!rule.is_external_url("/blog/2024/release.html"));
assert!(!rule.is_external_url("/react/hooks/use-state.html"));
assert!(!rule.is_external_url("/pkg/runtime"));
assert!(!rule.is_external_url("/doc/go1compat"));
assert!(!rule.is_external_url("/index.html"));
assert!(!rule.is_external_url("/assets/logo.png"));
assert!(MD057ExistingRelativeLinks::is_absolute_path("/api/v1/users"));
assert!(MD057ExistingRelativeLinks::is_absolute_path("/blog/2024/release.html"));
assert!(MD057ExistingRelativeLinks::is_absolute_path("/index.html"));
assert!(!MD057ExistingRelativeLinks::is_absolute_path("./relative.md"));
assert!(!MD057ExistingRelativeLinks::is_absolute_path("relative.md"));
assert!(rule.is_external_url("~/assets/image.png"));
assert!(rule.is_external_url("~/components/Button.vue"));
assert!(rule.is_external_url("~assets/logo.svg"));
assert!(rule.is_external_url("@/components/Header.vue"));
assert!(rule.is_external_url("@images/photo.jpg"));
assert!(rule.is_external_url("@assets/styles.css"));
assert!(!rule.is_external_url("./relative/path.md"));
assert!(!rule.is_external_url("relative/path.md"));
assert!(!rule.is_external_url("../parent/path.md"));
}
#[test]
fn test_framework_path_aliases() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"
# Framework Path Aliases




[Link](@/pages/about.md)
This is a [real missing link](missing.md) that should be flagged.
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should only warn about missing.md, not framework aliases. Got: {result:?}"
);
assert!(
result[0].message.contains("missing.md"),
"Warning should be for missing.md"
);
}
#[test]
fn test_url_decode_security_path_traversal() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let file_in_base = base_path.join("safe.md");
File::create(&file_in_base).unwrap().write_all(b"# Safe").unwrap();
let content = r#"
[Traversal attempt](..%2F..%2Fnonexistent_dir_12345%2Fmissing.md)
[Double encoded](..%252F..%252Fnonexistent%252Ffile.md)
[Safe link](safe.md)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
2,
"Should have warnings for traversal attempts. Got: {result:?}"
);
}
#[test]
fn test_url_encoded_utf8_filenames() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let cafe_file = base_path.join("café.md");
File::create(&cafe_file).unwrap().write_all(b"# Cafe").unwrap();
let content = r#"
[Café link](caf%C3%A9.md)
[Missing unicode](r%C3%A9sum%C3%A9.md)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should only warn about missing résumé.md. Got: {result:?}"
);
assert!(
result[0].message.contains("r%C3%A9sum%C3%A9.md"),
"Warning should mention the URL-encoded filename"
);
}
#[test]
fn test_url_encoded_emoji_filenames() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let emoji_dir = base_path.join("👤 Personal");
std::fs::create_dir(&emoji_dir).unwrap();
let file_path = emoji_dir.join("TV Shows.md");
File::create(&file_path)
.unwrap()
.write_all(b"# TV Shows\n\nContent here.")
.unwrap();
let content = r#"
# Test Document
[TV Shows](./%F0%9F%91%A4%20Personal/TV%20Shows.md)
[Missing](./%F0%9F%91%A4%20Personal/Missing.md)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should only warn about missing file. Got: {result:?}");
assert!(
result[0].message.contains("Missing.md"),
"Warning should be for Missing.md, got: {}",
result[0].message
);
}
#[test]
fn test_no_warnings_without_base_path() {
let rule = MD057ExistingRelativeLinks::new();
let content = "[Link](missing.md)";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "Should have no warnings without base path");
}
#[test]
fn test_existing_and_missing_links() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let exists_path = base_path.join("exists.md");
File::create(&exists_path).unwrap().write_all(b"# Test File").unwrap();
assert!(exists_path.exists(), "exists.md should exist for this test");
let content = r#"
# Test Document
[Valid Link](exists.md)
[Invalid Link](missing.md)
[External Link](https://example.com)
[Media Link](image.jpg)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2);
let messages: Vec<_> = result.iter().map(|w| w.message.as_str()).collect();
assert!(messages.iter().any(|m| m.contains("missing.md")));
assert!(messages.iter().any(|m| m.contains("image.jpg")));
}
#[test]
fn test_angle_bracket_links() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let exists_path = base_path.join("exists.md");
File::create(&exists_path).unwrap().write_all(b"# Test File").unwrap();
let content = r#"
# Test Document
[Valid Link](<exists.md>)
[Invalid Link](<missing.md>)
[External Link](<https://example.com>)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should have exactly one warning");
assert!(
result[0].message.contains("missing.md"),
"Warning should mention missing.md"
);
}
#[test]
fn test_angle_bracket_links_with_parens() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let app_dir = base_path.join("app");
std::fs::create_dir(&app_dir).unwrap();
let upload_dir = app_dir.join("(upload)");
std::fs::create_dir(&upload_dir).unwrap();
let page_file = upload_dir.join("page.tsx");
File::create(&page_file)
.unwrap()
.write_all(b"export default function Page() {}")
.unwrap();
let content = r#"
# Test Document with Paths Containing Parens
[Upload Page](<app/(upload)/page.tsx>)
[Unix pipe](<https://en.wikipedia.org/wiki/Pipeline_(Unix)>)
[Missing](<app/(missing)/file.md>)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should have exactly one warning for missing file. Got: {result:?}"
);
assert!(
result[0].message.contains("app/(missing)/file.md"),
"Warning should mention app/(missing)/file.md"
);
}
#[test]
fn test_all_file_types_checked() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"
[Image Link](image.jpg)
[Video Link](video.mp4)
[Markdown Link](document.md)
[PDF Link](file.pdf)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 4, "Should have warnings for all missing files");
}
#[test]
fn test_code_span_detection() {
let rule = MD057ExistingRelativeLinks::new();
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let rule = rule.with_path(base_path);
let content = "This is a [link](nonexistent.md) and `[not a link](not-checked.md)` in code.";
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should only flag the real link");
assert!(result[0].message.contains("nonexistent.md"));
}
#[test]
fn test_inline_code_spans() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"
# Test Document
This is a normal link: [Link](missing.md)
This is a code span with a link: `[Link](another-missing.md)`
Some more text with `inline code [Link](yet-another-missing.md) embedded`.
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should have exactly one warning");
assert!(
result[0].message.contains("missing.md"),
"Warning should be for missing.md"
);
assert!(
!result.iter().any(|w| w.message.contains("another-missing.md")),
"Should not warn about link in code span"
);
assert!(
!result.iter().any(|w| w.message.contains("yet-another-missing.md")),
"Should not warn about link in inline code"
);
}
#[test]
fn test_extensionless_link_resolution() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let page_path = base_path.join("page.md");
File::create(&page_path).unwrap().write_all(b"# Page").unwrap();
let content = r#"
# Test Document
[Link without extension](page)
[Link with extension](page.md)
[Missing link](nonexistent)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should only warn about nonexistent link");
assert!(
result[0].message.contains("nonexistent"),
"Warning should be for 'nonexistent' not 'page'"
);
}
#[test]
fn test_cross_file_scope() {
let rule = MD057ExistingRelativeLinks::new();
assert_eq!(rule.cross_file_scope(), CrossFileScope::Workspace);
}
#[test]
fn test_contribute_to_index_extracts_markdown_links() {
let rule = MD057ExistingRelativeLinks::new();
let content = r#"
# Document
[Link to docs](./docs/guide.md)
[Link with fragment](./other.md#section)
[External link](https://example.com)
[Image link](image.png)
[Media file](video.mp4)
"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let mut index = FileIndex::new();
rule.contribute_to_index(&ctx, &mut index);
assert_eq!(index.cross_file_links.len(), 2);
assert_eq!(index.cross_file_links[0].target_path, "./docs/guide.md");
assert_eq!(index.cross_file_links[0].fragment, "");
assert_eq!(index.cross_file_links[1].target_path, "./other.md");
assert_eq!(index.cross_file_links[1].fragment, "section");
}
#[test]
fn test_contribute_to_index_skips_external_and_anchors() {
let rule = MD057ExistingRelativeLinks::new();
let content = r#"
# Document
[External](https://example.com)
[Another external](http://example.org)
[Fragment only](#section)
[FTP link](ftp://files.example.com)
[Mail link](mailto:test@example.com)
[WWW link](www.example.com)
"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let mut index = FileIndex::new();
rule.contribute_to_index(&ctx, &mut index);
assert_eq!(index.cross_file_links.len(), 0);
}
#[test]
fn test_cross_file_check_valid_link() {
use crate::workspace_index::WorkspaceIndex;
let rule = MD057ExistingRelativeLinks::new();
let mut workspace_index = WorkspaceIndex::new();
workspace_index.insert_file(PathBuf::from("docs/guide.md"), FileIndex::new());
let mut file_index = FileIndex::new();
file_index.add_cross_file_link(CrossFileLinkIndex {
target_path: "guide.md".to_string(),
fragment: "".to_string(),
line: 5,
column: 1,
});
let warnings = rule
.cross_file_check(Path::new("docs/index.md"), &file_index, &workspace_index)
.unwrap();
assert!(warnings.is_empty());
}
#[test]
fn test_cross_file_check_missing_link() {
use crate::workspace_index::WorkspaceIndex;
let rule = MD057ExistingRelativeLinks::new();
let workspace_index = WorkspaceIndex::new();
let mut file_index = FileIndex::new();
file_index.add_cross_file_link(CrossFileLinkIndex {
target_path: "missing.md".to_string(),
fragment: "".to_string(),
line: 5,
column: 1,
});
let warnings = rule
.cross_file_check(Path::new("docs/index.md"), &file_index, &workspace_index)
.unwrap();
assert_eq!(warnings.len(), 1);
assert!(warnings[0].message.contains("missing.md"));
assert!(warnings[0].message.contains("does not exist"));
}
#[test]
fn test_cross_file_check_parent_path() {
use crate::workspace_index::WorkspaceIndex;
let rule = MD057ExistingRelativeLinks::new();
let mut workspace_index = WorkspaceIndex::new();
workspace_index.insert_file(PathBuf::from("readme.md"), FileIndex::new());
let mut file_index = FileIndex::new();
file_index.add_cross_file_link(CrossFileLinkIndex {
target_path: "../readme.md".to_string(),
fragment: "".to_string(),
line: 5,
column: 1,
});
let warnings = rule
.cross_file_check(Path::new("docs/guide.md"), &file_index, &workspace_index)
.unwrap();
assert!(warnings.is_empty());
}
#[test]
fn test_cross_file_check_html_link_with_md_source() {
use crate::workspace_index::WorkspaceIndex;
let rule = MD057ExistingRelativeLinks::new();
let mut workspace_index = WorkspaceIndex::new();
workspace_index.insert_file(PathBuf::from("docs/guide.md"), FileIndex::new());
let mut file_index = FileIndex::new();
file_index.add_cross_file_link(CrossFileLinkIndex {
target_path: "guide.html".to_string(),
fragment: "section".to_string(),
line: 10,
column: 5,
});
let warnings = rule
.cross_file_check(Path::new("docs/index.md"), &file_index, &workspace_index)
.unwrap();
assert!(
warnings.is_empty(),
"Expected no warnings for .html link with .md source, got: {warnings:?}"
);
}
#[test]
fn test_cross_file_check_html_link_without_source() {
use crate::workspace_index::WorkspaceIndex;
let rule = MD057ExistingRelativeLinks::new();
let workspace_index = WorkspaceIndex::new();
let mut file_index = FileIndex::new();
file_index.add_cross_file_link(CrossFileLinkIndex {
target_path: "missing.html".to_string(),
fragment: "".to_string(),
line: 10,
column: 5,
});
let warnings = rule
.cross_file_check(Path::new("docs/index.md"), &file_index, &workspace_index)
.unwrap();
assert_eq!(warnings.len(), 1, "Expected 1 warning for .html link without source");
assert!(warnings[0].message.contains("missing.html"));
}
#[test]
fn test_normalize_path_function() {
assert_eq!(
normalize_path(Path::new("docs/guide.md")),
PathBuf::from("docs/guide.md")
);
assert_eq!(
normalize_path(Path::new("./docs/guide.md")),
PathBuf::from("docs/guide.md")
);
assert_eq!(
normalize_path(Path::new("docs/sub/../guide.md")),
PathBuf::from("docs/guide.md")
);
assert_eq!(normalize_path(Path::new("a/b/c/../../d.md")), PathBuf::from("a/d.md"));
}
#[test]
fn test_html_link_with_md_source() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let md_file = base_path.join("guide.md");
File::create(&md_file).unwrap().write_all(b"# Guide").unwrap();
let content = r#"
[Read the guide](guide.html)
[Also here](getting-started.html)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should only warn about missing source. Got: {result:?}"
);
assert!(result[0].message.contains("getting-started.html"));
}
#[test]
fn test_htm_link_with_md_source() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let md_file = base_path.join("page.md");
File::create(&md_file).unwrap().write_all(b"# Page").unwrap();
let content = "[Page](page.htm)";
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not warn when .md source exists for .htm link"
);
}
#[test]
fn test_html_link_finds_various_markdown_extensions() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
File::create(base_path.join("doc.md")).unwrap();
File::create(base_path.join("tutorial.mdx")).unwrap();
File::create(base_path.join("guide.markdown")).unwrap();
let content = r#"
[Doc](doc.html)
[Tutorial](tutorial.html)
[Guide](guide.html)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should find all markdown variants as source files. Got: {result:?}"
);
}
#[test]
fn test_html_link_in_subdirectory() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let docs_dir = base_path.join("docs");
std::fs::create_dir(&docs_dir).unwrap();
File::create(docs_dir.join("guide.md"))
.unwrap()
.write_all(b"# Guide")
.unwrap();
let content = "[Guide](docs/guide.html)";
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(result.is_empty(), "Should find markdown source in subdirectory");
}
#[test]
fn test_absolute_path_skipped_in_check() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"
# Test Document
[Go Runtime](/pkg/runtime)
[Go Runtime with Fragment](/pkg/runtime#section)
[API Docs](/api/v1/users)
[Blog Post](/blog/2024/release.html)
[React Hook](/react/hooks/use-state.html)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Absolute paths should be skipped. Got warnings: {result:?}"
);
}
#[test]
fn test_absolute_path_skipped_in_cross_file_check() {
use crate::workspace_index::WorkspaceIndex;
let rule = MD057ExistingRelativeLinks::new();
let workspace_index = WorkspaceIndex::new();
let mut file_index = FileIndex::new();
file_index.add_cross_file_link(CrossFileLinkIndex {
target_path: "/pkg/runtime.md".to_string(),
fragment: "".to_string(),
line: 5,
column: 1,
});
file_index.add_cross_file_link(CrossFileLinkIndex {
target_path: "/api/v1/users.md".to_string(),
fragment: "section".to_string(),
line: 10,
column: 1,
});
let warnings = rule
.cross_file_check(Path::new("docs/index.md"), &file_index, &workspace_index)
.unwrap();
assert!(
warnings.is_empty(),
"Absolute paths should be skipped in cross_file_check. Got warnings: {warnings:?}"
);
}
#[test]
fn test_protocol_relative_url_not_skipped() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"
# Test Document
[External](//example.com/page)
[Another](//cdn.example.com/asset.js)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Protocol-relative URLs should be skipped. Got warnings: {result:?}"
);
}
#[test]
fn test_email_addresses_skipped() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"
# Test Document
[Contact](user@example.com)
[Steering](steering@kubernetes.io)
[Support](john.doe+filter@company.co.uk)
[User](user_name@sub.domain.com)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Email addresses should be skipped. Got warnings: {result:?}"
);
}
#[test]
fn test_email_addresses_vs_file_paths() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"
# Test Document
[Email](user@example.com) <!-- Should be skipped (email) -->
[Email2](steering@kubernetes.io) <!-- Should be skipped (email) -->
[Email3](user@file.md) <!-- Should be skipped (has @, treated as email) -->
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"All email addresses should be skipped. Got: {result:?}"
);
}
#[test]
fn test_diagnostic_position_accuracy() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = "prefix [text](missing.md) suffix";
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should have exactly one warning");
assert_eq!(result[0].line, 1, "Should be on line 1");
assert_eq!(result[0].column, 15, "Should point to start of URL 'missing.md'");
assert_eq!(result[0].end_column, 25, "Should point past end of URL 'missing.md'");
}
#[test]
fn test_diagnostic_position_angle_brackets() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = "[link](<missing.md>)";
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should have exactly one warning");
assert_eq!(result[0].line, 1, "Should be on line 1");
assert_eq!(result[0].column, 9, "Should point to start of URL in angle brackets");
}
#[test]
fn test_diagnostic_position_multiline() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"# Title
Some text on line 2
[link on line 3](missing1.md)
More text
[link on line 5](missing2.md)"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 2, "Should have two warnings");
assert_eq!(result[0].line, 3, "First warning should be on line 3");
assert!(result[0].message.contains("missing1.md"));
assert_eq!(result[1].line, 5, "Second warning should be on line 5");
assert!(result[1].message.contains("missing2.md"));
}
#[test]
fn test_diagnostic_position_with_spaces() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = "[link]( missing.md )";
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should have exactly one warning");
assert_eq!(result[0].column, 9, "Should point to URL after stripping spaces");
}
#[test]
fn test_diagnostic_position_image() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = "";
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should have exactly one warning for image");
assert_eq!(result[0].line, 1);
assert!(result[0].column > 0, "Should have valid column position");
assert!(result[0].message.contains("missing.jpg"));
}
#[test]
fn test_wikilinks_skipped() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"# Test Document
[[Microsoft#Windows OS]]
[[SomePage]]
[[Page With Spaces]]
[[path/to/page#section]]
[[page|Display Text]]
This is a [real missing link](missing.md) that should be flagged.
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should only warn about missing.md, not wikilinks. Got: {result:?}"
);
assert!(
result[0].message.contains("missing.md"),
"Warning should be for missing.md, not wikilinks"
);
}
#[test]
fn test_wikilinks_not_added_to_index() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"# Test Document
[[Microsoft#Windows OS]]
[[SomePage#section]]
[Regular Link](other.md)
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let mut file_index = FileIndex::new();
rule.contribute_to_index(&ctx, &mut file_index);
let cross_file_links = &file_index.cross_file_links;
assert_eq!(
cross_file_links.len(),
1,
"Only regular markdown links should be indexed, not wikilinks. Got: {cross_file_links:?}"
);
assert_eq!(file_index.cross_file_links[0].target_path, "other.md");
}
#[test]
fn test_reference_definition_missing_file() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"# Test Document
[test]: ./missing.md
[example]: ./nonexistent.html
Use [test] and [example] here.
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
2,
"Should have warnings for missing reference definition targets. Got: {result:?}"
);
assert!(
result.iter().any(|w| w.message.contains("missing.md")),
"Should warn about missing.md"
);
assert!(
result.iter().any(|w| w.message.contains("nonexistent.html")),
"Should warn about nonexistent.html"
);
}
#[test]
fn test_reference_definition_existing_file() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let exists_path = base_path.join("exists.md");
File::create(&exists_path)
.unwrap()
.write_all(b"# Existing file")
.unwrap();
let content = r#"# Test Document
[test]: ./exists.md
Use [test] here.
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not warn about existing file. Got: {result:?}"
);
}
#[test]
fn test_reference_definition_external_url_skipped() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"# Test Document
[google]: https://google.com
[example]: http://example.org
[mail]: mailto:test@example.com
[ftp]: ftp://files.example.com
[local]: ./missing.md
Use [google], [example], [mail], [ftp], [local] here.
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should only warn about local missing file. Got: {result:?}"
);
assert!(
result[0].message.contains("missing.md"),
"Warning should be for missing.md"
);
}
#[test]
fn test_reference_definition_fragment_only_skipped() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"# Test Document
[section]: #my-section
Use [section] here.
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Should not warn about fragment-only reference. Got: {result:?}"
);
}
#[test]
fn test_reference_definition_column_position() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = "[ref]: ./missing.md";
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(result.len(), 1, "Should have exactly one warning");
assert_eq!(result[0].line, 1, "Should be on line 1");
assert_eq!(result[0].column, 8, "Should point to start of URL './missing.md'");
}
#[test]
fn test_reference_definition_html_with_md_source() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let md_file = base_path.join("guide.md");
File::create(&md_file).unwrap().write_all(b"# Guide").unwrap();
let content = r#"# Test Document
[guide]: ./guide.html
[missing]: ./missing.html
Use [guide] and [missing] here.
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should only warn about missing source. Got: {result:?}"
);
assert!(result[0].message.contains("missing.html"));
}
#[test]
fn test_reference_definition_url_encoded() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let file_with_spaces = base_path.join("file with spaces.md");
File::create(&file_with_spaces).unwrap().write_all(b"# Spaces").unwrap();
let content = r#"# Test Document
[spaces]: ./file%20with%20spaces.md
[missing]: ./missing%20file.md
Use [spaces] and [missing] here.
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should only warn about missing URL-encoded file. Got: {result:?}"
);
assert!(result[0].message.contains("missing%20file.md"));
}
#[test]
fn test_inline_and_reference_both_checked() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let content = r#"# Test Document
[inline link](./inline-missing.md)
[ref]: ./ref-missing.md
Use [ref] here.
"#;
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
2,
"Should warn about both inline and reference links. Got: {result:?}"
);
assert!(
result.iter().any(|w| w.message.contains("inline-missing.md")),
"Should warn about inline-missing.md"
);
assert!(
result.iter().any(|w| w.message.contains("ref-missing.md")),
"Should warn about ref-missing.md"
);
}
#[test]
fn test_footnote_definitions_not_flagged() {
let rule = MD057ExistingRelativeLinks::default();
let content = r#"# Title
A footnote[^1].
[^1]: [link](https://www.google.com).
"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Footnote definitions should not trigger MD057 warnings. Got: {result:?}"
);
}
#[test]
fn test_footnote_with_relative_link_inside() {
let rule = MD057ExistingRelativeLinks::default();
let content = r#"# Title
See the footnote[^1].
[^1]: Check out [this file](./existing.md) for more info.
[^2]: Also see [missing](./does-not-exist.md).
"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
for warning in &result {
assert!(
!warning.message.contains("[this file]"),
"Footnote content should not be treated as URL: {warning:?}"
);
assert!(
!warning.message.contains("[missing]"),
"Footnote content should not be treated as URL: {warning:?}"
);
}
}
#[test]
fn test_mixed_footnotes_and_reference_definitions() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let content = r#"# Title
A footnote[^1] and a [ref link][myref].
[^1]: This is a footnote with [link](https://example.com).
[myref]: ./missing-file.md "This should be checked"
"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should only warn about the regular reference definition. Got: {result:?}"
);
assert!(
result[0].message.contains("missing-file.md"),
"Should warn about missing-file.md in reference definition"
);
}
#[test]
fn test_absolute_links_ignore_by_default() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let rule = MD057ExistingRelativeLinks::new().with_path(base_path);
let content = r#"# Links
[API docs](/api/v1/users)
[Blog post](/blog/2024/release.html)

[ref]: /docs/reference.md
"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert!(
result.is_empty(),
"Absolute links should be ignored by default. Got: {result:?}"
);
}
#[test]
fn test_absolute_links_warn_config() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let config = MD057Config {
absolute_links: AbsoluteLinksOption::Warn,
..Default::default()
};
let rule = MD057ExistingRelativeLinks::from_config_struct(config).with_path(base_path);
let content = r#"# Links
[API docs](/api/v1/users)
[Blog post](/blog/2024/release.html)
"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
2,
"Should warn about both absolute links. Got: {result:?}"
);
assert!(
result[0].message.contains("cannot be validated locally"),
"Warning should explain why: {}",
result[0].message
);
assert!(
result[0].message.contains("/api/v1/users"),
"Warning should include the link path"
);
}
#[test]
fn test_absolute_links_warn_images() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let config = MD057Config {
absolute_links: AbsoluteLinksOption::Warn,
..Default::default()
};
let rule = MD057ExistingRelativeLinks::from_config_struct(config).with_path(base_path);
let content = r#"# Images

"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should warn about absolute image path. Got: {result:?}"
);
assert!(
result[0].message.contains("/assets/logo.png"),
"Warning should include the image path"
);
}
#[test]
fn test_absolute_links_warn_reference_definitions() {
let temp_dir = tempdir().unwrap();
let base_path = temp_dir.path();
let config = MD057Config {
absolute_links: AbsoluteLinksOption::Warn,
..Default::default()
};
let rule = MD057ExistingRelativeLinks::from_config_struct(config).with_path(base_path);
let content = r#"# Reference
See the [docs][ref].
[ref]: /docs/reference.md
"#;
let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
let result = rule.check(&ctx).unwrap();
assert_eq!(
result.len(),
1,
"Should warn about absolute reference definition. Got: {result:?}"
);
assert!(
result[0].message.contains("/docs/reference.md"),
"Warning should include the reference path"
);
}
}