refactor: build config (#408)

* refactor: build config

* chore: fix conflict

* fix: 404 page path
This commit is contained in:
cinwell.li 2018-03-03 22:51:27 +08:00 committed by GitHub
commit 8352a1e489
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
64 changed files with 4246 additions and 5512 deletions

View file

@ -3,7 +3,7 @@ if (fixedPath !== location.href) {
location.href = fixedPath
}
function install (hook, vm) {
function install(hook, vm) {
const dom = Docsify.dom
const disqus = vm.config.disqus
if (!disqus) {
@ -15,7 +15,7 @@ function install (hook, vm) {
script.async = true
script.src = `https://${disqus}.disqus.com/embed.js`
script.setAttribute('data-timestamp', +new Date())
script.setAttribute('data-timestamp', Number(new Date()))
dom.appendTo(dom.body, script)
})

View file

@ -887,12 +887,12 @@ const AllGithubEmoji = [
'zzz'
]
// emoji from All-Github-Emoji-Icons
// Emoji from All-Github-Emoji-Icons
// https://github.com/scotch-io/All-Github-Emoji-Icons
window.emojify = function (match, $1) {
return AllGithubEmoji.indexOf($1) === -1
? match
: '<img class="emoji" src="https://assets-cdn.github.com/images/icons/emoji/' +
return AllGithubEmoji.indexOf($1) === -1 ?
match :
'<img class="emoji" src="https://assets-cdn.github.com/images/icons/emoji/' +
$1 +
'.png" alt="' +
$1 +

View file

@ -1,4 +1,4 @@
function handleExternalScript () {
function handleExternalScript() {
const container = Docsify.dom.getNode('#main')
const scripts = Docsify.dom.findAll(container, 'script')

View file

@ -2,7 +2,7 @@ import parser from './parser'
const install = function (hook, vm) {
hook.beforeEach(content => {
const { attributes, body } = parser(content)
const {attributes, body} = parser(content)
Docsify.util.merge(vm.config, attributes.config)

View file

@ -1,23 +1,23 @@
// From https://github.com/egoist/vue-ga/blob/master/src/index.js
function appendScript () {
function appendScript() {
const script = document.createElement('script')
script.async = true
script.src = 'https://www.google-analytics.com/analytics.js'
document.body.appendChild(script)
}
function init (id) {
function init(id) {
appendScript()
window.ga =
window.ga ||
function () {
;(window.ga.q = window.ga.q || []).push(arguments)
(window.ga.q = window.ga.q || []).push(arguments)
}
window.ga.l = Number(new Date())
window.ga('create', id, 'auto')
}
function collect () {
function collect() {
if (!window.ga) {
init($docsify.ga)
}

View file

@ -1,4 +1,4 @@
function install (hook, vm) {
function install(hook) {
const dom = Docsify.dom
hook.mounted(_ => {

View file

@ -1,8 +1,8 @@
import { search } from './search'
import {search} from './search'
let NO_DATA_TEXT = ''
function style () {
function style() {
const code = `
.sidebar {
padding-top: 0;
@ -91,7 +91,7 @@ function style () {
Docsify.dom.style(code)
}
function tpl (opts, defaultValue = '') {
function tpl(opts, defaultValue = '') {
const html =
`<div class="input-wrap">
<input type="search" value="${defaultValue}" />
@ -112,7 +112,7 @@ function tpl (opts, defaultValue = '') {
Docsify.dom.before(aside, el)
}
function doSearch (value) {
function doSearch(value) {
const $search = Docsify.dom.find('div.search')
const $panel = Docsify.dom.find($search, '.results-panel')
const $clearBtn = Docsify.dom.find($search, '.clear-button')
@ -140,7 +140,7 @@ function doSearch (value) {
$panel.innerHTML = html || `<p class="empty">${NO_DATA_TEXT}</p>`
}
function bindEvents () {
function bindEvents() {
const $search = Docsify.dom.find('div.search')
const $input = Docsify.dom.find($search, 'input')
const $inputWrap = Docsify.dom.find($search, '.input-wrap')
@ -157,7 +157,7 @@ function bindEvents () {
timeId = setTimeout(_ => doSearch(e.target.value.trim()), 100)
})
Docsify.dom.on($inputWrap, 'click', e => {
// click input outside
// Click input outside
if (e.target.tagName !== 'INPUT') {
$input.value = ''
doSearch()
@ -165,10 +165,12 @@ function bindEvents () {
})
}
function updatePlaceholder (text, path) {
function updatePlaceholder(text, path) {
const $input = Docsify.dom.getNode('.search input[type="search"]')
if (!$input) return
if (!$input) {
return
}
if (typeof text === 'string') {
$input.placeholder = text
} else {
@ -177,7 +179,7 @@ function updatePlaceholder (text, path) {
}
}
function updateNoData (text, path) {
function updateNoData(text, path) {
if (typeof text === 'string') {
NO_DATA_TEXT = text
} else {
@ -186,7 +188,7 @@ function updateNoData (text, path) {
}
}
export function init (opts, vm) {
export function init(opts, vm) {
const keywords = vm.router.parse().query.s
style()
@ -195,7 +197,7 @@ export function init (opts, vm) {
keywords && setTimeout(_ => doSearch(keywords), 500)
}
export function update (opts, vm) {
export function update(opts, vm) {
updatePlaceholder(opts.placeholder, vm.route.path)
updateNoData(opts.noData, vm.route.path)
}

View file

@ -1,5 +1,5 @@
import { init as initComponet, update as updateComponent } from './component'
import { init as initSearch } from './search'
import {init as initComponet, update as updateComponent} from './component'
import {init as initSearch} from './search'
const CONFIG = {
placeholder: 'Type to search',
@ -10,7 +10,7 @@ const CONFIG = {
}
const install = function (hook, vm) {
const { util } = Docsify
const {util} = Docsify
const opts = vm.config.search || CONFIG
if (Array.isArray(opts)) {

View file

@ -1,23 +1,23 @@
let INDEXS = {}
let helper
function escapeHtml (string) {
function escapeHtml(string) {
const entityMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
'\'': '&#39;',
'/': '&#x2F;'
}
return String(string).replace(/[&<>"'\/]/g, s => entityMap[s])
return String(string).replace(/[&<>"'/]/g, s => entityMap[s])
}
function getAllPaths (router) {
function getAllPaths(router) {
const paths = []
helper.dom.findAll('a:not([data-nosearch])').map(node => {
helper.dom.findAll('a:not([data-nosearch])').forEach(node => {
const href = node.href
const originHref = node.getAttribute('href')
const path = router.parse(href).path
@ -34,12 +34,12 @@ function getAllPaths (router) {
return paths
}
function saveData (maxAge) {
function saveData(maxAge) {
localStorage.setItem('docsify.search.expires', Date.now() + maxAge)
localStorage.setItem('docsify.search.index', JSON.stringify(INDEXS))
}
export function genIndex (path, content = '', router, depth) {
export function genIndex(path, content = '', router, depth) {
const tokens = window.marked.lexer(content)
const slugify = window.Docsify.slugify
const index = {}
@ -47,18 +47,18 @@ export function genIndex (path, content = '', router, depth) {
tokens.forEach(token => {
if (token.type === 'heading' && token.depth <= depth) {
slug = router.toURL(path, { id: slugify(token.text) })
index[slug] = { slug, title: token.text, body: '' }
slug = router.toURL(path, {id: slugify(token.text)})
index[slug] = {slug, title: token.text, body: ''}
} else {
if (!slug) return
if (!slug) {
return
}
if (!index[slug]) {
index[slug] = { slug, title: '', body: '' }
index[slug] = {slug, title: '', body: ''}
} else if (index[slug].body) {
index[slug].body += '\n' + (token.text || '')
} else {
if (index[slug].body) {
index[slug].body += '\n' + (token.text || '')
} else {
index[slug].body = token.text
}
index[slug].body = token.text
}
}
})
@ -70,7 +70,7 @@ export function genIndex (path, content = '', router, depth) {
* @param {String} query
* @returns {Array}
*/
export function search (query) {
export function search(query) {
const matchingResults = []
let data = []
Object.keys(INDEXS).forEach(key => {
@ -78,7 +78,7 @@ export function search (query) {
})
query = query.trim()
let keywords = query.split(/[\s\-\\\/]+/)
let keywords = query.split(/[\s\-\\/]+/)
if (keywords.length !== 1) {
keywords = [].concat(query, keywords)
}
@ -92,7 +92,7 @@ export function search (query) {
const postUrl = post.slug || ''
if (postTitle && postContent) {
keywords.forEach((keyword, i) => {
keywords.forEach(keyword => {
// From https://github.com/sindresorhus/escape-string-regexp
const regEx = new RegExp(
keyword.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'),
@ -108,7 +108,9 @@ export function search (query) {
isMatch = false
} else {
isMatch = true
if (indexContent < 0) indexContent = 0
if (indexContent < 0) {
indexContent = 0
}
let start = 0
let end = 0
@ -116,7 +118,9 @@ export function search (query) {
start = indexContent < 11 ? 0 : indexContent - 10
end = start === 0 ? 70 : indexContent + keyword.length + 60
if (end > postContent.length) end = postContent.length
if (end > postContent.length) {
end = postContent.length
}
const matchContent =
'...' +
@ -144,7 +148,7 @@ export function search (query) {
return matchingResults
}
export function init (config, vm) {
export function init(config, vm) {
helper = Docsify
const isAuto = config.paths === 'auto'
@ -163,7 +167,9 @@ export function init (config, vm) {
let count = 0
paths.forEach(path => {
if (INDEXS[path]) return count++
if (INDEXS[path]) {
return count++
}
helper
.get(vm.router.getFile(path), false, vm.config.requestHeaders)

View file

@ -1,6 +1,6 @@
import mediumZoom from 'medium-zoom'
function install (hook) {
function install(hook) {
let zoom
hook.doneEach(_ => {