Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrongly reported NotInSummary errors if -f option is used #87

Closed
Closed
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
26 changes: 14 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F>(
book: &Book,
dest: &mut Files<String>,
filter: F,
) -> Vec<FileId>
) -> (Vec<FileId>, Vec<FileId>)
where
F: Fn(&Path) -> bool,
{
let mut ids = Vec::new();
let mut filtered_files: Vec<FileId> = Vec::new();
let mut all_files: Vec<FileId> = 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(
Expand Down Expand Up @@ -195,10 +196,10 @@ where
{
log::info!("Scanning book for links");
let mut files: Files<String> = 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(),
Expand All @@ -212,7 +213,8 @@ where
&src,
cache,
&files,
&file_ids,
&filtered_files_ids,
&all_file_ids,
incomplete_links,
)?;

Expand Down
43 changes: 23 additions & 20 deletions src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ fn lc_validate(
src_dir: &Path,
cache: &mut Cache,
files: &Files<String>,
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();
Expand Down Expand Up @@ -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"));

Expand Down Expand Up @@ -183,7 +183,9 @@ fn merge_outcomes(
});
items
}
fn sorted_link(items: Vec<Link>) -> Vec<Link> { sorted(items, |link| link) }
fn sorted_link(items: Vec<Link>) -> Vec<Link> {
sorted(items, |link| link)
}

ValidationOutcome {
invalid_links: sorted(outcomes.invalid, |l| &l.link),
Expand All @@ -201,10 +203,11 @@ pub fn validate(
src_dir: &Path,
cache: &mut Cache,
files: &Files<String>,
file_ids: &[FileId],
filtered_files_ids: &[FileId],
all_files_ids: &[FileId],
incomplete_links: Vec<IncompleteLink>,
) -> Result<ValidationOutcome, Error> {
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))
}

Expand Down
18 changes: 10 additions & 8 deletions tests/smoke_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand All @@ -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(())
Expand Down