✨ (path) create a resolve path service to manage …
resolve path like ../..
This commit is contained in:
32
src/modules/repo/services/resolvePath.ts
Normal file
32
src/modules/repo/services/resolvePath.ts
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user