Skip to content

Commit d3d7f6a

Browse files
authored
fix: do not fail on empty # and #top fragments (#1609)
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. Credits go to @thiru-appitap for initial attempt and helping to find missing parts of the implementation. Solves: #1599 Signed-off-by: MichaIng <[email protected]>
1 parent e29a3c5 commit d3d7f6a

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

fixtures/fragments/file.html

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<a href="#Upper-ÄÖö">back to Upper-ÄÖö</a><br>
2323
<a href="#Upper-%C3%84%C3%96%C3%B6">back to öüä encoded</a><br>
2424
<a href="#in-the-end">doesn't exist</a><br>
25+
<a href="#">To the top</a><br>
26+
<a href="#top">To the top alt</a><br>
2527
</section>
2628
</body>
2729
</html>

fixtures/fragments/file1.md

+8
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,12 @@ Therefore we put the test into a code block for now to prevent false positives.
5454
[Link to umlauts wrong case](#fünf-sÜße-Äpfel)
5555
[Link to umlauts with percent encoding](#f%C3%BCnf-s%C3%BC%C3%9Fe-%C3%A4pfel)
5656

57+
# To top fragments
58+
59+
The empty "#" and "#top" fragments are always valid
60+
without related HTML element. Browser will scroll to the top of the page.
61+
62+
[Link to top of file2](file2.md#)
63+
[Alternative link to top of file2](file2.md#top)
64+
5765
##### Lets wear a hat: être

lychee-bin/tests/cli.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1835,8 +1835,10 @@ mod cli {
18351835
.stderr(contains(
18361836
"fixtures/fragments/file1.md#kebab-case-fragment-1",
18371837
))
1838-
.stdout(contains("21 Total"))
1839-
.stdout(contains("17 OK"))
1838+
.stderr(contains("fixtures/fragments/file.html#top"))
1839+
.stderr(contains("fixtures/fragments/file2.md#top"))
1840+
.stdout(contains("25 Total"))
1841+
.stdout(contains("21 OK"))
18401842
// 4 failures because of missing fragments
18411843
.stdout(contains("4 Errors"));
18421844
}

lychee-lib/src/utils/fragment_checker.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,18 @@ impl FragmentChecker {
3939

4040
/// Checks if the given path contains the given fragment.
4141
///
42-
/// Returns false, if there is a fragment in the link and the path is to a
43-
/// Markdown file, which doesn't contain the given fragment.
42+
/// Returns false, if there is a fragment in the link which is not empty or "top"
43+
/// and the path is to a Markdown file, which doesn't contain the given fragment.
44+
/// (Empty # and #top fragments are always valid, triggering the browser to scroll to top.)
4445
///
4546
/// In all other cases, returns true.
4647
pub(crate) async fn check(&self, path: &Path, url: &Url) -> Result<bool> {
4748
let Some(fragment) = url.fragment() else {
4849
return Ok(true);
4950
};
51+
if fragment.is_empty() || fragment.eq_ignore_ascii_case("top") {
52+
return Ok(true);
53+
};
5054
let mut fragment_decoded = percent_decode_str(fragment).decode_utf8()?;
5155
let url_without_frag = Self::remove_fragment(url.clone());
5256

0 commit comments

Comments
 (0)