diff --git a/lib/box/index.js b/lib/box/index.js index 50287e0e59..1fb44a5f70 100644 --- a/lib/box/index.js +++ b/lib/box/index.js @@ -23,7 +23,7 @@ function Box(ctx, base, options) { persistent: true }, options); - if (base.substring(base.length - 1) !== sep) { + if (!base.endsWith(sep)) { base += sep; } @@ -158,11 +158,11 @@ Box.prototype.process = function(callback) { // Check existing files in cache const relativeBase = escapeBackslash(base.substring(ctx.base_dir.length)); - const cacheFiles = Cache.filter(item => item._id.substring(0, relativeBase.length) === relativeBase).map(item => item._id.substring(relativeBase.length)); + const cacheFiles = Cache.filter(item => item._id.startsWith(relativeBase)).map(item => item._id.substring(relativeBase.length)); // Read files from directory return self._readDir(base, file => self._processFile(file.type, file.path)).map(file => file.path).then(files => // Handle deleted files - Promise.filter(cacheFiles, path => !~files.indexOf(path)).map(path => self._processFile(File.TYPE_DELETE, path))); + Promise.filter(cacheFiles, path => !files.includes(path)).map(path => self._processFile(File.TYPE_DELETE, path))); }).catch(err => { if (err.cause && err.cause.code !== 'ENOENT') throw err; }).asCallback(callback); diff --git a/lib/extend/tag.js b/lib/extend/tag.js index 79b59ed933..8350c8610a 100644 --- a/lib/extend/tag.js +++ b/lib/extend/tag.js @@ -55,21 +55,12 @@ Tag.prototype.render = function(str, options, callback) { const cache = []; - function escapeContent(str) { - return ``; - } + const escapeContent = str => ``; - const env = this.env; + str = str.replace(/
[\s\S]*?<\/code><\/pre>/gm, escapeContent); - return new Promise((resolve, reject) => { - str = str.replace(/ [\s\S]*?<\/code><\/pre>/gm, escapeContent); - env.renderString(str, options, (err, result) => { - if (err) return reject(err); - resolve(result.replace(rPlaceholder, function(...args) { - return cache[args[1]]; - })); - }); - }); + return Promise.fromCallback(cb => { this.env.renderString(str, options, cb); }) + .then(result => result.replace(rPlaceholder, (_, index) => cache[index])); }; function NunjucksTag(name, fn) { diff --git a/lib/hexo/load_config.js b/lib/hexo/load_config.js index b61dc63d6c..443937e378 100644 --- a/lib/hexo/load_config.js +++ b/lib/hexo/load_config.js @@ -53,12 +53,12 @@ function findConfigPath(path) { const basename = pathFn.basename(path, extname); return fs.readdir(dirname).then(files => { - let item = ''; + let item; for (let i = 0, len = files.length; i < len; i++) { item = files[i]; - if (item.substring(0, basename.length) === basename) { + if (item.startsWith(basename)) { return pathFn.join(dirname, item); } } diff --git a/lib/hexo/multi_config_path.js b/lib/hexo/multi_config_path.js index db84a3588a..2886c8bfcf 100644 --- a/lib/hexo/multi_config_path.js +++ b/lib/hexo/multi_config_path.js @@ -17,9 +17,8 @@ module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) { } // determine if comma or space separated - if (configPaths.indexOf(',') > -1) { + if (configPaths.includes(',')) { paths = configPaths.replace(' ', '').split(','); - } else { // only one config let configPath = pathFn.isAbsolute(configPaths) ? configPaths : pathFn.resolve(base, configPaths); diff --git a/lib/plugins/console/generate.js b/lib/plugins/console/generate.js index 0407567409..a89d6833f6 100644 --- a/lib/plugins/console/generate.js +++ b/lib/plugins/console/generate.js @@ -123,14 +123,14 @@ function generateConsole(args = {}) { throw err; }).then(() => { const routeList = route.list(); - const publicFiles = Cache.filter(item => item._id.substring(0, 7) === 'public/').map(item => item._id.substring(7)); + const publicFiles = Cache.filter(item => item._id.startsWith('public/')).map(item => item._id.substring(7)); return Promise.all([ // Generate files Promise.map(routeList, generateFile), // Clean files - Promise.filter(publicFiles, path => !~routeList.indexOf(path)).map(deleteFile) + Promise.filter(publicFiles, path => !routeList.includes(path)).map(deleteFile) ]); }).spread(result => { const interval = prettyHrtime(process.hrtime(start)); diff --git a/lib/plugins/filter/template_locals/i18n.js b/lib/plugins/filter/template_locals/i18n.js index 50c54b14e4..c125d2896c 100644 --- a/lib/plugins/filter/template_locals/i18n.js +++ b/lib/plugins/filter/template_locals/i18n.js @@ -17,7 +17,7 @@ function i18nLocalsFilter(locals) { const pattern = new Pattern(`${i18nDir}/*path`); const data = pattern.match(locals.path); - if (data && data.lang && ~i18nLanguages.indexOf(data.lang)) { + if (data && data.lang && i18nLanguages.includes(data.lang)) { lang = data.lang; page.canonical_path = data.path; } else { diff --git a/lib/plugins/helper/css.js b/lib/plugins/helper/css.js index 84b7fb77a7..810790c0df 100644 --- a/lib/plugins/helper/css.js +++ b/lib/plugins/helper/css.js @@ -12,7 +12,7 @@ function cssHelper(...args) { if (Array.isArray(path)) { result += cssHelper.apply(this, path); } else { - if (path.indexOf('?') < 0 && path.substring(path.length - 4, path.length) !== '.css') path += '.css'; + if (!path.includes('?') && !path.endsWith('.css')) path += '.css'; result += ``; } } diff --git a/lib/plugins/helper/is.js b/lib/plugins/helper/is.js index f904147441..203619d68b 100644 --- a/lib/plugins/helper/is.js +++ b/lib/plugins/helper/is.js @@ -1,13 +1,11 @@ 'use strict'; function isCurrentHelper(path = '/', strict) { - const currentPath = this.path.replace(/^[^\/].*/, _ => // eslint-disable-line no-useless-escape - `/${_}`); + const currentPath = this.path.replace(/^[^/].*/, '/$&'); if (strict) { if (path[path.length - 1] === '/') path += 'index.html'; - path = path.replace(/^[^\/].*/, _ => // eslint-disable-line no-useless-escape - `/${_}`); + path = path.replace(/^[^/].*/, '/$&'); return currentPath === path; } @@ -16,10 +14,9 @@ function isCurrentHelper(path = '/', strict) { if (path === '/') return currentPath === '/index.html'; - path = path.replace(/^[^\/].*/, _ => // eslint-disable-line no-useless-escape - `/${_}`); + path = path.replace(/^[^/].*/, '/$&'); - return currentPath.substring(0, path.length) === path; + return currentPath.startsWith(path); } function isHomeHelper() { diff --git a/lib/plugins/helper/js.js b/lib/plugins/helper/js.js index 99c1a1a365..f8b561a99a 100644 --- a/lib/plugins/helper/js.js +++ b/lib/plugins/helper/js.js @@ -12,7 +12,7 @@ function jsHelper(...args) { if (Array.isArray(path)) { result += jsHelper.apply(this, path); } else { - if (path.indexOf('?') < 0 && path.substring(path.length - 3, path.length) !== '.js') path += '.js'; + if (!path.includes('?') && !path.endsWith('.js')) path += '.js'; result += ``; } } diff --git a/lib/plugins/helper/list_categories.js b/lib/plugins/helper/list_categories.js index d07c0bb1f2..f921fa1371 100644 --- a/lib/plugins/helper/list_categories.js +++ b/lib/plugins/helper/list_categories.js @@ -56,7 +56,7 @@ function listCategoriesHelper(categories, options) { // special case: category page if (!isCurrent && self.page.base) { - if (self.page.base.indexOf(cat.path) === 0) { + if (self.page.base.startsWith(cat.path)) { isCurrent = true; } } diff --git a/lib/plugins/helper/tagcloud.js b/lib/plugins/helper/tagcloud.js index c3e34b8d83..e7796bd9f1 100644 --- a/lib/plugins/helper/tagcloud.js +++ b/lib/plugins/helper/tagcloud.js @@ -204,7 +204,7 @@ function tagcloudHelper(tags, options) { tags.sort('length').forEach(tag => { const length = tag.length; - if (~sizes.indexOf(length)) return; + if (sizes.includes(length)) return; sizes.push(length); }); diff --git a/lib/plugins/helper/url_for.js b/lib/plugins/helper/url_for.js index d9a6863569..964ea49617 100644 --- a/lib/plugins/helper/url_for.js +++ b/lib/plugins/helper/url_for.js @@ -4,7 +4,7 @@ const url = require('url'); const _ = require('lodash'); function urlForHelper(path = '/', options) { - if (path[0] === '#' || path.substring(0, 2) === '//') { + if (path[0] === '#' || path.startsWith('//')) { return path; } diff --git a/lib/plugins/tag/img.js b/lib/plugins/tag/img.js index 50810e0822..b26c275374 100644 --- a/lib/plugins/tag/img.js +++ b/lib/plugins/tag/img.js @@ -17,7 +17,7 @@ module.exports = ctx => { const config = ctx.config; function makeUrl(path) { - if (path[0] === '#' || path.substring(0, 2) === '//') { + if (path[0] === '#' || path.startsWith('//')) { return path; } diff --git a/lib/theme/processors/source.js b/lib/theme/processors/source.js index b0130a228e..c291d19413 100644 --- a/lib/theme/processors/source.js +++ b/lib/theme/processors/source.js @@ -29,7 +29,7 @@ exports.pattern = new Pattern(path => { if (!_.startsWith(path, 'source/')) return false; path = path.substring(7); - if (common.isHiddenFile(path) || common.isTmpFile(path) || ~path.indexOf('node_modules')) return false; + if (common.isHiddenFile(path) || common.isTmpFile(path) || path.includes('node_modules')) return false; return {path}; });