Skip to content

Commit 079bd00

Browse files
committedFeb 19, 2017
refactor(plugins): update search plugin
1 parent 86594a3 commit 079bd00

22 files changed

+348
-393
lines changed
 

‎.eslintrc

+4
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22
"extends": ["vue"],
33
"env": {
44
"browser": true
5+
},
6+
"globals": {
7+
"Docsify": true,
8+
"$docsify": true
59
}
610
}

‎build/build.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,26 @@ build({
3232
plugins: [commonjs(), nodeResolve()]
3333
})
3434

35-
// build({
36-
// entry: 'plugins/search.js',
37-
// output: 'plugins/search.js',
38-
// moduleName: 'D.Search'
39-
// })
35+
build({
36+
entry: 'plugins/search/index.js',
37+
output: 'plugins/search.js',
38+
moduleName: 'D.Search'
39+
})
4040

41-
// build({
42-
// entry: 'plugins/ga.js',
43-
// output: 'plugins/ga.js',
44-
// moduleName: 'D.GA'
45-
// })
41+
build({
42+
entry: 'plugins/ga.js',
43+
output: 'plugins/ga.js',
44+
moduleName: 'D.GA'
45+
})
4646

4747
if (isProd) {
4848
build({
49-
entry: 'index.js',
49+
entry: 'core/index.js',
5050
output: 'docsify.min.js',
5151
plugins: [commonjs(), nodeResolve(), uglify()]
5252
})
5353
build({
54-
entry: 'plugins/search.js',
54+
entry: 'plugins/search/index.js',
5555
output: 'plugins/search.min.js',
5656
moduleName: 'D.Search',
5757
plugins: [uglify()]

‎docs/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css">
1111
</head>
1212
<body>
13-
<nav>
13+
<nav data-cloak>
1414
<a href="#/">En</a>
1515
<a href="#/zh-cn/">中文</a>
1616
</nav>

‎docs/plugins.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ window.$docsify = {
9090
})
9191

9292
hook.ready(function() {
93-
// Called after initialization is complete. Only trigger once, no arguments.
93+
// Called after initial completion, no arguments.
9494
})
9595
}
9696
]

‎docs/zh-cn/plugins.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ window.$docsify = {
8585
})
8686

8787
hook.ready(function() {
88-
// 初始化完成后调用,只调用一次,没有参数。
88+
// 初始化并第一次加完成数据后调用,没有参数。
8989
})
9090
}
9191
]

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"build": "rm -rf lib themes && node build/build.js && mkdir lib/themes && mkdir themes && node build/build-css.js",
1313
"dev:build": "rm -rf lib themes && mkdir themes && node build/build.js --dev && node build/build-css.js --dev",
1414
"dev": "node app.js & nodemon -w src -e js,css --exec 'npm run dev:build'",
15-
"test": "eslint src test"
15+
"test": "eslint src"
1616
},
1717
"repository": {
1818
"type": "git",

‎src/core/config.js

+2
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ if (script) {
3939
if (config.name === true) config.name = ''
4040
}
4141

42+
window.$docsify = config
43+
4244
export default config

‎src/core/event/scroll.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export function scrollActiveSidebar () {
4545
const li = nav[last.getAttribute('data-id')]
4646

4747
if (!li || li === active) return
48-
if (active) active.classList.remove('active')
4948

49+
active && active.classList.remove('active')
5050
li.classList.add('active')
5151
active = li
5252

@@ -79,7 +79,7 @@ export function scrollActiveSidebar () {
7979

8080
export function scrollIntoView (id) {
8181
const section = dom.find('#' + id)
82-
section && setTimeout(() => section.scrollIntoView(), 0)
82+
section && section.scrollIntoView()
8383
}
8484

8585
const scrollEl = dom.$.scrollingElement || dom.$.documentElement

‎src/core/fetch/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,16 @@ export function fetchMixin (proto) {
5757
.then(text => this._renderCover(text))
5858
}
5959

60-
proto.$fetch = function () {
60+
proto.$fetch = function (cb = noop) {
6161
this._fetchCover()
6262
this._fetch(result => {
6363
this.$resetEvents()
6464
callHook(this, 'doneEach')
65+
cb()
6566
})
6667
}
6768
}
6869

6970
export function initFetch (vm) {
70-
vm.$fetch()
71+
vm.$fetch(_ => callHook(vm, 'ready'))
7172
}

‎src/core/global-api.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as util from './util'
22
import * as dom from './util/dom'
33
import * as render from './render/compiler'
4-
import * as route from './route/util'
4+
import * as route from './route/hash'
55
import { get } from './fetch/ajax'
66
import marked from 'marked'
77
import prism from 'prismjs'

‎src/core/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ initGlobalAPI()
2525
/**
2626
* Run Docsify
2727
*/
28-
new Docsify()
28+
29+
setTimeout(_ => new Docsify(), 0)

‎src/core/init/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export function initMixin (proto) {
1818
initEvent(vm) // Bind events
1919
initRoute(vm) // Add hashchange eventListener
2020
initFetch(vm) // Fetch data
21-
callHook(vm, 'ready')
2221
}
2322
}
2423

‎src/core/render/compiler.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { genTree } from './gen-tree'
55
import { slugify, clearSlugCache } from './slugify'
66
import { emojify } from './emojify'
77
import { toURL, parse } from '../route/hash'
8-
import { getBasePath, isResolvePath, getPath } from '../route/util'
8+
import { getBasePath, isAbsolutePath, getPath } from '../route/util'
99
import { isFn, merge, cached } from '../util/core'
1010

1111
let markdownCompiler = marked
@@ -87,7 +87,7 @@ renderer.image = function (href, title, text) {
8787
let url = href
8888
const titleHTML = title ? ` title="${title}"` : ''
8989

90-
if (!isResolvePath(href)) {
90+
if (!isAbsolutePath(href)) {
9191
url = getPath(contentBase, href)
9292
}
9393

‎src/core/render/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import cssVars from '../util/polyfill/css-vars'
55
import * as tpl from './tpl'
66
import { markdown, sidebar, subSidebar, cover } from './compiler'
77
import { callHook } from '../init/lifecycle'
8-
import { getBasePath, getPath, isResolvePath } from '../route/util'
8+
import { getBasePath, getPath, isAbsolutePath } from '../route/util'
99

1010
function executeScript () {
1111
const script = dom.findAll('.markdown-section>script')
@@ -101,7 +101,7 @@ export function renderMixin (proto) {
101101
let path = m[1]
102102

103103
dom.toggleClass(el, 'add', 'has-mask')
104-
if (isResolvePath(m[1])) {
104+
if (isAbsolutePath(m[1])) {
105105
path = getPath(getBasePath(this.config.basePath), m[1])
106106
}
107107
el.style.backgroundImage = `url(${path})`
@@ -157,4 +157,5 @@ export function initRender (vm) {
157157
// Polyfll
158158
cssVars(config.themeColor)
159159
}
160+
dom.toggleClass(dom.body, 'ready')
160161
}

‎src/core/route/hash.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { merge, cached } from '../util/core'
22
import { parseQuery, stringifyQuery, cleanPath } from './util'
3+
export * from './util'
34

45
function replaceHash (path) {
56
const i = window.location.href.indexOf('#')

‎src/core/route/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { normalize, parse } from './hash'
2-
import { getBasePath, getPath } from './util'
2+
import { getBasePath, getPath, isAbsolutePath } from './util'
33
import { on } from '../util/dom'
44

55
function getAlias (path, alias) {
6-
if (alias[path]) return getAlias(alias[path], alias)
7-
return path
6+
return alias[path] ? getAlias(alias[path], alias) : path
87
}
98

109
function getFileName (path) {
@@ -24,7 +23,7 @@ export function routeMixin (proto) {
2423
path = getAlias(path, config.alias)
2524
path = getFileName(path)
2625
path = path === '/README.md' ? (config.homepage || path) : path
27-
path = getPath(base, path)
26+
path = isAbsolutePath(path) ? path : getPath(base, path)
2827

2928
return path
3029
}

‎src/core/route/util.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function stringifyQuery (obj) {
2525
const qs = []
2626

2727
for (const key in obj) {
28-
qs.push(`${encode(key)}=${encode(obj[key])}`)
28+
qs.push(`${encode(key)}=${encode(obj[key])}`.toLowerCase())
2929
}
3030

3131
return qs.length ? `?${qs.join('&')}` : ''
@@ -41,8 +41,8 @@ export function getPath (...args) {
4141
return cleanPath(args.join('/'))
4242
}
4343

44-
export const isResolvePath = cached(path => {
45-
return /:|(\/{2})/.test(path)
44+
export const isAbsolutePath = cached(path => {
45+
return /(:|(\/{2}))/.test(path)
4646
})
4747

4848
export const getRoot = cached(path => {

‎src/plugins/ga.js

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// From https://github.com/egoist/vue-ga/blob/master/src/index.js
2-
32
function appendScript () {
43
const script = document.createElement('script')
54
script.async = true
@@ -24,19 +23,13 @@ function collect () {
2423
window.ga('send', 'pageview')
2524
}
2625

27-
const install = function () {
28-
if (install.installed) return
29-
install.installed = true
30-
26+
const install = function (hook) {
3127
if (!window.$docsify.ga) {
3228
console.error('[Docsify] ga is required.')
3329
return
3430
}
3531

36-
window.$docsify.plugins = [].concat(function (hook) {
37-
hook.init(collect)
38-
hook.beforeEach(collect)
39-
}, window.$docsify.plugins)
32+
hook.beforeEach(collect)
4033
}
4134

42-
export default install()
35+
window.$docsify.plugins = [].concat(install, window.$docsify.plugins)

0 commit comments

Comments
 (0)