Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved is_colocated_asset_links #1972

Merged
merged 1 commit into from
Aug 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
}