Skip to content

Commit

Permalink
fix: resolve './' and '../' (docsifyjs#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoCLin committed Jul 31, 2018
1 parent 1079cbb commit bee0db8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/core/router/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
isAbsolutePath,
stringifyQuery,
cleanPath,
replaceSlug
replaceSlug,
resolvePath
} from '../util'
import {noop, merge} from '../../util/core'

Expand Down Expand Up @@ -76,11 +77,13 @@ export class History {
(idIndex > 0 ? currentRoute.substr(0, idIndex) : currentRoute) + path
}

if (currentRoute !== undefined) {
currentRoute = currentRoute.substr(0, currentRoute.lastIndexOf('/') + 1)
console.log(currentRoute, path)
if (path.startsWith('/')) {
return cleanPath(path)
}

return cleanPath(path.startsWith('/') ? path : currentRoute + path)
if (currentRoute !== undefined) {
const currentDir = currentRoute.substr(0, currentRoute.lastIndexOf('/') + 1)
return cleanPath(resolvePath(currentDir + path))
}
}
}
16 changes: 16 additions & 0 deletions src/core/router/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,19 @@ export function getPath(...args) {
export const replaceSlug = cached(path => {
return path.replace('#', '?id=')
})

export const resolvePath = cached(path => {
const segments = path.replace(/^\//, '').split('/')
let resolved = []

for (let i = 0, len = segments.length; i < len; i++) {
const segment = segments[i]

if (segment === '..') {
resolved.pop()
} else if (segment !== '.') {
resolved.push(segment)
}
}
return '/' + resolved.join('/')
})

0 comments on commit bee0db8

Please sign in to comment.