Skip to content

Commit fc7bf84

Browse files
committed
Enable linkchecker on books
Previously, mdBook used JavaScript to add header links, so we skipped checking the book. As of #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.
1 parent b4cd3d9 commit fc7bf84

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/tools/linkchecker/main.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ enum Redirect {
6565
struct FileEntry {
6666
source: String,
6767
ids: HashSet<String>,
68+
names: HashSet<String>,
6869
}
6970

7071
type Cache = HashMap<PathBuf, FileEntry>;
@@ -81,6 +82,15 @@ impl FileEntry {
8182
});
8283
}
8384
}
85+
86+
fn parse_names(&mut self, contents: &str) {
87+
if self.names.is_empty() {
88+
with_attrs_in_source(contents, " name", |fragment, _| {
89+
let frag = fragment.trim_left_matches("#").to_owned();
90+
self.names.insert(frag);
91+
});
92+
}
93+
}
8494
}
8595

8696
fn walk(cache: &mut Cache, root: &Path, dir: &Path, errors: &mut bool) {
@@ -139,6 +149,9 @@ fn check(cache: &mut Cache,
139149
cache.get_mut(&pretty_file)
140150
.unwrap()
141151
.parse_ids(&pretty_file, &contents, errors);
152+
cache.get_mut(&pretty_file)
153+
.unwrap()
154+
.parse_names(&contents);
142155
}
143156

144157
// Search for anything that's the regex 'href[ ]*=[ ]*".*?"'
@@ -209,13 +222,6 @@ fn check(cache: &mut Cache,
209222
Err(LoadError::IsRedirect) => unreachable!(),
210223
};
211224

212-
// we don't check the book for fragments because they're added via JS
213-
for book in ["book/", "nomicon/"].iter() {
214-
if !pretty_path.to_str().unwrap().starts_with(book) {
215-
return;
216-
}
217-
}
218-
219225
if let Some(ref fragment) = fragment {
220226
// Fragments like `#1-6` are most likely line numbers to be
221227
// interpreted by javascript, so we're ignoring these
@@ -226,8 +232,9 @@ fn check(cache: &mut Cache,
226232

227233
let entry = &mut cache.get_mut(&pretty_path).unwrap();
228234
entry.parse_ids(&pretty_path, &contents, errors);
235+
entry.parse_names(&contents);
229236

230-
if !entry.ids.contains(*fragment) {
237+
if !(entry.ids.contains(*fragment) || entry.names.contains(*fragment)) {
231238
*errors = true;
232239
print!("{}:{}: broken link fragment ",
233240
pretty_file.display(),
@@ -277,6 +284,7 @@ fn load_file(cache: &mut Cache,
277284
entry.insert(FileEntry {
278285
source: contents.clone(),
279286
ids: HashSet::new(),
287+
names: HashSet::new(),
280288
});
281289
}
282290
maybe

0 commit comments

Comments
 (0)