use anyhow::Result;
use regex::Regex;
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use crate::config::MarkdownAnalyzerConfig;
use crate::errors;
use crate::file_index::FileIndex;
use crate::graph::Product;
use super::{DepAnalyzer, ScanResult};
pub struct MarkdownDepAnalyzer {
config: MarkdownAnalyzerConfig,
}
impl MarkdownDepAnalyzer {
pub const fn new(config: MarkdownAnalyzerConfig) -> Self {
Self { config }
}
fn scan_references(&self, source: &Path, file_index: &FileIndex) -> Result<ScanResult> {
let content = crate::errors::ctx(
fs::read_to_string(source),
&format!("Failed to read markdown: {}", source.display()),
)?;
let mut refs = Vec::new();
let mut absent = Vec::new();
let mut seen = HashSet::new();
let source_dir = crate::processor::parent_dir(source);
for path_str in reference_targets(&content) {
let path_str = path_str.as_str();
let candidates = [
super::lexical_normalize(&source_dir.join(path_str)),
super::lexical_normalize(Path::new(path_str)),
];
let found = candidates
.iter()
.find(|c| c.is_file() || file_index.contains(c));
absent.extend(super::probed_before(&candidates, found));
if let Some(found) = found
&& seen.insert(found.clone())
{
refs.push(found.clone());
}
}
absent.sort();
absent.dedup();
Ok(ScanResult::deps(refs, absent))
}
}
fn reference_targets(content: &str) -> Vec<String> {
static INLINE_RE: OnceLock<Regex> = OnceLock::new();
static DEFINITION_RE: OnceLock<Regex> = OnceLock::new();
static ATTRIBUTE_RE: OnceLock<Regex> = OnceLock::new();
let inline_re = INLINE_RE.get_or_init(|| {
Regex::new(r"!?\[[^\]]*\]\(\s*(?:<([^>\n]*)>|([^)\s]+))[^)]*\)")
.expect(errors::INVALID_REGEX)
});
let definition_re = DEFINITION_RE.get_or_init(|| {
Regex::new(r"(?m)^ {0,3}\[[^\]\n]+\]:\s*(?:<([^>\n]*)>|(\S+))")
.expect(errors::INVALID_REGEX)
});
let attribute_re = ATTRIBUTE_RE.get_or_init(|| {
Regex::new(r#"(?i)<[a-z][^>]*?\s(?:src|href)\s*=\s*(?:"([^"]*)"|'([^']*)')"#)
.expect(errors::INVALID_REGEX)
});
let mut targets = Vec::new();
for re in [inline_re, definition_re, attribute_re] {
for caps in re.captures_iter(content) {
let Some(raw) = caps.get(1).or_else(|| caps.get(2)) else {
continue;
};
let raw = raw.as_str().trim();
let has_scheme = raw.split_once(':').is_some_and(|(scheme, _)| {
!scheme.is_empty()
&& scheme
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'))
});
if has_scheme || raw.starts_with("//") {
continue;
}
let path = raw.split('#').next().unwrap_or(raw);
if !path.is_empty() {
targets.push(percent_decode(path));
}
}
}
targets
}
fn percent_decode(text: &str) -> String {
let bytes = text.as_bytes();
let mut out = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
let hex = (bytes[i] == b'%')
.then(|| text.get(i + 1..i + 3))
.flatten()
.and_then(|pair| u8::from_str_radix(pair, 16).ok());
if let Some(byte) = hex {
out.push(byte);
i += 3;
} else {
out.push(bytes[i]);
i += 1;
}
}
String::from_utf8(out).unwrap_or_else(|_| text.to_string())
}
impl DepAnalyzer for MarkdownDepAnalyzer {
fn description(&self) -> &'static str {
"Scan Markdown files for local file dependencies"
}
fn enabled(&self) -> bool {
self.config.enabled
}
fn auto_detect(&self, file_index: &FileIndex) -> bool {
file_index.has_extension(".md")
}
fn match_product(&self, p: &Product) -> Option<PathBuf> {
if p.inputs.is_empty() {
return None;
}
let source = &p.inputs[0];
let ext = source.extension().and_then(|s| s.to_str()).unwrap_or("");
if ext == "md" {
Some(source.clone())
} else {
None
}
}
fn scan(
&self,
_ctx: &crate::build_context::BuildContext,
source: &Path,
file_index: &FileIndex,
) -> Result<ScanResult> {
self.scan_references(source, file_index)
}
fn fingerprint_parts(&self, _ctx: &crate::build_context::BuildContext) -> Result<Vec<String>> {
super::config_fingerprint(&self.config)
}
}
inventory::submit! {
crate::registries::AnalyzerPlugin {
name: "markdown",
description: "Scan Markdown files for local file dependencies",
is_builtin: true,
version: 2,
create: |toml_value, _| {
let cfg: MarkdownAnalyzerConfig = toml::from_str(&toml::to_string(toml_value)?)?;
Ok(Box::new(MarkdownDepAnalyzer::new(cfg)))
},
defconfig_toml: || {
toml::to_string_pretty(&MarkdownAnalyzerConfig::default()).ok()
},
known_fields: crate::registries::typed_known_fields::<MarkdownAnalyzerConfig>,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reference_targets_cover_the_forms_markdown_names_a_file_in() {
let content = "\


[text](doc/page.md#section)


![by reference][r]
<img src=\"b.png\" alt=\"x\">
<a href='notes.txt'>notes</a>
[r]: a.png \"title\"
[site]: https://example.com/x.png
[web](https://example.com) [mail](mailto:a@b.c) [top](#top) [cdn](//cdn/x.js)
<img src=\"https://example.com/y.png\">
";
let mut targets = reference_targets(content);
targets.sort();
assert_eq!(
targets,
[
"a.png",
"an image.png",
"b.png",
"c.png",
"d.png",
"doc/page.md",
"my img.png",
"notes.txt",
]
);
}
#[test]
fn percent_decode_leaves_what_is_not_an_escape() {
assert_eq!(percent_decode("a%20b%2Fc"), "a b/c");
assert_eq!(percent_decode("100%.png"), "100%.png");
assert_eq!(percent_decode("bad%zz"), "bad%zz");
assert_eq!(percent_decode("caf%C3%A9"), "café");
assert_eq!(percent_decode("lone%FF"), "lone%FF");
}
}