Skip to content

Commit

Permalink
fix: do not fail on empty # and #top fragments
Browse files Browse the repository at this point in the history
The empty "#" and "#top" fragments are always valid without related HTML element. Browser will scroll to the top of the page. Hence lychee must not fail on those.

Signed-off-by: MichaIng <[email protected]>
  • Loading branch information
MichaIng committed Jan 8, 2025
1 parent a11d515 commit bd21857
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions fixtures/fragments/file.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<a href="#Upper-ÄÖö">back to Upper-ÄÖö</a><br>
<a href="#Upper-%C3%84%C3%96%C3%B6">back to öüä encoded</a><br>
<a href="#in-the-end">doesn't exist</a><br>
<a href="#">To the top</a><br>
<a href="#top">To the top alt</a><br>
</section>
</body>
</html>
8 changes: 8 additions & 0 deletions fixtures/fragments/file1.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ Therefore we put the test into a code block for now to prevent false positives.
[Link to umlauts wrong case](#fünf-sÜße-Äpfel)
[Link to umlauts with percent encoding](#f%C3%BCnf-s%C3%BC%C3%9Fe-%C3%A4pfel)

# To top fragments

The empty "#" and "#top" fragments are always valid
without related HTML element. Browser will scroll to the top of the page.

[Link to top of file2](file2.md#)
[Alternative link to top of file2](file2.md#top)

##### Lets wear a hat: être
6 changes: 4 additions & 2 deletions lychee-bin/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1834,8 +1834,10 @@ mod cli {
.stderr(contains(
"fixtures/fragments/file1.md#kebab-case-fragment-1",
))
.stdout(contains("21 Total"))
.stdout(contains("17 OK"))
.stderr(contains("fixtures/fragments/file.html#top"))
.stderr(contains("fixtures/fragments/file2.md#top"))
.stdout(contains("25 Total"))
.stdout(contains("21 OK"))
// 4 failures because of missing fragments
.stdout(contains("4 Errors"));
}
Expand Down
6 changes: 4 additions & 2 deletions lychee-lib/src/utils/fragment_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ impl FragmentChecker {
Entry::Vacant(entry) => {
let content = fs::read_to_string(path).await?;
let file_frags = extractor(&content);
let contains_fragment =
file_frags.contains(fragment) || file_frags.contains(&fragment_decoded as &str);
let contains_fragment = fragment.is_empty()
|| fragment == "top"
|| file_frags.contains(fragment)
|| file_frags.contains(&fragment_decoded as &str);
entry.insert(file_frags);
Ok(contains_fragment)
}
Expand Down

0 comments on commit bd21857

Please sign in to comment.