refactor(core): add router
This commit is contained in:
parent
8bee8f0213
commit
30da0d5d46
17 changed files with 316 additions and 88 deletions
|
|
@ -1,17 +1,42 @@
|
|||
import { ensureSlash } from './hash'
|
||||
import { normalize, parse } from './hash'
|
||||
import { getBasePath, cleanPath } from './util'
|
||||
import { on } from '../util/dom'
|
||||
|
||||
function getAlias (path, alias) {
|
||||
if (alias[path]) return getAlias(alias[path], alias)
|
||||
return path
|
||||
}
|
||||
|
||||
function getFileName (path) {
|
||||
return /\.(md|html)$/g.test(path)
|
||||
? path
|
||||
: /\/$/g.test(path)
|
||||
? `${path}README.md`
|
||||
: `${path}.md`
|
||||
}
|
||||
|
||||
export function routeMixin (Docsify) {
|
||||
Docsify.prototype.$route = {
|
||||
query: location.query || {},
|
||||
path: location.path || '/',
|
||||
base: ''
|
||||
Docsify.prototype.route = {}
|
||||
Docsify.prototype.$getFile = function (path) {
|
||||
const { config } = this
|
||||
const base = getBasePath(config.basePath)
|
||||
|
||||
path = getAlias(path, config.alias)
|
||||
path = getFileName(path)
|
||||
path = path === '/README.md' ? ('/' + config.homepage || path) : path
|
||||
path = cleanPath(base + path)
|
||||
|
||||
return path
|
||||
}
|
||||
}
|
||||
|
||||
export function initRoute (vm) {
|
||||
ensureSlash()
|
||||
window.addEventListener('hashchange', () => {
|
||||
ensureSlash()
|
||||
vm.$fetch()
|
||||
normalize()
|
||||
vm.route = parse()
|
||||
|
||||
on('hashchange', _ => {
|
||||
normalize()
|
||||
vm.route = parse()
|
||||
vm._fetch()
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue