Skip to content

Commit 8741c74

Browse files
committed
refactor(router): clean global api
1 parent 2c7041c commit 8741c74

File tree

12 files changed

+44
-124
lines changed

12 files changed

+44
-124
lines changed

Diff for: src/core/event/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { scrollIntoView } from './scroll'
66
export function eventMixin (proto) {
77
proto.$resetEvents = function () {
88
scrollIntoView(this.route.query.id)
9-
sidebar.getAndActive('nav')
9+
sidebar.getAndActive(this.router, 'nav')
1010
}
1111
}
1212

1313
export function initEvent (vm) {
1414
// Bind toggle button
15-
sidebar.btn('button.sidebar-toggle')
15+
sidebar.btn('button.sidebar-toggle', vm.router)
1616
// Bind sticky effect
1717
if (vm.config.coverpage) {
1818
!isMobile && on('scroll', sidebar.sticky)

Diff for: src/core/event/scroll.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { isMobile } from '../util/env'
22
import * as dom from '../util/dom'
3-
import { parse } from '../router/hash'
43

54
const nav = {}
65
let hoverOver = false
@@ -53,7 +52,7 @@ function highlight () {
5352
}
5453
}
5554

56-
export function scrollActiveSidebar () {
55+
export function scrollActiveSidebar (router) {
5756
if (isMobile) return
5857

5958
const sidebar = dom.getNode('.sidebar')
@@ -66,7 +65,7 @@ export function scrollActiveSidebar () {
6665
let href = a.getAttribute('href')
6766

6867
if (href !== '/') {
69-
href = parse(href).query.id
68+
href = router.parse(href).query.id
7069
}
7170

7271
nav[decodeURIComponent(href)] = li

Diff for: src/core/event/sidebar.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { isMobile } from '../util/env'
22
import * as dom from '../util/dom'
3-
import { getHash } from '../router/hash'
43

54
const title = dom.$.title
65
/**
76
* Toggle button
87
*/
9-
export function btn (el) {
8+
export function btn (el, router) {
109
const toggle = _ => dom.body.classList.toggle('close')
1110

1211
el = dom.getNode(el)
@@ -21,7 +20,7 @@ export function btn (el) {
2120
dom.body.classList.contains('close') && toggle()
2221
)
2322
dom.on(sidebar, 'click', _ =>
24-
setTimeout((_ => getAndActive(sidebar, true, true), 0))
23+
setTimeout((_ => getAndActive(router, sidebar, true, true), 0))
2524
)
2625
}
2726

@@ -39,16 +38,17 @@ export function sticky () {
3938

4039
/**
4140
* Get and active link
41+
* @param {object} router
4242
* @param {string|element} el
4343
* @param {Boolean} isParent acitve parent
4444
* @param {Boolean} autoTitle auto set title
4545
* @return {element}
4646
*/
47-
export function getAndActive (el, isParent, autoTitle) {
47+
export function getAndActive (router, el, isParent, autoTitle) {
4848
el = dom.getNode(el)
4949

5050
const links = dom.findAll(el, 'a')
51-
const hash = '#' + getHash()
51+
const hash = router.toURL(router.getCurrentPath())
5252
let target
5353

5454
links

Diff for: src/core/global-api.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as util from './util'
22
import * as dom from './util/dom'
3-
import * as render from './render/compiler'
4-
import * as route from './router/hash'
3+
import { Compiler } from './render/compiler'
54
import { slugify } from './render/slugify'
65
import { get } from './fetch/ajax'
76
import marked from 'marked'
87
import prism from 'prismjs'
98

109
export default function () {
11-
window.Docsify = { util, dom, render, route, get, slugify }
10+
window.Docsify = { util, dom, get, slugify }
11+
window.DocsifyCompiler = Compiler
1212
window.marked = marked
1313
window.Prism = prism
1414
}

Diff for: src/core/render/compiler.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ export class Compiler {
1717
this.contentBase = getBasePath(config.base)
1818

1919
const renderer = this._initRenderer()
20-
let runner
20+
let compile
2121
const mdConf = config.markdown || {}
2222

2323
if (isFn(mdConf)) {
24-
runner = mdConf(marked, renderer)
24+
compile = mdConf(marked, renderer)
2525
} else {
2626
marked.setOptions(merge(mdConf, {
2727
renderer: merge(renderer, mdConf.renderer)
2828
}))
29-
runner = marked
29+
compile = marked
3030
}
3131

32-
this.runner = cached(text => {
32+
this.compile = cached(text => {
3333
let html = ''
3434

3535
if (!text) return text
3636

37-
html = runner(text)
37+
html = compile(text)
3838
html = emojify(html)
3939
slugify.clear()
4040

@@ -119,7 +119,7 @@ export class Compiler {
119119
let html = ''
120120

121121
if (text) {
122-
html = this.runner(text)
122+
html = this.compile(text)
123123
html = html.match(/<ul[^>]*>([\s\S]+)<\/ul>/g)[0]
124124
} else {
125125
const tree = this.cacheTree[currentPath] || genTree(this.toc, level)
@@ -151,15 +151,15 @@ export class Compiler {
151151
}
152152

153153
article (text) {
154-
return this.runner(text)
154+
return this.compile(text)
155155
}
156156

157157
/**
158158
* Compile cover page
159159
*/
160160
cover (text) {
161161
const cacheToc = this.toc.slice()
162-
const html = this.runner(text)
162+
const html = this.compile(text)
163163

164164
this.toc = cacheToc.slice()
165165

Diff for: src/core/render/index.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as dom from '../util/dom'
2-
import { getAndActive, sticky } from '../event/sidebar'
3-
import { scrollActiveSidebar, scroll2Top } from '../event/scroll'
4-
import cssVars from '../util/polyfill/css-vars'
52
import * as tpl from './tpl'
6-
import { Compiler } from './compiler'
3+
import cssVars from '../util/polyfill/css-vars'
4+
import tinydate from 'tinydate'
75
import { callHook } from '../init/lifecycle'
6+
import { Compiler } from './compiler'
7+
import { getAndActive, sticky } from '../event/sidebar'
88
import { getBasePath, getPath, isAbsolutePath } from '../router/util'
9-
import { isPrimitive } from '../util/core'
109
import { isMobile } from '../util/env'
11-
import tinydate from 'tinydate'
10+
import { isPrimitive } from '../util/core'
11+
import { scrollActiveSidebar, scroll2Top } from '../event/scroll'
1212

1313
function executeScript () {
1414
const script = dom.findAll('.markdown-section>script')
@@ -86,13 +86,13 @@ export function renderMixin (proto) {
8686
const { maxLevel, subMaxLevel, autoHeader, loadSidebar } = this.config
8787

8888
this._renderTo('.sidebar-nav', this.compiler.sidebar(text, maxLevel))
89-
const activeEl = getAndActive('.sidebar-nav', true, true)
89+
const activeEl = getAndActive(this.router, '.sidebar-nav', true, true)
9090
if (loadSidebar && activeEl) {
9191
activeEl.parentNode.innerHTML += this.compiler.subSidebar(subMaxLevel)
9292
}
9393
// bind event
9494
this.activeLink = activeEl
95-
scrollActiveSidebar()
95+
scrollActiveSidebar(this.router)
9696

9797
if (autoHeader && activeEl) {
9898
const main = dom.getNode('#main')
@@ -106,13 +106,13 @@ export function renderMixin (proto) {
106106
}
107107

108108
proto._renderNav = function (text) {
109-
text && this._renderTo('nav', this.compiler.runner(text))
110-
getAndActive('nav')
109+
text && this._renderTo('nav', this.compiler.compile(text))
110+
getAndActive(this.router, 'nav')
111111
}
112112

113113
proto._renderMain = function (text, opt = {}) {
114114
callHook(this, 'beforeEach', text, result => {
115-
let html = this.isHTML ? result : this.compiler.runner(result)
115+
let html = this.isHTML ? result : this.compiler.compile(result)
116116
if (opt.updatedAt) {
117117
html = formatUpdated(html, opt.updatedAt, this.config.formatUpdated)
118118
}

Diff for: src/core/router/hash.js

-80
This file was deleted.

Diff for: src/core/router/history/hash.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { History } from './base'
22
import { merge, cached, noop } from '../../util/core'
3-
import { parseQuery, stringifyQuery, cleanPath } from '../util'
43
import { on } from '../../util/dom'
4+
import { parseQuery, stringifyQuery, cleanPath } from '../util'
55

66
function replaceHash (path) {
77
const i = location.href.indexOf('#')

Diff for: src/core/util/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './core'
22
export * from './env'
3+
export * from '../router/util'

Diff for: src/plugins/search/component.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ function updateNoData (text, path) {
138138
}
139139
}
140140

141-
export function init (opts) {
141+
export function init (opts, vm) {
142142
dom = Docsify.dom
143-
const keywords = Docsify.route.parse().query.s
143+
const keywords = vm.router.parse().query.s
144144

145145
style()
146146
tpl(opts, keywords)

Diff for: src/plugins/search/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const install = function (hook, vm) {
2424
const isAuto = CONFIG.paths === 'auto'
2525

2626
hook.mounted(_ => {
27-
initComponet(CONFIG)
27+
initComponet(CONFIG, vm)
2828
!isAuto && initSearch(CONFIG, vm)
2929
})
3030
hook.doneEach(_ => {
@@ -33,4 +33,4 @@ const install = function (hook, vm) {
3333
})
3434
}
3535

36-
window.$docsify.plugins = [].concat(install, window.$docsify.plugins)
36+
$docsify.plugins = [].concat(install, $docsify.plugins)

Diff for: src/plugins/search/search.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ function escapeHtml (string) {
1414
return String(string).replace(/[&<>"'\/]/g, s => entityMap[s])
1515
}
1616

17-
function getAllPaths () {
17+
function getAllPaths (router) {
1818
const paths = []
1919

2020
helper.dom.findAll('a:not([data-nosearch])')
2121
.map(node => {
2222
const href = node.href
2323
const originHref = node.getAttribute('href')
24-
const path = helper.route.parse(href).path
24+
const path = router.parse(href).path
2525

2626
if (path &&
2727
paths.indexOf(path) === -1 &&
28-
!helper.route.isAbsolutePath(originHref)) {
28+
!Docsify.util.isAbsolutePath(originHref)) {
2929
paths.push(path)
3030
}
3131
})
@@ -38,10 +38,10 @@ function saveData (maxAge) {
3838
localStorage.setItem('docsify.search.index', JSON.stringify(INDEXS))
3939
}
4040

41-
export function genIndex (path, content = '') {
41+
export function genIndex (path, content = '', router) {
4242
const tokens = window.marked.lexer(content)
4343
const slugify = window.Docsify.slugify
44-
const toURL = Docsify.route.toURL
44+
const toURL = router.toURL
4545
const index = {}
4646
let slug
4747

@@ -145,7 +145,7 @@ export function init (config, vm) {
145145
return
146146
}
147147

148-
const paths = isAuto ? getAllPaths() : config.paths
148+
const paths = isAuto ? getAllPaths(vm.router) : config.paths
149149
const len = paths.length
150150
let count = 0
151151

@@ -155,7 +155,7 @@ export function init (config, vm) {
155155
helper
156156
.get(vm.router.getFile(path))
157157
.then(result => {
158-
INDEXS[path] = genIndex(path, result)
158+
INDEXS[path] = genIndex(path, result, vm.router)
159159
len === ++count && saveData(config.maxAge)
160160
})
161161
})

0 commit comments

Comments
 (0)