@@ -3,24 +3,22 @@ def remove_start_slash(input_path : String) : String
33end
44
55def get_relative_path (base_path : String , path : String ) : String
6- # Handle special case where base_path is "." (current directory)
7- # Without this, .sub(".", "") would remove the first "." found anywhere in the path
8- # (e.g., removing the "." from ".php" extension)
9- if base_path == "."
10- return path
11- .sub(/^\.\//, "") # Remove leading "./" only at the start
12- .sub("//", "/")
13- .lstrip('/')
14- end
15-
16- # Ensure base_path ends with slash for consistent substitution
17- base = base_path.ends_with?("/") ? base_path : "#{base_path}/"
18-
19- # Remove base path and normalize
20- relative_path = path
21- .sub(base, "")
22- .sub(base_path, "") # Fallback if base doesn't end with /
23- .sub(/^\.\//, "") # Remove leading "./" only at the start
6+ # First, determine the path relative to the base_path, without other normalization.
7+ unstripped_path = if base_path == " ."
8+ # When base_path is ".", the path is already relative.
9+ # This avoids an issue where `.sub(".", "")` would remove the dot from file extensions.
10+ path
11+ else
12+ # For other base paths, remove the base path prefix.
13+ base = base_path.ends_with?(" /" ) ? base_path : " #{ base_path } /"
14+ path
15+ .sub(base, " " )
16+ .sub(base_path, " " ) # Fallback if base doesn't end with /
17+ end
18+
19+ # Then, normalize the resulting path.
20+ relative_path = unstripped_path
21+ .sub(/^\.\/ / , " " ) # Remove leading "./" only at the start
2422 .sub(" //" , " /" )
2523
2624 remove_start_slash(relative_path)
0 commit comments