feat: allows relative path, fixed #590
This commit is contained in:
parent
d80aa2189b
commit
31654f12ec
4 changed files with 62 additions and 2 deletions
|
|
@ -25,7 +25,8 @@ export default function () {
|
|||
formatUpdated: '',
|
||||
externalLinkTarget: '_blank',
|
||||
routerMode: 'hash',
|
||||
noCompileLinks: []
|
||||
noCompileLinks: [],
|
||||
relativePath: false
|
||||
},
|
||||
window.$docsify
|
||||
)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,8 @@ import {
|
|||
isAbsolutePath,
|
||||
stringifyQuery,
|
||||
cleanPath,
|
||||
replaceSlug
|
||||
replaceSlug,
|
||||
resolvePath
|
||||
} from '../util'
|
||||
import {noop, merge} from '../../util/core'
|
||||
|
||||
|
|
@ -76,6 +77,10 @@ export class History {
|
|||
(idIndex > 0 ? currentRoute.substr(0, idIndex) : currentRoute) + path
|
||||
}
|
||||
|
||||
if (this.config.relativePath && !path.startsWith('/')) {
|
||||
const currentDir = currentRoute.substr(0, currentRoute.lastIndexOf('/') + 1)
|
||||
return cleanPath(resolvePath(currentDir + path))
|
||||
}
|
||||
return cleanPath('/' + path)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,6 +53,20 @@ export const cleanPath = cached(path => {
|
|||
return path.replace(/^\/+/, '/').replace(/([^:])\/{2,}/g, '$1/')
|
||||
})
|
||||
|
||||
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('/')
|
||||
})
|
||||
|
||||
export function getPath(...args) {
|
||||
return cleanPath(args.join('/'))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue