Skip to content

Commit

Permalink
chore: prettier code
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Sep 22, 2017
1 parent a9704b5 commit aba2aec
Show file tree
Hide file tree
Showing 30 changed files with 1,546 additions and 747 deletions.
20 changes: 11 additions & 9 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@ var renderer = new Renderer({
alias: {
'/de-de/changelog': '/changelog',
'/zh-cn/changelog': '/changelog',
'/changelog': 'https://github.com/raw/QingWei-Li/docsify/master/CHANGELOG'
'/changelog':
'https://github.com/raw/QingWei-Li/docsify/master/CHANGELOG'
}
},
path: './'
})

http.createServer(function (req, res) {
serveStatic('.')(req, res, function () {
// TEST SSR
// renderer.renderToString(req.url)
http
.createServer(function (req, res) {
serveStatic('.')(req, res, function () {
// TEST SSR
// renderer.renderToString(req.url)
// .then(html => res.end(html))

res.writeHead(404, { 'Content-Type': 'text/html' })
res.end(fs.readFileSync('dev.html'))
res.writeHead(404, { 'Content-Type': 'text/html' })
res.end(fs.readFileSync('dev.html'))
})
})
}).listen(3000, '0.0.0.0')
.listen(3000, '0.0.0.0')

console.log(`\nListening at http://0.0.0.0:3000\n`)

5 changes: 4 additions & 1 deletion build/build-cover.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ var file = __dirname + '/../docs/_coverpage.md'
var cover = read(file, 'utf8').toString()

console.log('Replace version number in cover page...')
cover = cover.replace(/<small>(\S+)?<\/small>/g, '<small>' + version + '</small>')
cover = cover.replace(
/<small>(\S+)?<\/small>/g,
'<small>' + version + '</small>'
)
write(file, cover)
27 changes: 15 additions & 12 deletions build/build-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ var resolve = require('path').resolve
var postcss = require('postcss')
var isProd = process.argv[process.argv.length - 1] !== '--dev'

var processor = postcss([require('postcss-salad')({
features: {
precss: {
properties: {
preserve: true
var processor = postcss([
require('postcss-salad')({
features: {
precss: {
properties: {
preserve: true
}
}
}
}
})])
})
])

var saveMin = function (file, content) {
fs.writeFileSync(resolve(__dirname, '../lib/themes/', file), content)
Expand All @@ -31,17 +33,18 @@ var list = fs.readdirSync(resolve(__dirname, '../src/themes'))

list.forEach(function (file) {
if (!/\.css$/.test(file)) return
processor.process(load(file), { from: resolve(__dirname, '../src/themes/', file) })
processor
.process(load(file), { from: resolve(__dirname, '../src/themes/', file) })
.then(function (result) {
save(file, result.css)
console.log('salad - ' + file)
isProd && cssnano(loadLib(file))
.then(function (result) {
isProd &&
cssnano(loadLib(file)).then(function (result) {
saveMin(file, result.css)
console.log('cssnao - ' + file)
})
}).catch(function (err) {
})
.catch(function (err) {
console.log(err)
})
})

2 changes: 1 addition & 1 deletion build/build-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rollup
}
})
],
onwarn: function() {}
onwarn: function () {}
})
.then(function (bundle) {
var dest = 'packages/docsify-server-renderer/build.js'
Expand Down
22 changes: 11 additions & 11 deletions packages/docsify-server-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ function cwd (...args) {
}

function mainTpl (config) {
let html = `<nav class="app-nav${config.repo ? '' : ' no-badge'}"><!--navbar--></nav>`
let html = `<nav class="app-nav${config.repo
? ''
: ' no-badge'}"><!--navbar--></nav>`

if (config.repo) {
html += tpl.corner(config.repo)
Expand All @@ -28,11 +30,7 @@ function mainTpl (config) {
}

export default class Renderer {
constructor ({
template,
config,
cache
}) {
constructor ({ template, config, cache }) {
this.html = template
this.config = config = Object.assign({}, config, {
routerMode: 'history'
Expand All @@ -43,7 +41,10 @@ export default class Renderer {
this.compiler = new Compiler(config, this.router)

this.router.getCurrentPath = () => this.url
this._renderHtml('inject-config', `<script>window.$docsify = ${JSON.stringify(config)}</script>`)
this._renderHtml(
'inject-config',
`<script>window.$docsify = ${JSON.stringify(config)}</script>`
)
this._renderHtml('inject-app', mainTpl(config))

this.template = this.html
Expand All @@ -52,9 +53,7 @@ export default class Renderer {
_getPath (url) {
const file = this.router.getFile(url)

return isAbsolutePath(file)
? file
: cwd(`./${file}`)
return isAbsolutePath(file) ? file : cwd(`./${file}`)
}

async renderToString (url) {
Expand Down Expand Up @@ -94,7 +93,8 @@ export default class Renderer {

switch (type) {
case 'sidebar':
html = this.compiler.sidebar(html, maxLevel) +
html =
this.compiler.sidebar(html, maxLevel) +
`<script>window.__SUB_SIDEBAR__ = ${JSON.stringify(
this.compiler.subSidebar(subMaxLevel)
)}</script>`
Expand Down
57 changes: 31 additions & 26 deletions src/core/config.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
import { merge, hyphenate, isPrimitive } from './util/core'

const config = merge({
el: '#app',
repo: '',
maxLevel: 6,
subMaxLevel: 0,
loadSidebar: null,
loadNavbar: null,
homepage: 'README.md',
coverpage: '',
basePath: '',
auto2top: false,
name: '',
themeColor: '',
nameLink: window.location.pathname,
autoHeader: false,
executeScript: null,
noEmoji: false,
ga: '',
mergeNavbar: false,
formatUpdated: '',
externalLinkTarget: '_blank',
routerMode: 'hash',
noCompileLinks: []
}, window.$docsify)
const config = merge(
{
el: '#app',
repo: '',
maxLevel: 6,
subMaxLevel: 0,
loadSidebar: null,
loadNavbar: null,
homepage: 'README.md',
coverpage: '',
basePath: '',
auto2top: false,
name: '',
themeColor: '',
nameLink: window.location.pathname,
autoHeader: false,
executeScript: null,
noEmoji: false,
ga: '',
mergeNavbar: false,
formatUpdated: '',
externalLinkTarget: '_blank',
routerMode: 'hash',
noCompileLinks: []
},
window.$docsify
)

const script = document.currentScript ||
[].slice.call(document.getElementsByTagName('script'))
const script =
document.currentScript ||
[].slice
.call(document.getElementsByTagName('script'))
.filter(n => /docsify\./.test(n.src))[0]

if (script) {
Expand Down
31 changes: 16 additions & 15 deletions src/core/event/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ function scrollTo (el) {
end: el.getBoundingClientRect().top + window.scrollY,
duration: 500
})
.on('tick', v => window.scrollTo(0, v))
.on('done', () => { enableScrollEvent = true; scroller = null })
.begin()
.on('tick', v => window.scrollTo(0, v))
.on('done', () => {
enableScrollEvent = true
scroller = null
})
.begin()
}

function highlight () {
Expand All @@ -28,7 +31,7 @@ function highlight () {
const wrap = dom.find(sidebar, '.sidebar-nav')
let active = dom.find(sidebar, 'li.active')
const doc = document.documentElement
const top = (doc && doc.scrollTop || document.body.scrollTop) - coverHeight
const top = ((doc && doc.scrollTop) || document.body.scrollTop) - coverHeight
let last

for (let i = 0, len = anchors.length; i < len; i += 1) {
Expand Down Expand Up @@ -56,16 +59,10 @@ function highlight () {
const height = sidebar.clientHeight
const curOffset = 0
const cur = active.offsetTop + active.clientHeight + 40
const isInView = (
active.offsetTop >= wrap.scrollTop &&
cur <= wrap.scrollTop + height
)
const isInView =
active.offsetTop >= wrap.scrollTop && cur <= wrap.scrollTop + height
const notThan = cur - curOffset < height
const top = isInView
? wrap.scrollTop
: notThan
? curOffset
: cur - height
const top = isInView ? wrap.scrollTop : notThan ? curOffset : cur - height

sidebar.scrollTop = top
}
Expand Down Expand Up @@ -95,8 +92,12 @@ export function scrollActiveSidebar (router) {

dom.off('scroll', highlight)
dom.on('scroll', highlight)
dom.on(sidebar, 'mouseover', () => { hoverOver = true })
dom.on(sidebar, 'mouseleave', () => { hoverOver = false })
dom.on(sidebar, 'mouseover', () => {
hoverOver = true
})
dom.on(sidebar, 'mouseleave', () => {
hoverOver = false
})
}

export function scrollIntoView (id) {
Expand Down
31 changes: 16 additions & 15 deletions src/core/event/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ export function btn (el, router) {

const sidebar = dom.getNode('.sidebar')

isMobile && dom.on(dom.body, 'click', _ =>
dom.body.classList.contains('close') && toggle()
)
isMobile &&
dom.on(
dom.body,
'click',
_ => dom.body.classList.contains('close') && toggle()
)
dom.on(sidebar, 'click', _ =>
setTimeout((_ => getAndActive(router, sidebar, true, true), 0))
)
Expand Down Expand Up @@ -51,19 +54,17 @@ export function getAndActive (router, el, isParent, autoTitle) {
const hash = router.toURL(router.getCurrentPath())
let target

links
.sort((a, b) => b.href.length - a.href.length)
.forEach(a => {
const href = a.getAttribute('href')
const node = isParent ? a.parentNode : a
links.sort((a, b) => b.href.length - a.href.length).forEach(a => {
const href = a.getAttribute('href')
const node = isParent ? a.parentNode : a

if (hash.indexOf(href) === 0 && !target) {
target = a
dom.toggleClass(node, 'add', 'active')
} else {
dom.toggleClass(node, 'remove', 'active')
}
})
if (hash.indexOf(href) === 0 && !target) {
target = a
dom.toggleClass(node, 'add', 'active')
} else {
dom.toggleClass(node, 'remove', 'active')
}
})

if (autoTitle) {
dom.$.title = target ? `${target.innerText} - ${title}` : title
Expand Down
14 changes: 9 additions & 5 deletions src/core/fetch/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ export function get (url, hasBar = false) {
return {
then: function (success, error = noop) {
if (hasBar) {
const id = setInterval(_ => progressbar({
step: Math.floor(Math.random() * 5 + 1)
}), 500)
const id = setInterval(
_ =>
progressbar({
step: Math.floor(Math.random() * 5 + 1)
}),
500
)

on('progress', progressbar)
on('loadend', evt => {
Expand All @@ -42,12 +46,12 @@ export function get (url, hasBar = false) {
if (target.status >= 400) {
error(target)
} else {
const result = cache[url] = {
const result = (cache[url] = {
content: target.response,
opt: {
updatedAt: xhr.getResponseHeader('last-modified')
}
}
})

success(result.content, result.opt)
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/init/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function initLifecycle (vm) {
vm._hooks = {}
vm._lifecycle = {}
hooks.forEach(hook => {
const arr = vm._hooks[hook] = []
const arr = (vm._hooks[hook] = [])
vm._lifecycle[hook] = fn => arr.push(fn)
})
}
Expand Down
6 changes: 4 additions & 2 deletions src/core/render/slugify.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ const re = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,.\/:;<=>?@\[\]^`{|}~]/g
export function slugify (str) {
if (typeof str !== 'string') return ''

let slug = str.toLowerCase().trim()
let slug = str
.toLowerCase()
.trim()
.replace(/<[^>\d]+>/g, '')
.replace(re, '')
.replace(/\s/g, '-')
.replace(/-+/g, '-')
.replace(/^(\d)/, '_$1')
let count = cache[slug]

count = cache.hasOwnProperty(slug) ? (count + 1) : 0
count = cache.hasOwnProperty(slug) ? count + 1 : 0
cache[slug] = count

if (count) {
Expand Down
Loading

0 comments on commit aba2aec

Please sign in to comment.