Remove anchor from file links

This commit is contained in:
Matthias 2021-09-07 00:20:09 +02:00
parent b2ce61357f
commit 5d0b95271d
3 changed files with 40 additions and 11 deletions

View file

@ -12,6 +12,9 @@
<li>
<a href="/about">About</a>
</li>
<li>
<a href="/about#fragment">About</a>
</li>
</ul>
</p>
</body>

View file

@ -123,7 +123,7 @@ fn extract_links_from_plaintext(input: &str) -> Vec<String> {
}
fn create_uri_from_path(root: &Path, base: &Option<Base>, link: &str) -> Result<Url> {
let link = url::remove_get_params(link);
let link = url::remove_get_params_and_fragment(link);
let path = path::resolve(root, &PathBuf::from(&link), base)?;
Url::from_file_path(&path).map_err(|_e| ErrorKind::InvalidPath(path))
}

View file

@ -2,11 +2,15 @@ use linkify::LinkFinder;
/// Remove all GET parameters from a URL.
/// The link is not a URL but a String as it may not have a base domain.
pub(crate) fn remove_get_params(url: &str) -> &str {
let path = match url.split_once('?') {
Some((path, _params)) => path,
pub(crate) fn remove_get_params_and_fragment(url: &str) -> &str {
let path = match url.split_once('#') {
Some((path_without_fragment, _fragment)) => path_without_fragment,
None => url,
};
let path = match path.split_once('?') {
Some((path_without_params, _params)) => path_without_params,
None => path,
};
path
}
@ -42,18 +46,40 @@ mod test_fs_tree {
}
#[test]
fn test_remove_get_params() {
assert_eq!(remove_get_params("/"), "/");
assert_eq!(remove_get_params("index.html?foo=bar"), "index.html");
assert_eq!(remove_get_params("/index.html?foo=bar"), "/index.html");
fn test_remove_get_params_and_fragment() {
assert_eq!(remove_get_params_and_fragment("/"), "/");
assert_eq!(
remove_get_params("/index.html?foo=bar&baz=zorx?bla=blub"),
remove_get_params_and_fragment("index.html?foo=bar"),
"index.html"
);
assert_eq!(
remove_get_params_and_fragment("/index.html?foo=bar"),
"/index.html"
);
assert_eq!(
remove_get_params("https://example.org/index.html?foo=bar"),
remove_get_params_and_fragment("/index.html?foo=bar&baz=zorx?bla=blub"),
"/index.html"
);
assert_eq!(
remove_get_params_and_fragment("https://example.org/index.html?foo=bar"),
"https://example.org/index.html"
);
assert_eq!(remove_get_params("test.png?foo=bar"), "test.png");
assert_eq!(
remove_get_params_and_fragment("test.png?foo=bar"),
"test.png"
);
assert_eq!(
remove_get_params_and_fragment("https://example.org/index.html#anchor"),
"https://example.org/index.html"
);
assert_eq!(
remove_get_params_and_fragment("https://example.org/index.html?foo=bar#anchor"),
"https://example.org/index.html"
);
assert_eq!(
remove_get_params_and_fragment("test.png?foo=bar#anchor"),
"test.png"
);
}
}