Skip to content

Commit

Permalink
refactor(plugins): update search plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Feb 19, 2017
1 parent 86594a3 commit 079bd00
Show file tree
Hide file tree
Showing 22 changed files with 348 additions and 393 deletions.
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
"extends": ["vue"],
"env": {
"browser": true
},
"globals": {
"Docsify": true,
"$docsify": true
}
}
24 changes: 12 additions & 12 deletions build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ build({
plugins: [commonjs(), nodeResolve()]
})

// build({
// entry: 'plugins/search.js',
// output: 'plugins/search.js',
// moduleName: 'D.Search'
// })
build({
entry: 'plugins/search/index.js',
output: 'plugins/search.js',
moduleName: 'D.Search'
})

// build({
// entry: 'plugins/ga.js',
// output: 'plugins/ga.js',
// moduleName: 'D.GA'
// })
build({
entry: 'plugins/ga.js',
output: 'plugins/ga.js',
moduleName: 'D.GA'
})

if (isProd) {
build({
entry: 'index.js',
entry: 'core/index.js',
output: 'docsify.min.js',
plugins: [commonjs(), nodeResolve(), uglify()]
})
build({
entry: 'plugins/search.js',
entry: 'plugins/search/index.js',
output: 'plugins/search.min.js',
moduleName: 'D.Search',
plugins: [uglify()]
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css">
</head>
<body>
<nav>
<nav data-cloak>
<a href="#/">En</a>
<a href="#/zh-cn/">中文</a>
</nav>
Expand Down
2 changes: 1 addition & 1 deletion docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ window.$docsify = {
})

hook.ready(function() {
// Called after initialization is complete. Only trigger once, no arguments.
// Called after initial completion, no arguments.
})
}
]
Expand Down
2 changes: 1 addition & 1 deletion docs/zh-cn/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ window.$docsify = {
})

hook.ready(function() {
// 初始化完成后调用,只调用一次,没有参数。
// 初始化并第一次加完成数据后调用,没有参数。
})
}
]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "rm -rf lib themes && node build/build.js && mkdir lib/themes && mkdir themes && node build/build-css.js",
"dev:build": "rm -rf lib themes && mkdir themes && node build/build.js --dev && node build/build-css.js --dev",
"dev": "node app.js & nodemon -w src -e js,css --exec 'npm run dev:build'",
"test": "eslint src test"
"test": "eslint src"
},
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ if (script) {
if (config.name === true) config.name = ''
}

window.$docsify = config

export default config
4 changes: 2 additions & 2 deletions src/core/event/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export function scrollActiveSidebar () {
const li = nav[last.getAttribute('data-id')]

if (!li || li === active) return
if (active) active.classList.remove('active')

active && active.classList.remove('active')
li.classList.add('active')
active = li

Expand Down Expand Up @@ -79,7 +79,7 @@ export function scrollActiveSidebar () {

export function scrollIntoView (id) {
const section = dom.find('#' + id)
section && setTimeout(() => section.scrollIntoView(), 0)
section && section.scrollIntoView()
}

const scrollEl = dom.$.scrollingElement || dom.$.documentElement
Expand Down
5 changes: 3 additions & 2 deletions src/core/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ export function fetchMixin (proto) {
.then(text => this._renderCover(text))
}

proto.$fetch = function () {
proto.$fetch = function (cb = noop) {
this._fetchCover()
this._fetch(result => {
this.$resetEvents()
callHook(this, 'doneEach')
cb()
})
}
}

export function initFetch (vm) {
vm.$fetch()
vm.$fetch(_ => callHook(vm, 'ready'))
}
2 changes: 1 addition & 1 deletion src/core/global-api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as util from './util'
import * as dom from './util/dom'
import * as render from './render/compiler'
import * as route from './route/util'
import * as route from './route/hash'
import { get } from './fetch/ajax'
import marked from 'marked'
import prism from 'prismjs'
Expand Down
3 changes: 2 additions & 1 deletion src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ initGlobalAPI()
/**
* Run Docsify
*/
new Docsify()

setTimeout(_ => new Docsify(), 0)
1 change: 0 additions & 1 deletion src/core/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export function initMixin (proto) {
initEvent(vm) // Bind events
initRoute(vm) // Add hashchange eventListener
initFetch(vm) // Fetch data
callHook(vm, 'ready')
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/render/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { genTree } from './gen-tree'
import { slugify, clearSlugCache } from './slugify'
import { emojify } from './emojify'
import { toURL, parse } from '../route/hash'
import { getBasePath, isResolvePath, getPath } from '../route/util'
import { getBasePath, isAbsolutePath, getPath } from '../route/util'
import { isFn, merge, cached } from '../util/core'

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

if (!isResolvePath(href)) {
if (!isAbsolutePath(href)) {
url = getPath(contentBase, href)
}

Expand Down
5 changes: 3 additions & 2 deletions src/core/render/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import cssVars from '../util/polyfill/css-vars'
import * as tpl from './tpl'
import { markdown, sidebar, subSidebar, cover } from './compiler'
import { callHook } from '../init/lifecycle'
import { getBasePath, getPath, isResolvePath } from '../route/util'
import { getBasePath, getPath, isAbsolutePath } from '../route/util'

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

dom.toggleClass(el, 'add', 'has-mask')
if (isResolvePath(m[1])) {
if (isAbsolutePath(m[1])) {
path = getPath(getBasePath(this.config.basePath), m[1])
}
el.style.backgroundImage = `url(${path})`
Expand Down Expand Up @@ -157,4 +157,5 @@ export function initRender (vm) {
// Polyfll
cssVars(config.themeColor)
}
dom.toggleClass(dom.body, 'ready')
}
1 change: 1 addition & 0 deletions src/core/route/hash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { merge, cached } from '../util/core'
import { parseQuery, stringifyQuery, cleanPath } from './util'
export * from './util'

function replaceHash (path) {
const i = window.location.href.indexOf('#')
Expand Down
7 changes: 3 additions & 4 deletions src/core/route/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { normalize, parse } from './hash'
import { getBasePath, getPath } from './util'
import { getBasePath, getPath, isAbsolutePath } from './util'
import { on } from '../util/dom'

function getAlias (path, alias) {
if (alias[path]) return getAlias(alias[path], alias)
return path
return alias[path] ? getAlias(alias[path], alias) : path
}

function getFileName (path) {
Expand All @@ -24,7 +23,7 @@ export function routeMixin (proto) {
path = getAlias(path, config.alias)
path = getFileName(path)
path = path === '/README.md' ? (config.homepage || path) : path
path = getPath(base, path)
path = isAbsolutePath(path) ? path : getPath(base, path)

return path
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/route/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function stringifyQuery (obj) {
const qs = []

for (const key in obj) {
qs.push(`${encode(key)}=${encode(obj[key])}`)
qs.push(`${encode(key)}=${encode(obj[key])}`.toLowerCase())
}

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

export const isResolvePath = cached(path => {
return /:|(\/{2})/.test(path)
export const isAbsolutePath = cached(path => {
return /(:|(\/{2}))/.test(path)
})

export const getRoot = cached(path => {
Expand Down
13 changes: 3 additions & 10 deletions src/plugins/ga.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// From https://github.com/egoist/vue-ga/blob/master/src/index.js

function appendScript () {
const script = document.createElement('script')
script.async = true
Expand All @@ -24,19 +23,13 @@ function collect () {
window.ga('send', 'pageview')
}

const install = function () {
if (install.installed) return
install.installed = true

const install = function (hook) {
if (!window.$docsify.ga) {
console.error('[Docsify] ga is required.')
return
}

window.$docsify.plugins = [].concat(function (hook) {
hook.init(collect)
hook.beforeEach(collect)
}, window.$docsify.plugins)
hook.beforeEach(collect)
}

export default install()
window.$docsify.plugins = [].concat(install, window.$docsify.plugins)
Loading

0 comments on commit 079bd00

Please sign in to comment.