Enable linkchecker on books

Previously, mdBook used JavaScript to add header links, so we
skipped checking the book. As of
https://github.com/rust-lang/rust/pull/39966, it no longer does,
so we can start checking again.

There is a twist, though: it uses name instead of id, so let's test
for both. They're both valid links anyway, so it's good to have the
checker check anyway.
This commit is contained in:
Steve Klabnik 2017-02-20 09:30:42 -05:00
parent b4cd3d9206
commit fc7bf8498b
1 changed files with 16 additions and 8 deletions

View File

@ -65,6 +65,7 @@ enum Redirect {
struct FileEntry { struct FileEntry {
source: String, source: String,
ids: HashSet<String>, ids: HashSet<String>,
names: HashSet<String>,
} }
type Cache = HashMap<PathBuf, FileEntry>; type Cache = HashMap<PathBuf, FileEntry>;
@ -81,6 +82,15 @@ impl FileEntry {
}); });
} }
} }
fn parse_names(&mut self, contents: &str) {
if self.names.is_empty() {
with_attrs_in_source(contents, " name", |fragment, _| {
let frag = fragment.trim_left_matches("#").to_owned();
self.names.insert(frag);
});
}
}
} }
fn walk(cache: &mut Cache, root: &Path, dir: &Path, errors: &mut bool) { fn walk(cache: &mut Cache, root: &Path, dir: &Path, errors: &mut bool) {
@ -139,6 +149,9 @@ fn check(cache: &mut Cache,
cache.get_mut(&pretty_file) cache.get_mut(&pretty_file)
.unwrap() .unwrap()
.parse_ids(&pretty_file, &contents, errors); .parse_ids(&pretty_file, &contents, errors);
cache.get_mut(&pretty_file)
.unwrap()
.parse_names(&contents);
} }
// Search for anything that's the regex 'href[ ]*=[ ]*".*?"' // Search for anything that's the regex 'href[ ]*=[ ]*".*?"'
@ -209,13 +222,6 @@ fn check(cache: &mut Cache,
Err(LoadError::IsRedirect) => unreachable!(), Err(LoadError::IsRedirect) => unreachable!(),
}; };
// we don't check the book for fragments because they're added via JS
for book in ["book/", "nomicon/"].iter() {
if !pretty_path.to_str().unwrap().starts_with(book) {
return;
}
}
if let Some(ref fragment) = fragment { if let Some(ref fragment) = fragment {
// Fragments like `#1-6` are most likely line numbers to be // Fragments like `#1-6` are most likely line numbers to be
// interpreted by javascript, so we're ignoring these // interpreted by javascript, so we're ignoring these
@ -226,8 +232,9 @@ fn check(cache: &mut Cache,
let entry = &mut cache.get_mut(&pretty_path).unwrap(); let entry = &mut cache.get_mut(&pretty_path).unwrap();
entry.parse_ids(&pretty_path, &contents, errors); entry.parse_ids(&pretty_path, &contents, errors);
entry.parse_names(&contents);
if !entry.ids.contains(*fragment) { if !(entry.ids.contains(*fragment) || entry.names.contains(*fragment)) {
*errors = true; *errors = true;
print!("{}:{}: broken link fragment ", print!("{}:{}: broken link fragment ",
pretty_file.display(), pretty_file.display(),
@ -277,6 +284,7 @@ fn load_file(cache: &mut Cache,
entry.insert(FileEntry { entry.insert(FileEntry {
source: contents.clone(), source: contents.clone(),
ids: HashSet::new(), ids: HashSet::new(),
names: HashSet::new(),
}); });
} }
maybe maybe