refactor(router): clean global api

This commit is contained in:
qingwei.li 2017-05-29 19:55:28 +08:00 committed by cinwell.li
commit 8741c746df
12 changed files with 46 additions and 126 deletions

View file

@ -138,9 +138,9 @@ function updateNoData (text, path) {
}
}
export function init (opts) {
export function init (opts, vm) {
dom = Docsify.dom
const keywords = Docsify.route.parse().query.s
const keywords = vm.router.parse().query.s
style()
tpl(opts, keywords)

View file

@ -24,7 +24,7 @@ const install = function (hook, vm) {
const isAuto = CONFIG.paths === 'auto'
hook.mounted(_ => {
initComponet(CONFIG)
initComponet(CONFIG, vm)
!isAuto && initSearch(CONFIG, vm)
})
hook.doneEach(_ => {
@ -33,4 +33,4 @@ const install = function (hook, vm) {
})
}
window.$docsify.plugins = [].concat(install, window.$docsify.plugins)
$docsify.plugins = [].concat(install, $docsify.plugins)

View file

@ -14,18 +14,18 @@ function escapeHtml (string) {
return String(string).replace(/[&<>"'\/]/g, s => entityMap[s])
}
function getAllPaths () {
function getAllPaths (router) {
const paths = []
helper.dom.findAll('a:not([data-nosearch])')
.map(node => {
const href = node.href
const originHref = node.getAttribute('href')
const path = helper.route.parse(href).path
const path = router.parse(href).path
if (path &&
paths.indexOf(path) === -1 &&
!helper.route.isAbsolutePath(originHref)) {
!Docsify.util.isAbsolutePath(originHref)) {
paths.push(path)
}
})
@ -38,10 +38,10 @@ function saveData (maxAge) {
localStorage.setItem('docsify.search.index', JSON.stringify(INDEXS))
}
export function genIndex (path, content = '') {
export function genIndex (path, content = '', router) {
const tokens = window.marked.lexer(content)
const slugify = window.Docsify.slugify
const toURL = Docsify.route.toURL
const toURL = router.toURL
const index = {}
let slug
@ -145,7 +145,7 @@ export function init (config, vm) {
return
}
const paths = isAuto ? getAllPaths() : config.paths
const paths = isAuto ? getAllPaths(vm.router) : config.paths
const len = paths.length
let count = 0
@ -155,7 +155,7 @@ export function init (config, vm) {
helper
.get(vm.router.getFile(path))
.then(result => {
INDEXS[path] = genIndex(path, result)
INDEXS[path] = genIndex(path, result, vm.router)
len === ++count && saveData(config.maxAge)
})
})