refactor(core): fix route path

This commit is contained in:
qingwei.li 2017-02-18 19:35:14 +08:00 committed by cinwell.li
commit e2b7b976cf
16 changed files with 125 additions and 444 deletions

View file

@ -1,4 +1,4 @@
import { merge } from '../util/core'
import { merge, cached } from '../util/core'
import { parseQuery, stringifyQuery } from './util'
function replaceHash (path) {
@ -8,6 +8,11 @@ function replaceHash (path) {
)
}
const replaceSlug = cached(path => {
return path
.replace('#', '?id=')
.replace(/\?(\w+)=/g, (_, slug) => slug === 'id' ? '?id=' : `&${slug}=`)
})
/**
* Normalize the current url
*
@ -18,9 +23,7 @@ function replaceHash (path) {
export function normalize () {
let path = getHash()
path = path
.replace('#', '?id=')
.replace(/\?(\w+)=/g, (_, slug) => slug === 'id' ? '?id=' : `&${slug}=`)
path = replaceSlug(path)
if (path.charAt(0) === '/') return replaceHash(path)
replaceHash('/' + path)
@ -62,7 +65,7 @@ export function parse (path = window.location.href) {
* @param {object} qs query params
*/
export function toURL (path, params) {
const route = parse(path)
const route = parse(replaceSlug(path))
route.query = merge({}, route.query, params)
path = route.path + stringifyQuery(route.query)

View file

@ -1,5 +1,5 @@
import { normalize, parse } from './hash'
import { getBasePath, cleanPath } from './util'
import { getBasePath, getPath } from './util'
import { on } from '../util/dom'
function getAlias (path, alias) {
@ -15,16 +15,16 @@ function getFileName (path) {
: `${path}.md`
}
export function routeMixin (Docsify) {
Docsify.prototype.route = {}
Docsify.prototype.$getFile = function (path) {
export function routeMixin (proto) {
proto.route = {}
proto.$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)
path = getPath(base, path)
return path
}
@ -39,14 +39,14 @@ export function initRoute (vm) {
on('hashchange', _ => {
normalize()
vm.route = parse()
vm._updateRender()
if (lastRoute.path === vm.route.path) {
// TODO: goto xxx
vm.$resetEvents()
return
}
vm._fetchCover()
vm._fetch()
vm.$fetch()
lastRoute = vm.route
})
}

View file

@ -3,7 +3,7 @@ import { cached } from '../util/core'
const decode = decodeURIComponent
const encode = encodeURIComponent
export const parseQuery = cached(query => {
export function parseQuery (query) {
const res = {}
query = query.trim().replace(/^(\?|#|&)/, '')
@ -19,7 +19,7 @@ export const parseQuery = cached(query => {
res[parts[0]] = decode(parts[1])
})
return res
})
}
export function stringifyQuery (obj) {
const qs = []
@ -37,6 +37,14 @@ export const getBasePath = cached(base => {
: cleanPath(window.location.pathname + '/' + base)
})
export function getPath (...args) {
const path = args.find(path => /:|(\/{2})/.test(path))
if (path) return path
return cleanPath(args.join('/'))
}
export const getRoot = cached(path => {
return /\/$/g.test(path) ? path : path.match(/(\S*\/)[^\/]+$/)[1]
})