#absolute to relative path
28 messages · Page 1 of 1 (latest)
Which part are you asking for help with? What exactly is the issue? What have you tried? What "didn't work?"
the problem: i can't find a way to turn an absolute path into relative.
without writing a custom regex
@astral pond
@thick cosmos This depends on how you obtain the path in the first place. And what the relative path is relative to.
i have a path to "cut" the front off.
and the absolute.
it is obtained by a Path type in the rust backend.
it's a string in frontend
Can you provide an example?
- What's the full/absolute path?
- What to do you want to cut off and what part remains?
- How is it supposed to look in the UI?
- How do you plan to display the path and make it clickable (using a link or js?)
do you not know just path lingo?
abs path: full path from drive to file
relative: a short term path (sometimes starting with ./) "relative" to the current path.
already have it displayed and clickable (just a button calling a function)
i already have the UI setup. i just want a way to turn abs to rel
I do know what absolute and relative paths are, but it is hard to know what you mean, for example by "current path". Current path of a file browser? Or of the shell's working directory? A concrete example will make it clearer what exactly you are working with.
If you have the two components of the path in two different variables, it should be easy enough to use JavaScript to subtract the base path from the absolute path to get your relative path.
const absolutePath = '/home/{user}/Documents/file.txt';
const prefix = '/home/{user}'
const relativePath = absolutePath.substring(prefix.length, absolutePath.length);
// console.log(relativePath);
// relativePath contains '/Documents/file.txt'
it's a file system
Sorry, I meant a concrete example with actual paths so we know what you are trying to do, not what absolute/relative paths are in general. 😉
C:/beans/taco/arf.what & C:/beans -> taco/arf.what
Ok great. You can use the code I posted above to get exactly that.
You just need C:\beans in a variable. This may need to change dynamically, depending on where you get your current path from or what the directory separators of your OS are.
also, that is the worst possible way to do that.
also, also, that's the whole point.
i don't want to have to make a custom path formater thing to cut the front off.
i want a lib or smth todo it.
also, i want to join paths too 😄
You are not making yourself easy to help.
This path manipulation stuff is easy to do in Rust. If you want to do it in the JS only, find a browser-compatible implementation somewhere.
I don't suspect you're going to find many browser-compatible packages for cross-platform (assuming that's a concern of yours, it may not be) file path manipulation, because that's not a common concern in browser-specific apps.
As I've said, doing what you want in Rust is very easy. But if you refuse to do that or to write your own JS code to do it, you can search the npm package registry just the same as others here.