Skip to content

Commit 20033c1

Browse files
committed
Fix wrongly reported NotInSummary errors if -f option is used
Fixes: Michael-F-Bryan#77, Michael-F-Bryan#86
1 parent bffff60 commit 20033c1

File tree

3 files changed

+47
-40
lines changed

3 files changed

+47
-40
lines changed

src/lib.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -138,35 +138,36 @@ pub fn version_check(version: &str) -> Result<(), Error> {
138138
}
139139
}
140140

141-
/// A helper for reading the chapters of a [`Book`] into memory, filtering out
142-
/// files using the given `filter`.
141+
/// A helper for reading the chapters of a [`Book`] into memory.
143142
pub fn load_files_into_memory<F>(
144143
book: &Book,
145144
dest: &mut Files<String>,
146145
filter: F,
147-
) -> Vec<FileId>
146+
) -> (Vec<FileId>, Vec<FileId>)
148147
where
149148
F: Fn(&Path) -> bool,
150149
{
151-
let mut ids = Vec::new();
150+
let mut filtered_files: Vec<FileId> = Vec::new();
151+
let mut all_files: Vec<FileId> = Vec::new();
152152

153153
for item in book.iter() {
154154
match item {
155155
BookItem::Chapter(ref ch) => {
156156
if let Some(ref path) = ch.path {
157+
let path_str = path.display().to_string();
158+
let content = ch.content.clone();
159+
let id = dest.add(path_str, content);
157160
if filter(path) {
158-
let path_str = path.display().to_string();
159-
let content = ch.content.clone();
160-
let id = dest.add(path_str, content);
161-
ids.push(id);
161+
filtered_files.push(id);
162162
}
163+
all_files.push(id);
163164
}
164165
},
165166
BookItem::Separator | BookItem::PartTitle(_) => {},
166167
}
167168
}
168169

169-
ids
170+
(filtered_files, all_files)
170171
}
171172

172173
fn report_errors(
@@ -195,10 +196,10 @@ where
195196
{
196197
log::info!("Scanning book for links");
197198
let mut files: Files<String> = Files::new();
198-
let file_ids =
199+
let (filtered_files_ids, all_file_ids) =
199200
crate::load_files_into_memory(&ctx.book, &mut files, file_filter);
200201
let (links, incomplete_links) =
201-
crate::extract_links(cfg, file_ids.clone(), &files);
202+
crate::extract_links(cfg, filtered_files_ids.clone(), &files);
202203
log::info!(
203204
"Found {} links ({} incomplete links)",
204205
links.len(),
@@ -212,7 +213,8 @@ where
212213
&src,
213214
cache,
214215
&files,
215-
&file_ids,
216+
&filtered_files_ids,
217+
&all_file_ids,
216218
incomplete_links,
217219
)?;
218220

src/validate.rs

+23-20
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ fn lc_validate(
2121
src_dir: &Path,
2222
cache: &mut Cache,
2323
files: &Files<String>,
24-
file_ids: &[FileId],
24+
all_files_ids: &[FileId],
2525
) -> Outcomes {
26-
let file_names = file_ids
26+
let file_names = all_files_ids
2727
.iter()
2828
.map(|id| files.name(*id).to_os_string())
2929
.collect();
@@ -90,21 +90,21 @@ fn ensure_included_in_book(
9090
// Not part of the book.
9191
Err(_) => return Ok(()),
9292
};
93-
let was_included_in_summary =
94-
file_names.iter().any(|summary_path| {
95-
let summary_path = Path::new(summary_path);
96-
if summary_path.parent() != resolved_link.parent() {
97-
return false;
98-
}
99-
match (summary_path.file_name(), resolved_link.file_name()) {
100-
(a, b) if a == b => true,
101-
(Some(summary), Some(resolved)) => {
102-
// index preprocessor rewrites summary paths before we get to them.
103-
summary == Path::new("index.md") && resolved == Path::new("README.md")
104-
}
105-
_ => false,
106-
}
107-
});
93+
let was_included_in_summary = file_names.iter().any(|summary_path| {
94+
let summary_path = Path::new(summary_path);
95+
if summary_path.parent() != resolved_link.parent() {
96+
return false;
97+
}
98+
match (summary_path.file_name(), resolved_link.file_name()) {
99+
(a, b) if a == b => true,
100+
(Some(summary), Some(resolved)) => {
101+
// index preprocessor rewrites summary paths before we get to them.
102+
summary == Path::new("index.md")
103+
&& resolved == Path::new("README.md")
104+
},
105+
_ => false,
106+
}
107+
});
108108
let ext = resolved_link.extension();
109109
let is_markdown = ext == Some(OsStr::new("md"));
110110

@@ -183,7 +183,9 @@ fn merge_outcomes(
183183
});
184184
items
185185
}
186-
fn sorted_link(items: Vec<Link>) -> Vec<Link> { sorted(items, |link| link) }
186+
fn sorted_link(items: Vec<Link>) -> Vec<Link> {
187+
sorted(items, |link| link)
188+
}
187189

188190
ValidationOutcome {
189191
invalid_links: sorted(outcomes.invalid, |l| &l.link),
@@ -201,10 +203,11 @@ pub fn validate(
201203
src_dir: &Path,
202204
cache: &mut Cache,
203205
files: &Files<String>,
204-
file_ids: &[FileId],
206+
filtered_files_ids: &[FileId],
207+
all_files_ids: &[FileId],
205208
incomplete_links: Vec<IncompleteLink>,
206209
) -> Result<ValidationOutcome, Error> {
207-
let got = lc_validate(links, cfg, src_dir, cache, files, file_ids);
210+
let got = lc_validate(links, cfg, src_dir, cache, files, all_files_ids);
208211
Ok(merge_outcomes(got, incomplete_links))
209212
}
210213

tests/smoke_tests.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,15 @@ impl Renderer for TestRun {
326326

327327
let noop_filter = |_: &Path| true;
328328

329-
let file_ids = mdbook_linkcheck::load_files_into_memory(
330-
&ctx.book,
331-
&mut files,
332-
noop_filter,
333-
);
329+
let (filtered_files_ids, all_files_ids) =
330+
mdbook_linkcheck::load_files_into_memory(
331+
&ctx.book,
332+
&mut files,
333+
noop_filter,
334+
);
334335
let (links, incomplete) = mdbook_linkcheck::extract_links(
335336
&self.config,
336-
file_ids.clone(),
337+
filtered_files_ids.clone(),
337338
&files,
338339
);
339340

@@ -344,11 +345,12 @@ impl Renderer for TestRun {
344345
&src,
345346
&mut cache,
346347
&files,
347-
&file_ids,
348+
&filtered_files_ids,
349+
&all_files_ids,
348350
incomplete,
349351
)?;
350352

351-
(self.after_validation)(&files, &outcome, &file_ids);
353+
(self.after_validation)(&files, &outcome, &filtered_files_ids);
352354

353355
self.validation_outcome.set(Some(outcome));
354356
Ok(())

0 commit comments

Comments
 (0)