From 567f103ee32febab98bc821ee0b6d8136aec4caf Mon Sep 17 00:00:00 2001 From: Joel Montes de Oca <6587811+JoelMon@users.noreply.github.com> Date: Mon, 22 Aug 2022 16:34:23 -0400 Subject: [PATCH] Imporved is_colocated_asset_links (#1972) This PR includes the following: - Added a check for `../` which would check to see if a file is in a _sub_ directory - Wrote test for `is_colocated_asset_links` This PR is similare to PR #1969 --- components/markdown/src/markdown.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/components/markdown/src/markdown.rs b/components/markdown/src/markdown.rs index 63007e12..1b08cea3 100644 --- a/components/markdown/src/markdown.rs +++ b/components/markdown/src/markdown.rs @@ -56,7 +56,10 @@ fn insert_many(input: &mut Vec, elem_to_insert: Vec<(usize, T)>) { /// Colocated asset links refers to the files in the same directory. fn is_colocated_asset_link(link: &str) -> bool { - !link.starts_with('/') && !link.starts_with('#') && !STARTS_WITH_SCHEMA_RE.is_match(link) + !link.starts_with('/') + && !link.starts_with("..") + && !link.starts_with('#') + && !STARTS_WITH_SCHEMA_RE.is_match(link) } #[derive(Debug)] @@ -606,4 +609,22 @@ mod tests { assert!(!is_external_link("http.jpg")) } + + #[test] + // Tests for link that points to files in the same directory + fn test_is_colocated_asset_link_true() { + let links: [&str; 2] = ["./same-dir.md", "file.md"]; + for link in links { + assert!(is_colocated_asset_link(link)); + } + } + + #[test] + // Tests for files where the link points to a different directory + fn test_is_colocated_asset_link_false() { + let links: [&str; 2] = ["/other-dir/file.md", "../sub-dir/file.md"]; + for link in links { + assert!(!is_colocated_asset_link(link)); + } + } }