Skip to content

Commit 786ed1b

Browse files
committed
feat: add getRelativePath function to compute relative paths based on current location
1 parent bdcf48a commit 786ed1b

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
}
4141
}
4242

43+
function getRelativePath(path) {
44+
if (!path && isBrowser) {
45+
path = window.location.pathname.replace(/\/[^\/]*$/, ""); // Remove file from path
46+
}
47+
if (!path.endsWith("/")) {
48+
path += "/";
49+
}
50+
let depth = path.split("/").filter(Boolean).length;
51+
return depth > 0 ? "../".repeat(depth) : "./";
52+
}
53+
4354
/**
4455
* Generates an ObjectId
4556
*/
@@ -1248,6 +1259,7 @@
12481259
if (isBrowser) clickedElement();
12491260

12501261
return {
1262+
getRelativePath,
12511263
ObjectId,
12521264
checkValue,
12531265
isValidDate,

0 commit comments

Comments
 (0)