(path) create a resolve path service to manage …

resolve path like ../..
This commit is contained in:
2021-03-21 15:26:27 +01:00
parent 299bc75529
commit 3749588b0b
7 changed files with 91 additions and 38 deletions

View File

@@ -0,0 +1,32 @@
const sanitizePath = (path: string) => {
if (path.startsWith('./')) {
return decodeURIComponent(path.replace('./', ''))
}
return decodeURIComponent(path)
}
const removeNoteFilename = (pathNote: string) => {
const path = pathNote.split('/')
path.pop()
return sanitizePath(path.join('/'))
}
export const resolvePath = (
currentAbsolutePathNote: string,
pathToResolve: string
) => {
let currentAbsolutePath = removeNoteFilename(currentAbsolutePathNote)
pathToResolve = sanitizePath(pathToResolve)
while (pathToResolve.startsWith('../')) {
const adjustedAbsolutePath = currentAbsolutePath.split('/')
adjustedAbsolutePath.pop()
currentAbsolutePath = adjustedAbsolutePath.join('/')
pathToResolve = pathToResolve.replace('../', '')
}
return currentAbsolutePath
? `${currentAbsolutePath}/${pathToResolve}`
: pathToResolve
}