refactor(core): add router

This commit is contained in:
qingwei.li 2017-02-18 11:41:33 +08:00 • committed by cinwell.li
commit 30da0d5d46
17 changed files with 316 additions and 88 deletions

View file

@ -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()
})
}