Skip to content

Commit

Permalink
Imporved is_colocated_asset_links (#1972)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JoelMon authored Aug 22, 2022
1 parent 991d7b0 commit 76453d2
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion components/markdown/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ fn insert_many<T>(input: &mut Vec<T>, 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)]
Expand Down Expand Up @@ -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));
}
}
}

0 comments on commit 76453d2

Please sign in to comment.