Skip to main content

lychee_lib/utils/
url.rs

1use std::sync::LazyLock;
2
3use linkify::LinkFinder;
4
5static LINK_FINDER: LazyLock<LinkFinder> = LazyLock::new(LinkFinder::new);
6
7/// Remove all GET parameters from a URL and separates out the fragment.
8/// The link is not a URL but a String as it may not have a base domain.
9pub(crate) fn remove_get_params_and_separate_fragment(url: &str) -> (&str, Option<&str>) {
10    let (path, frag) = match url.split_once('#') {
11        Some((path, fragment)) => (path, Some(fragment)),
12        None => (url, None),
13    };
14    let path = match path.split_once('?') {
15        Some((path_without_params, _params)) => path_without_params,
16        None => path,
17    };
18    (path, frag)
19}
20
21// Use `LinkFinder` to offload the raw link searching in plaintext
22pub(crate) fn find_links(input: &str) -> impl Iterator<Item = linkify::Link<'_>> {
23    LINK_FINDER.links(input)
24}
25
26#[cfg(test)]
27mod test_fs_tree {
28    use super::*;
29
30    #[test]
31    fn test_remove_get_params_and_fragment() {
32        assert_eq!(remove_get_params_and_separate_fragment("/"), ("/", None));
33        assert_eq!(
34            remove_get_params_and_separate_fragment("index.html?foo=bar"),
35            ("index.html", None)
36        );
37        assert_eq!(
38            remove_get_params_and_separate_fragment("/index.html?foo=bar"),
39            ("/index.html", None)
40        );
41        assert_eq!(
42            remove_get_params_and_separate_fragment("/index.html?foo=bar&baz=zorx?bla=blub"),
43            ("/index.html", None)
44        );
45        assert_eq!(
46            remove_get_params_and_separate_fragment("https://example.com/index.html?foo=bar"),
47            ("https://example.com/index.html", None)
48        );
49        assert_eq!(
50            remove_get_params_and_separate_fragment("test.png?foo=bar"),
51            ("test.png", None)
52        );
53
54        assert_eq!(
55            remove_get_params_and_separate_fragment("https://example.com/index.html#anchor"),
56            ("https://example.com/index.html", Some("anchor"))
57        );
58        assert_eq!(
59            remove_get_params_and_separate_fragment(
60                "https://example.com/index.html?foo=bar#anchor"
61            ),
62            ("https://example.com/index.html", Some("anchor"))
63        );
64        assert_eq!(
65            remove_get_params_and_separate_fragment("test.png?foo=bar#anchor"),
66            ("test.png", Some("anchor"))
67        );
68        assert_eq!(
69            remove_get_params_and_separate_fragment("test.png#anchor?anchor!?"),
70            ("test.png", Some("anchor?anchor!?"))
71        );
72        assert_eq!(
73            remove_get_params_and_separate_fragment("test.png?foo=bar#anchor?anchor!"),
74            ("test.png", Some("anchor?anchor!"))
75        );
76    }
77}