From f19fc78a151c761642cf7c9b654e0da9db3c4418 Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Sun, 13 Aug 2023 20:33:03 +0100 Subject: More robust relative path handling --- src/lib/relativePath.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/lib/relativePath.ts (limited to 'src/lib') diff --git a/src/lib/relativePath.ts b/src/lib/relativePath.ts new file mode 100644 index 0000000..ad8582c --- /dev/null +++ b/src/lib/relativePath.ts @@ -0,0 +1,12 @@ +export default function (currentPath: string, relativePath: string) { + if (relativePath.length > 2 && relativePath.slice(0, 3) === '../') { + throw new Error('Cannot calculate relative path because it goes up a directory.'); + } + + if (currentPath.at(-1) !== '/') { + currentPath = currentPath + '/'; + } + relativePath = relativePath.replace(/^\.?\//, ''); + + return currentPath + relativePath; +} -- cgit v1.2.3