Skip to content

Commit

Permalink
Merge pull request #11 from web-infra-dev/fix/image-relative-link
Browse files Browse the repository at this point in the history
fix: preserve image relative link
  • Loading branch information
sanyuan0704 committed Oct 10, 2023
2 parents 433e55f + 10b188a commit 98e521e
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions crates/plugin_normalize_link/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,23 @@ fn normalize_link(url: &String, root: &String, filepath: &String) -> String {
let mut url = url.to_string();
let root_path = Path::new(root);
let file_path = Path::new(filepath);
// find the extname(before hash)
// first, find the hash
let hash_index = url.rfind('#').or_else(|| Some(url.len())).unwrap();
// then, find the extname
let extname = match url[..hash_index].rfind('.') {
Some(index) => url[index..hash_index].to_string(),
None => "".to_string(),
};

let is_image = IMAGE_EXTNAMES.contains(&extname.as_str());

if let Ok(relative_path) = file_path.strip_prefix(root_path) {
if url.starts_with(".") {
// If the url is a image and relative path, return directly
if is_image {
return url;
}
let mut base_dir = relative_path.parent().unwrap();

if url.starts_with("./") {
Expand All @@ -65,17 +79,8 @@ fn normalize_link(url: &String, root: &String, filepath: &String) -> String {
url = format!("/{}", url);
}

// find the extname(before hash)
// first, find the hash
let hash_index = url.rfind('#').or_else(|| Some(url.len())).unwrap();
// then, find the extname
let extname = match url[..hash_index].rfind('.') {
Some(index) => url[index..hash_index].to_string(),
None => "".to_string(),
};

// remove extname if it is not an image
if !extname.is_empty() && !IMAGE_EXTNAMES.contains(&extname.as_str()) {
if !extname.is_empty() && !is_image {
url = url.replace(&extname, "");
}
}
Expand Down Expand Up @@ -333,7 +338,7 @@ mod tests {
if let hast::Node::MdxjsEsm(esm) = &root.children[0] {
assert_eq!(
esm.value,
"import image_0 from \"/assets/a.png\";".to_string()
"import image_0 from \"../../assets/a.png\";".to_string()
);
}
if let hast::Node::MdxJsxElement(element) = &root.children[1] {
Expand Down

0 comments on commit 98e521e

Please sign in to comment.