Skip to content

Commit

Permalink
Enable linkchecker on books
Browse files Browse the repository at this point in the history
Previously, mdBook used JavaScript to add header links, so we
skipped checking the book. As of
rust-lang#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.
  • Loading branch information
steveklabnik committed Feb 20, 2017
1 parent b4cd3d9 commit fc7bf84
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/tools/linkchecker/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ enum Redirect {
struct FileEntry {
source: String,
ids: HashSet<String>,
names: HashSet<String>,
}

type Cache = HashMap<PathBuf, FileEntry>;
Expand All @@ -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) {
Expand Down Expand Up @@ -139,6 +149,9 @@ fn check(cache: &mut Cache,
cache.get_mut(&pretty_file)
.unwrap()
.parse_ids(&pretty_file, &contents, errors);
cache.get_mut(&pretty_file)
.unwrap()
.parse_names(&contents);
}

// Search for anything that's the regex 'href[ ]*=[ ]*".*?"'
Expand Down Expand Up @@ -209,13 +222,6 @@ fn check(cache: &mut Cache,
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 {
// Fragments like `#1-6` are most likely line numbers to be
// interpreted by javascript, so we're ignoring these
Expand All @@ -226,8 +232,9 @@ fn check(cache: &mut Cache,

let entry = &mut cache.get_mut(&pretty_path).unwrap();
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;
print!("{}:{}: broken link fragment ",
pretty_file.display(),
Expand Down Expand Up @@ -277,6 +284,7 @@ fn load_file(cache: &mut Cache,
entry.insert(FileEntry {
source: contents.clone(),
ids: HashSet::new(),
names: HashSet::new(),
});
}
maybe
Expand Down

0 comments on commit fc7bf84

Please sign in to comment.