Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eslint fixes for v4x #989

Merged
merged 13 commits into from
Dec 30, 2019
20 changes: 17 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"extends": "xo-space/browser",
"rules": {
"semi": [2, "never"],
"semi": [
2,
"never"
],
"no-return-assign": "off",
"no-unused-expressions": "off",
"no-new-func": "off",
Expand All @@ -10,11 +13,22 @@
"max-params": "off",
"no-script-url": "off",
"camelcase": "off",
"no-warning-comments": "off"
"object-curly-spacing": "off",
"no-warning-comments": "off",
"no-negated-condition": "off",
"eqeqeq": "warn",
"no-eq-null": "warn",
"max-statements-per-line": "warn"
},
"globals": {
"Docsify": true,
"$docsify": true,
"process": true
},
"env": {
"browser": true,
"amd": true,
"node": true,
"jest": true
}
}
}
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
sudo: false
language: node_js
node_js: stable

script:
- npm run lint
10 changes: 5 additions & 5 deletions docs/_media/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const PORT = 8080

/// [demo]
const result = fetch(`${URL}:${PORT}`)
.then(function(response) {
return response.json();
.then(function (response) {
return response.json()
})
.then(function (myJson) {
console.log(JSON.stringify(myJson))
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
});
/// [demo]

result.then(console.log).catch(console.error)
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"serve:ssr": "cross-env SSR=1 node server",
"dev": "run-p serve watch:*",
"dev:ssr": "run-p serve:ssr watch:*",
"lint": "eslint {src,packages} --fix",
"lint": "eslint . --fix",
"test": "mocha test/*/**",
"css": "node build/css",
"watch:css": "npm run css -- -o themes -w",
Expand All @@ -48,8 +48,8 @@
},
"lint-staged": {
"*.js": [
"npm run lint",
"git add"
"npm run lint",
"git add"
]
},
"dependencies": {
Expand Down Expand Up @@ -98,4 +98,4 @@
"collective": {
"url": "https://opencollective.com/docsify"
}
}
}
3 changes: 3 additions & 0 deletions packages/docsify-server-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function mainTpl(config) {
if (config.repo) {
html += tpl.corner(config.repo)
}

if (config.coverpage) {
html += tpl.cover()
}
Expand Down Expand Up @@ -154,12 +155,14 @@ export default class Renderer {
if (!res.ok) {
throw Error()
}

content = await res.text()
this.lock = 0
} else {
content = await readFileSync(filePath, 'utf8')
this.lock = 0
}

return content
} catch (e) {
this.lock = this.lock || 0
Expand Down
8 changes: 6 additions & 2 deletions src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export default function () {
ext: '.md',
mergeNavbar: false,
formatUpdated: '',
// this config for the links inside markdown
// This config for the links inside markdown
externalLinkTarget: '_blank',
// this config for the corner
// This config for the corner
cornerExternalLinkTarget: '_blank',
externalLinkRel: 'noopener',
routerMode: 'hash',
Expand Down Expand Up @@ -55,15 +55,19 @@ export default function () {
if (config.loadSidebar === true) {
config.loadSidebar = '_sidebar' + config.ext
}

if (config.loadNavbar === true) {
config.loadNavbar = '_navbar' + config.ext
}

if (config.coverpage === true) {
config.coverpage = '_coverpage' + config.ext
}

if (config.repo === true) {
config.repo = ''
}

if (config.name === true) {
config.name = ''
}
Expand Down
13 changes: 10 additions & 3 deletions src/core/event/scroll.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isMobile} from '../util/env'
import { isMobile } from '../util/env'
import * as dom from '../util/dom'
import Tweezer from 'tweezer.js'

Expand All @@ -12,6 +12,7 @@ function scrollTo(el) {
if (scroller) {
scroller.stop()
}

enableScrollEvent = false
scroller = new Tweezer({
start: window.pageYOffset,
Expand All @@ -30,6 +31,7 @@ function highlight(path) {
if (!enableScrollEvent) {
return
}

const sidebar = dom.getNode('.sidebar')
const anchors = dom.findAll('.anchor')
const wrap = dom.find(sidebar, '.sidebar-nav')
Expand All @@ -45,14 +47,17 @@ function highlight(path) {
if (!last) {
last = node
}

break
} else {
last = node
}
}

if (!last) {
return
}

const li = nav[getNavKey(decodeURIComponent(path), last.getAttribute('data-id'))]

if (!li || li === active) {
Expand Down Expand Up @@ -88,7 +93,7 @@ export function scrollActiveSidebar(router) {

const sidebar = dom.getNode('.sidebar')
let lis = []
if (sidebar != null) {
if (sidebar !== null && sidebar !== undefined) {
lis = dom.findAll(sidebar, 'li')
}

Expand All @@ -98,10 +103,11 @@ export function scrollActiveSidebar(router) {
if (!a) {
continue
}

let href = a.getAttribute('href')

if (href !== '/') {
const {query: {id}, path} = router.parse(href)
const { query: { id }, path } = router.parse(href)
if (id) {
href = getNavKey(path, id)
}
Expand All @@ -115,6 +121,7 @@ export function scrollActiveSidebar(router) {
if (isMobile) {
return
}

const path = router.getCurrentPath()
dom.off('scroll', () => highlight(path))
dom.on('scroll', () => highlight(path))
Expand Down
26 changes: 16 additions & 10 deletions src/core/event/sidebar.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import {isMobile} from '../util/env'
import { isMobile } from '../util/env'
import * as dom from '../util/dom'

const title = dom.$.title
/**
* Toggle button
* @param {Element} el Button to be toggled
* @void
*/
export function btn(el) {
const toggle = _ => dom.body.classList.toggle('close')

el = dom.getNode(el)
if (el == null) {
if (el === null || el === undefined) {
return
}

dom.on(el, 'click', e => {
e.stopPropagation()
toggle()
Expand All @@ -27,10 +30,11 @@ export function btn(el) {

export function collapse(el) {
el = dom.getNode(el)
if (el == null) {
if (el === null || el === undefined) {
return
}
dom.on(el, 'click', ({target}) => {

dom.on(el, 'click', ({ target }) => {
if (
target.nodeName === 'A' &&
target.nextSibling &&
Expand All @@ -46,6 +50,7 @@ export function sticky() {
if (!cover) {
return
}

const coverHeight = cover.getBoundingClientRect().height

if (window.pageYOffset >= coverHeight || cover.classList.contains('hidden')) {
Expand All @@ -57,18 +62,19 @@ export function sticky() {

/**
* Get and active link
* @param {object} router
* @param {string|element} el
* @param {Boolean} isParent acitve parent
* @param {Boolean} autoTitle auto set title
* @return {element}
* @param {Object} router Router
* @param {String|Element} el Target element
* @param {Boolean} isParent Active parent
* @param {Boolean} autoTitle Automatically set title
* @return {Element} Active element
*/
export function getAndActive(router, el, isParent, autoTitle) {
el = dom.getNode(el)
let links = []
if (el != null) {
if (el !== null && el !== undefined) {
links = dom.findAll(el, 'a')
}

const hash = decodeURI(router.toURL(router.getCurrentPath()))
let target

Expand Down
17 changes: 10 additions & 7 deletions src/core/fetch/ajax.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import progressbar from '../render/progressbar'
import {noop, hasOwn} from '../util/core'
import { noop, hasOwn } from '../util/core'

const cache = {}

/**
* Simple ajax get
* @param {string} url
* @param {boolean} [hasBar=false] has progress bar
* @return { then(resolve, reject), abort }
* Ajax GET implmentation
* @param {string} url Resource URL
* @param {boolean} [hasBar=false] Has progress bar
* @param {String[]} headers Array of headers
* @return {Promise} Promise response
*/
export function get(url, hasBar = false, headers = {}) {
const xhr = new XMLHttpRequest()
const on = function () {
xhr.addEventListener.apply(xhr, arguments)
}

const cached = cache[url]

if (cached) {
return {then: cb => cb(cached.content, cached.opt), abort: noop}
return { then: cb => cb(cached.content, cached.opt), abort: noop }
}

xhr.open('GET', url)
Expand All @@ -26,6 +28,7 @@ export function get(url, hasBar = false, headers = {}) {
xhr.setRequestHeader(i, headers[i])
}
}

xhr.send()

return {
Expand All @@ -47,7 +50,7 @@ export function get(url, hasBar = false, headers = {}) {
}

on('error', error)
on('load', ({target}) => {
on('load', ({ target }) => {
if (target.status >= 400) {
error(target)
} else {
Expand Down
Loading