From 20033c1ee2a7ddefeea4fcbe7290d450e81c9060 Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Thu, 8 Aug 2024 14:45:00 +0200 Subject: [PATCH] Fix wrongly reported NotInSummary errors if -f option is used Fixes: #77, #86 --- src/lib.rs | 26 ++++++++++++++------------ src/validate.rs | 43 +++++++++++++++++++++++-------------------- tests/smoke_tests.rs | 18 ++++++++++-------- 3 files changed, 47 insertions(+), 40 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3cef84a83..98d68a656 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -138,35 +138,36 @@ pub fn version_check(version: &str) -> Result<(), Error> { } } -/// A helper for reading the chapters of a [`Book`] into memory, filtering out -/// files using the given `filter`. +/// A helper for reading the chapters of a [`Book`] into memory. pub fn load_files_into_memory( book: &Book, dest: &mut Files, filter: F, -) -> Vec +) -> (Vec, Vec) where F: Fn(&Path) -> bool, { - let mut ids = Vec::new(); + let mut filtered_files: Vec = Vec::new(); + let mut all_files: Vec = Vec::new(); for item in book.iter() { match item { BookItem::Chapter(ref ch) => { if let Some(ref path) = ch.path { + let path_str = path.display().to_string(); + let content = ch.content.clone(); + let id = dest.add(path_str, content); if filter(path) { - let path_str = path.display().to_string(); - let content = ch.content.clone(); - let id = dest.add(path_str, content); - ids.push(id); + filtered_files.push(id); } + all_files.push(id); } }, BookItem::Separator | BookItem::PartTitle(_) => {}, } } - ids + (filtered_files, all_files) } fn report_errors( @@ -195,10 +196,10 @@ where { log::info!("Scanning book for links"); let mut files: Files = Files::new(); - let file_ids = + let (filtered_files_ids, all_file_ids) = crate::load_files_into_memory(&ctx.book, &mut files, file_filter); let (links, incomplete_links) = - crate::extract_links(cfg, file_ids.clone(), &files); + crate::extract_links(cfg, filtered_files_ids.clone(), &files); log::info!( "Found {} links ({} incomplete links)", links.len(), @@ -212,7 +213,8 @@ where &src, cache, &files, - &file_ids, + &filtered_files_ids, + &all_file_ids, incomplete_links, )?; diff --git a/src/validate.rs b/src/validate.rs index 71789b516..268df8340 100644 --- a/src/validate.rs +++ b/src/validate.rs @@ -21,9 +21,9 @@ fn lc_validate( src_dir: &Path, cache: &mut Cache, files: &Files, - file_ids: &[FileId], + all_files_ids: &[FileId], ) -> Outcomes { - let file_names = file_ids + let file_names = all_files_ids .iter() .map(|id| files.name(*id).to_os_string()) .collect(); @@ -90,21 +90,21 @@ fn ensure_included_in_book( // Not part of the book. Err(_) => return Ok(()), }; - let was_included_in_summary = - file_names.iter().any(|summary_path| { - let summary_path = Path::new(summary_path); - if summary_path.parent() != resolved_link.parent() { - return false; - } - match (summary_path.file_name(), resolved_link.file_name()) { - (a, b) if a == b => true, - (Some(summary), Some(resolved)) => { - // index preprocessor rewrites summary paths before we get to them. - summary == Path::new("index.md") && resolved == Path::new("README.md") - } - _ => false, - } - }); + let was_included_in_summary = file_names.iter().any(|summary_path| { + let summary_path = Path::new(summary_path); + if summary_path.parent() != resolved_link.parent() { + return false; + } + match (summary_path.file_name(), resolved_link.file_name()) { + (a, b) if a == b => true, + (Some(summary), Some(resolved)) => { + // index preprocessor rewrites summary paths before we get to them. + summary == Path::new("index.md") + && resolved == Path::new("README.md") + }, + _ => false, + } + }); let ext = resolved_link.extension(); let is_markdown = ext == Some(OsStr::new("md")); @@ -183,7 +183,9 @@ fn merge_outcomes( }); items } - fn sorted_link(items: Vec) -> Vec { sorted(items, |link| link) } + fn sorted_link(items: Vec) -> Vec { + sorted(items, |link| link) + } ValidationOutcome { invalid_links: sorted(outcomes.invalid, |l| &l.link), @@ -201,10 +203,11 @@ pub fn validate( src_dir: &Path, cache: &mut Cache, files: &Files, - file_ids: &[FileId], + filtered_files_ids: &[FileId], + all_files_ids: &[FileId], incomplete_links: Vec, ) -> Result { - let got = lc_validate(links, cfg, src_dir, cache, files, file_ids); + let got = lc_validate(links, cfg, src_dir, cache, files, all_files_ids); Ok(merge_outcomes(got, incomplete_links)) } diff --git a/tests/smoke_tests.rs b/tests/smoke_tests.rs index 6dda5bbe9..5e3b38073 100644 --- a/tests/smoke_tests.rs +++ b/tests/smoke_tests.rs @@ -326,14 +326,15 @@ impl Renderer for TestRun { let noop_filter = |_: &Path| true; - let file_ids = mdbook_linkcheck::load_files_into_memory( - &ctx.book, - &mut files, - noop_filter, - ); + let (filtered_files_ids, all_files_ids) = + mdbook_linkcheck::load_files_into_memory( + &ctx.book, + &mut files, + noop_filter, + ); let (links, incomplete) = mdbook_linkcheck::extract_links( &self.config, - file_ids.clone(), + filtered_files_ids.clone(), &files, ); @@ -344,11 +345,12 @@ impl Renderer for TestRun { &src, &mut cache, &files, - &file_ids, + &filtered_files_ids, + &all_files_ids, incomplete, )?; - (self.after_validation)(&files, &outcome, &file_ids); + (self.after_validation)(&files, &outcome, &filtered_files_ids); self.validation_outcome.set(Some(outcome)); Ok(())