From c6f70a3aa5b19b7a2834ecdbc15104652c35024b Mon Sep 17 00:00:00 2001 From: segayuu Date: Wed, 24 Oct 2018 09:49:14 +0900 Subject: [PATCH 01/12] Replace simple assign of key/value between Objects with Object.assign() --- lib/plugins/helper/image_tag.js | 12 ++---------- lib/plugins/helper/link_to.js | 12 ++---------- lib/plugins/helper/mail_to.js | 12 ++---------- 3 files changed, 6 insertions(+), 30 deletions(-) diff --git a/lib/plugins/helper/image_tag.js b/lib/plugins/helper/image_tag.js index 4467afffbc..fa7a0a98eb 100644 --- a/lib/plugins/helper/image_tag.js +++ b/lib/plugins/helper/image_tag.js @@ -3,17 +3,9 @@ const htmlTag = require('hexo-util').htmlTag; function imageTagHelper(path, options = {}) { - const attrs = { + const attrs = Object.assign({ src: this.url_for(path) - }; - - const keys = Object.keys(options); - let key = ''; - - for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; - attrs[key] = options[key]; - } + }, options); if (attrs.class && Array.isArray(attrs.class)) { attrs.class = attrs.class.join(' '); diff --git a/lib/plugins/helper/link_to.js b/lib/plugins/helper/link_to.js index 579fb7a9dc..0429fd7b6e 100644 --- a/lib/plugins/helper/link_to.js +++ b/lib/plugins/helper/link_to.js @@ -8,18 +8,10 @@ function linkToHelper(path, text, options) { if (!text) text = path.replace(/^https?:\/\/|\/$/g, ''); - const attrs = { + const attrs = Object.assign({ href: this.url_for(path), title: text - }; - - const keys = Object.keys(options); - let key = ''; - - for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; - attrs[key] = options[key]; - } + }, options); if (attrs.external) { attrs.target = '_blank'; diff --git a/lib/plugins/helper/mail_to.js b/lib/plugins/helper/mail_to.js index 7d50ea4fb6..0a39ea4d20 100644 --- a/lib/plugins/helper/mail_to.js +++ b/lib/plugins/helper/mail_to.js @@ -7,18 +7,10 @@ function mailToHelper(path, text, options = {}) { if (Array.isArray(path)) path = path.join(','); if (!text) text = path; - const attrs = { + const attrs = Object.assign({ href: `mailto:${path}`, title: text - }; - - const keys = Object.keys(options); - let key = ''; - - for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; - attrs[key] = options[key]; - } + }, options); if (attrs.class && Array.isArray(attrs.class)) { attrs.class = attrs.class.join(' '); From a72afcdf42c6e5e288017c10a1855171c21653de Mon Sep 17 00:00:00 2001 From: segayuu Date: Wed, 24 Oct 2018 09:55:35 +0900 Subject: [PATCH 02/12] Use Array Iteration methods --- lib/box/index.js | 8 ++------ lib/extend/filter.js | 9 +++------ lib/hexo/load_config.js | 11 ++--------- lib/hexo/router.js | 11 +---------- lib/hexo/scaffold.js | 9 +-------- lib/plugins/helper/css.js | 18 ++++++------------ lib/plugins/processor/common.js | 9 +-------- lib/plugins/processor/post.js | 9 ++------- 8 files changed, 18 insertions(+), 66 deletions(-) diff --git a/lib/box/index.js b/lib/box/index.js index ca58d023c3..f10700c0f8 100644 --- a/lib/box/index.js +++ b/lib/box/index.js @@ -110,12 +110,8 @@ Box.prototype._readDir = function(base, fn, prefix = '') { const self = this; const ignore = self.ignore; - if (base && ignore && ignore.length) { - for (let i = 0, len = ignore.length; i < len; i++) { - if (minimatch(base, ignore[i])) { - return Promise.resolve('Ignoring dir.'); - } - } + if (base && ignore && ignore.length && ignore.some(item => minimatch(base, item))) { + return Promise.resolve('Ignoring dir.'); } return fs.readdir(base).map(path => fs.stat(join(base, path)).then(stats => { diff --git a/lib/extend/filter.js b/lib/extend/filter.js index 1e51c93c87..9b2a569a32 100644 --- a/lib/extend/filter.js +++ b/lib/extend/filter.js @@ -45,12 +45,9 @@ Filter.prototype.unregister = function(type, fn) { const list = this.list(type); if (!list || !list.length) return; - for (let i = 0, len = list.length; i < len; i++) { - if (list[i] === fn) { - list.splice(i, 1); - break; - } - } + const index = list.findIndex(item => item === fn); + + if (index != null) list.splice(index, 1); }; Filter.prototype.exec = function(type, data, options = {}) { diff --git a/lib/hexo/load_config.js b/lib/hexo/load_config.js index e8773fa03d..2b921bed5d 100644 --- a/lib/hexo/load_config.js +++ b/lib/hexo/load_config.js @@ -53,14 +53,7 @@ function findConfigPath(path) { const basename = pathFn.basename(path, extname); return fs.readdir(dirname).then(files => { - let item; - - for (let i = 0, len = files.length; i < len; i++) { - item = files[i]; - - if (item.startsWith(basename)) { - return pathFn.join(dirname, item); - } - } + const item = files.find(item => item.startsWith(basename)); + if (item != null) return pathFn.join(dirname, item); }); } diff --git a/lib/hexo/router.js b/lib/hexo/router.js index 93256d9d76..711278069b 100644 --- a/lib/hexo/router.js +++ b/lib/hexo/router.js @@ -33,16 +33,7 @@ Router.format = Router.prototype.format = path => { Router.prototype.list = function() { const routes = this.routes; - const keys = Object.keys(routes); - const arr = []; - let key; - - for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; - if (routes[key]) arr.push(key); - } - - return arr; + return Object.keys(routes).filter(key => routes[key]); }; Router.prototype.get = function(path) { diff --git a/lib/hexo/scaffold.js b/lib/hexo/scaffold.js index 46c73fb90d..3d141341e9 100644 --- a/lib/hexo/scaffold.js +++ b/lib/hexo/scaffold.js @@ -35,14 +35,7 @@ Scaffold.prototype._listDir = function() { }; Scaffold.prototype._getScaffold = function(name) { - return this._listDir().then(list => { - let item; - - for (let i = 0, len = list.length; i < len; i++) { - item = list[i]; - if (item.name === name) return item; - } - }); + return this._listDir().then(list => list.find(item => item.name === name)); }; Scaffold.prototype.get = function(name, callback) { diff --git a/lib/plugins/helper/css.js b/lib/plugins/helper/css.js index 810790c0df..6af6e86775 100644 --- a/lib/plugins/helper/css.js +++ b/lib/plugins/helper/css.js @@ -1,23 +1,17 @@ 'use strict'; function cssHelper(...args) { - let result = ''; - let path = ''; - - for (let i = 0, len = args.length; i < len; i++) { - path = args[i]; - - if (i) result += '\n'; + return args.reduce((_result, path, i) => { + if (i) _result += '\n'; if (Array.isArray(path)) { - result += cssHelper.apply(this, path); + _result += cssHelper.apply(this, path); } else { if (!path.includes('?') && !path.endsWith('.css')) path += '.css'; - result += ``; + _result += ``; } - } - - return result; + return _result; + }, ''); } module.exports = cssHelper; diff --git a/lib/plugins/processor/common.js b/lib/plugins/processor/common.js index 7f2a154b06..0e5b328726 100644 --- a/lib/plugins/processor/common.js +++ b/lib/plugins/processor/common.js @@ -50,12 +50,5 @@ exports.isMatch = (path, patterns) => { if (!patterns) return false; if (!Array.isArray(patterns)) patterns = [patterns]; - patterns = patterns.filter(value => value); - if (!patterns.length) return false; - - for (let i = 0, len = patterns.length; i < len; i++) { - if (minimatch(path, patterns[i])) return true; - } - - return false; + return patterns.some(pattern => pattern && minimatch(path, pattern)); }; diff --git a/lib/plugins/processor/post.js b/lib/plugins/processor/post.js index ebc9b0e14c..1ff3546daf 100644 --- a/lib/plugins/processor/post.js +++ b/lib/plugins/processor/post.js @@ -186,13 +186,9 @@ module.exports = ctx => { } // TODO: Better post searching - const posts = Post.toArray(); - let post; + const post = Post.toArray().find(post => file.source.startsWith(post.asset_dir)); - for (let i = 0, len = posts.length; i < len; i++) { - post = posts[i]; - - if (file.source.startsWith(post.asset_dir)) { + if (post != null) { return PostAsset.save({ _id: id, slug: file.source.substring(post.asset_dir.length), @@ -201,7 +197,6 @@ module.exports = ctx => { renderable: file.params.renderable }); } - } if (doc) { return doc.remove(); From 02ece29ed24c43f113ce552a4d8694bb93a0f2d2 Mon Sep 17 00:00:00 2001 From: segayuu Date: Wed, 24 Oct 2018 10:06:23 +0900 Subject: [PATCH 03/12] Change the variable used only in the loop to the variable in the loop --- lib/extend/filter.js | 3 +-- lib/hexo/register_models.js | 3 +-- lib/plugins/console/list/route.js | 13 ++++++------- lib/plugins/console/new.js | 3 +-- lib/plugins/filter/new_post_path.js | 6 +++--- lib/plugins/filter/post_permalink.js | 3 +-- lib/plugins/helper/js.js | 3 +-- lib/plugins/helper/list_archives.js | 10 ++++------ lib/plugins/helper/number_format.js | 5 ++--- lib/plugins/helper/paginator.js | 13 ++++++------- lib/plugins/helper/toc.js | 9 ++++----- lib/plugins/processor/post.js | 19 +++++++++---------- lib/plugins/tag/asset_img.js | 19 ++++++------------- lib/plugins/tag/img.js | 7 ++----- lib/plugins/tag/link.js | 3 +-- lib/theme/view.js | 3 +-- 16 files changed, 49 insertions(+), 73 deletions(-) diff --git a/lib/extend/filter.js b/lib/extend/filter.js index 9b2a569a32..2da259b688 100644 --- a/lib/extend/filter.js +++ b/lib/extend/filter.js @@ -67,12 +67,11 @@ Filter.prototype.execSync = function(type, data, options = {}) { const filters = this.list(type); const ctx = options.context; const args = options.args || []; - let result; args.unshift(data); for (let i = 0, len = filters.length; i < len; i++) { - result = filters[i].apply(ctx, args); + const result = filters[i].apply(ctx, args); args[0] = result == null ? args[0] : result; } diff --git a/lib/hexo/register_models.js b/lib/hexo/register_models.js index 54073da006..6245e122a1 100644 --- a/lib/hexo/register_models.js +++ b/lib/hexo/register_models.js @@ -6,10 +6,9 @@ module.exports = ctx => { const db = ctx.database; const keys = Object.keys(models); - let key = ''; for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; + const key = keys[i]; db.model(key, models[key](ctx)); } }; diff --git a/lib/plugins/console/list/route.js b/lib/plugins/console/list/route.js index 912e008233..389dd59457 100644 --- a/lib/plugins/console/list/route.js +++ b/lib/plugins/console/list/route.js @@ -17,14 +17,14 @@ function listRoute() { function buildTree(routes) { const obj = {}; - let item, j, lenj, seg, cursor; + let cursor; for (let i = 0, len = routes.length; i < len; i++) { - item = routes[i].split('/'); + const item = routes[i].split('/'); cursor = obj; - for (j = 0, lenj = item.length; j < lenj; j++) { - seg = item[j]; + for (let j = 0, lenj = item.length; j < lenj; j++) { + const seg = item[j]; cursor = cursor[seg] = cursor[seg] || {}; } } @@ -35,11 +35,10 @@ function buildTree(routes) { function buildNodes(tree) { const keys = Object.keys(tree); const nodes = []; - let key, item; for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; - item = tree[key]; + const key = keys[i]; + const item = tree[key]; if (Object.keys(item).length) { nodes.push({ diff --git a/lib/plugins/console/new.js b/lib/plugins/console/new.js index daa6871267..bc33082fe3 100644 --- a/lib/plugins/console/new.js +++ b/lib/plugins/console/new.js @@ -31,11 +31,10 @@ function newConsole(args) { }; const keys = Object.keys(args); - let key = ''; const self = this; for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; + const key = keys[i]; if (!reservedKeys[key]) data[key] = args[key]; } diff --git a/lib/plugins/filter/new_post_path.js b/lib/plugins/filter/new_post_path.js index 17beee56b1..0660c325e4 100644 --- a/lib/plugins/filter/new_post_path.js +++ b/lib/plugins/filter/new_post_path.js @@ -28,12 +28,13 @@ function newPostPathFilter(data = {}, replace) { const path = data.path; const layout = data.layout; const slug = data.slug; - let target = ''; if (!permalink || permalink.rule !== newPostName) { permalink = new Permalink(newPostName); } + let target = ''; + if (path) { switch (layout) { case 'page': @@ -60,7 +61,6 @@ function newPostPathFilter(data = {}, replace) { default: { const date = moment(data.date || Date.now()); const keys = Object.keys(data); - let key = ''; const filenameData = { year: date.format('YYYY'), @@ -72,7 +72,7 @@ function newPostPathFilter(data = {}, replace) { }; for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; + const key = keys[i]; if (!reservedKeys[key]) filenameData[key] = data[key]; } diff --git a/lib/plugins/filter/post_permalink.js b/lib/plugins/filter/post_permalink.js index 936e445ec3..0c7f61ccf1 100644 --- a/lib/plugins/filter/post_permalink.js +++ b/lib/plugins/filter/post_permalink.js @@ -33,10 +33,9 @@ function postPermalinkFilter(data) { } const keys = Object.keys(data); - let key = ''; for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; + const key = keys[i]; if (meta.hasOwnProperty(key)) continue; // Use Object.getOwnPropertyDescriptor to copy getters to avoid "Maximum call diff --git a/lib/plugins/helper/js.js b/lib/plugins/helper/js.js index f8b561a99a..87a18fa6b7 100644 --- a/lib/plugins/helper/js.js +++ b/lib/plugins/helper/js.js @@ -2,10 +2,9 @@ function jsHelper(...args) { let result = ''; - let path = ''; for (let i = 0, len = args.length; i < len; i++) { - path = args[i]; + let path = args[i]; if (i) result += '\n'; diff --git a/lib/plugins/helper/list_archives.js b/lib/plugins/helper/list_archives.js index 1e956380a0..3f8f7464b3 100644 --- a/lib/plugins/helper/list_archives.js +++ b/lib/plugins/helper/list_archives.js @@ -61,13 +61,11 @@ function listArchivesHelper(options = {}) { return self.url_for(url); } - let item, i, len; - if (style === 'list') { result += `
    `; - for (i = 0, len = data.length; i < len; i++) { - item = data[i]; + for (let i = 0, len = data.length; i < len; i++) { + const item = data[i]; result += `
  • `; @@ -84,8 +82,8 @@ function listArchivesHelper(options = {}) { result += '
'; } else { - for (i = 0, len = data.length; i < len; i++) { - item = data[i]; + for (let i = 0, len = data.length; i < len; i++) { + const item = data[i]; if (i) result += separator; diff --git a/lib/plugins/helper/number_format.js b/lib/plugins/helper/number_format.js index 8f6fb3b80f..a8f5266b4b 100644 --- a/lib/plugins/helper/number_format.js +++ b/lib/plugins/helper/number_format.js @@ -7,7 +7,6 @@ function numberFormatHelper(num, options = {}) { const delimiter = options.delimiter || ','; const separator = options.separator || '.'; const precision = options.precision; - let i, len; if (delimiter) { const beforeArr = []; @@ -16,7 +15,7 @@ function numberFormatHelper(num, options = {}) { if (beforeFirst) beforeArr.push(before.substr(0, beforeFirst)); - for (i = beforeFirst; i < beforeLength; i += 3) { + for (let i = beforeFirst; i < beforeLength; i += 3) { beforeArr.push(before.substr(i, 3)); } @@ -34,7 +33,7 @@ function numberFormatHelper(num, options = {}) { afterResult = after.substr(0, precision - 1) + (afterLast < 5 ? last : last + 1); } else { afterResult = after; - for (i = 0, len = precision - afterLength; i < len; i++) { + for (let i = 0, len = precision - afterLength; i < len; i++) { afterResult += '0'; } } diff --git a/lib/plugins/helper/paginator.js b/lib/plugins/helper/paginator.js index 2e2e08cb03..e57295f89d 100644 --- a/lib/plugins/helper/paginator.js +++ b/lib/plugins/helper/paginator.js @@ -14,7 +14,6 @@ function paginatorHelper(options = {}) { const transform = options.transform; const self = this; let result = ''; - let i; if (!current) return ''; @@ -35,7 +34,7 @@ function paginatorHelper(options = {}) { if (options.show_all) { // Display pages on the left side of the current page - for (i = 1; i < current; i++) { + for (let i = 1; i < current; i++) { result += pageLink(i); } @@ -43,7 +42,7 @@ function paginatorHelper(options = {}) { result += currentPage; // Display pages on the right side of the current page - for (i = current + 1; i <= total; i++) { + for (let i = current + 1; i <= total; i++) { result += pageLink(i); } } else { @@ -55,7 +54,7 @@ function paginatorHelper(options = {}) { const spaceHtml = `${space}`; // Display pages on the left edge - for (i = 1; i <= leftEnd; i++) { + for (let i = 1; i <= leftEnd; i++) { result += pageLink(i); } @@ -66,7 +65,7 @@ function paginatorHelper(options = {}) { // Display left middle pages if (leftMid > leftEnd) { - for (i = leftMid; i < current; i++) { + for (let i = leftMid; i < current; i++) { result += pageLink(i); } } @@ -76,7 +75,7 @@ function paginatorHelper(options = {}) { // Display right middle pages if (rightMid < rightEnd) { - for (i = current + 1; i <= rightMid; i++) { + for (let i = current + 1; i <= rightMid; i++) { result += pageLink(i); } } @@ -87,7 +86,7 @@ function paginatorHelper(options = {}) { } // Dispaly pages on the right edge - for (i = rightEnd; i <= total; i++) { + for (let i = rightEnd; i <= total; i++) { result += pageLink(i); } } diff --git a/lib/plugins/helper/toc.js b/lib/plugins/helper/toc.js index ea74ae011b..04cc47ff3a 100644 --- a/lib/plugins/helper/toc.js +++ b/lib/plugins/helper/toc.js @@ -19,7 +19,6 @@ function tocHelper(str, options = {}) { const lastNumber = [0, 0, 0, 0, 0, 0]; let firstLevel = 0; let lastLevel = 0; - let i = 0; headings.each(function() { const level = +this.name[1]; @@ -28,12 +27,12 @@ function tocHelper(str, options = {}) { lastNumber[level - 1]++; - for (i = level; i <= 5; i++) { + for (let i = level; i <= 5; i++) { lastNumber[i] = 0; } if (firstLevel) { - for (i = level; i < lastLevel; i++) { + for (let i = level; i < lastLevel; i++) { result += ''; } @@ -52,7 +51,7 @@ function tocHelper(str, options = {}) { if (listNumber) { result += ``; - for (i = firstLevel - 1; i < level; i++) { + for (let i = firstLevel - 1; i < level; i++) { result += `${lastNumber[i]}.`; } @@ -64,7 +63,7 @@ function tocHelper(str, options = {}) { lastLevel = level; }); - for (i = firstLevel - 1; i < lastLevel; i++) { + for (let i = firstLevel - 1; i < lastLevel; i++) { result += ''; } diff --git a/lib/plugins/processor/post.js b/lib/plugins/processor/post.js index 1ff3546daf..63383a2859 100644 --- a/lib/plugins/processor/post.js +++ b/lib/plugins/processor/post.js @@ -51,7 +51,6 @@ module.exports = ctx => { const data = yfm(content); const info = parseFilename(config.new_post_name, path); const keys = Object.keys(info); - let key; data.source = file.path; data.raw = content; @@ -64,7 +63,7 @@ module.exports = ctx => { } for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; + const key = keys[i]; if (!preservedKeys[key]) data[key] = info[key]; } @@ -189,14 +188,14 @@ module.exports = ctx => { const post = Post.toArray().find(post => file.source.startsWith(post.asset_dir)); if (post != null) { - return PostAsset.save({ - _id: id, - slug: file.source.substring(post.asset_dir.length), - post: post._id, - modified: file.type !== 'skip', - renderable: file.params.renderable - }); - } + return PostAsset.save({ + _id: id, + slug: file.source.substring(post.asset_dir.length), + post: post._id, + modified: file.type !== 'skip', + renderable: file.params.renderable + }); + } if (doc) { return doc.remove(); diff --git a/lib/plugins/tag/asset_img.js b/lib/plugins/tag/asset_img.js index 7525e7df38..752d0deb05 100644 --- a/lib/plugins/tag/asset_img.js +++ b/lib/plugins/tag/asset_img.js @@ -13,22 +13,15 @@ module.exports = ctx => { const PostAsset = ctx.model('PostAsset'); return function assetImgTag(args) { - let asset; - let item = ''; - let i = 0; const len = args.length; // Find image URL - for (; i < len; i++) { - item = args[i]; - asset = PostAsset.findOne({post: this._id, slug: item}); - if (asset) break; + for (let i = 0; i < len; i++) { + const asset = PostAsset.findOne({post: this._id, slug: args[i]}); + if (asset) { + args[i] = url.resolve('/', asset.path); + return img(ctx)(args); + } } - - if (!asset) return; - - args[i] = url.resolve('/', asset.path); - - return img(ctx)(args); }; }; diff --git a/lib/plugins/tag/img.js b/lib/plugins/tag/img.js index b26c275374..741a88796e 100644 --- a/lib/plugins/tag/img.js +++ b/lib/plugins/tag/img.js @@ -33,17 +33,14 @@ module.exports = ctx => { } return function imgTag(args, content) { - const classes = []; let meta = ''; let width, height, title, alt, src; - let item = ''; let i = 0; - const len = args.length; // Find image URL and class name - for (; i < len; i++) { - item = args[i]; + for (const len = args.length; i < len; i++) { + let item = args[i]; if (rUrl.test(item)) { src = makeUrl(item); diff --git a/lib/plugins/tag/link.js b/lib/plugins/tag/link.js index 43a3973884..2e69c827cf 100644 --- a/lib/plugins/tag/link.js +++ b/lib/plugins/tag/link.js @@ -17,13 +17,12 @@ function linkTag(args, content) { const text = []; let external = false; let title = ''; - let item = ''; let i = 0; const len = args.length; // Find link URL and text for (; i < len; i++) { - item = args[i]; + const item = args[i]; if (rUrl.test(item)) { url = item; diff --git a/lib/theme/view.js b/lib/theme/view.js index 3cecb86171..a980eeadcf 100644 --- a/lib/theme/view.js +++ b/lib/theme/view.js @@ -70,10 +70,9 @@ View.prototype._buildLocals = function(locals) { View.prototype._bindHelpers = function(locals) { const helpers = this._helper.list(); const keys = Object.keys(helpers); - let key = ''; for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; + const key = keys[i]; locals[key] = helpers[key].bind(locals); } From 7db1741aff2ba044a186a177d4af251acd972d8e Mon Sep 17 00:00:00 2001 From: segayuu Date: Wed, 24 Oct 2018 10:17:29 +0900 Subject: [PATCH 04/12] Change to declare variables in scopes that are used only within scope --- lib/hexo/index.js | 6 ++---- lib/hexo/locals.js | 6 ++---- lib/plugins/helper/tagcloud.js | 14 ++++++-------- lib/plugins/tag/blockquote.js | 9 ++++----- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/lib/hexo/index.js b/lib/hexo/index.js index 80374b1b41..4145259171 100644 --- a/lib/hexo/index.js +++ b/lib/hexo/index.js @@ -383,11 +383,9 @@ Hexo.prototype._generate = function(options) { route.set(path, () => { if (options.cache && cache != null) return cache; - let view, name; - for (let i = 0; i < layoutLength; i++) { - name = layout[i]; - view = theme.getView(name); + const name = layout[i]; + const view = theme.getView(name); if (view) { log.debug('Rendering %s: %s', name, chalk.magenta(path)); diff --git a/lib/hexo/locals.js b/lib/hexo/locals.js index 6c127d69fe..c3d227482e 100644 --- a/lib/hexo/locals.js +++ b/lib/hexo/locals.js @@ -56,12 +56,10 @@ Locals.prototype.invalidate = function() { Locals.prototype.toObject = function() { const result = {}; const keys = Object.keys(this.getters); - let key = ''; - let item; for (let i = 0, len = keys.length; i < len; i++) { - key = keys[i]; - item = this.get(key); + const key = keys[i]; + const item = this.get(key); if (item != null) result[key] = item; } diff --git a/lib/plugins/helper/tagcloud.js b/lib/plugins/helper/tagcloud.js index e7796bd9f1..59319310ee 100644 --- a/lib/plugins/helper/tagcloud.js +++ b/lib/plugins/helper/tagcloud.js @@ -263,33 +263,31 @@ Color.prototype.parse = function(color) { return; } - let match, txt, code; - if (rHex3.test(color)) { - txt = color.substring(1); - code = parseInt(txt, 16); + const txt = color.substring(1); + const code = parseInt(txt, 16); this.r = ((code & 0xF00) >> 8) * 17; this.g = ((code & 0xF0) >> 4) * 17; this.b = (code & 0xF) * 17; this.a = 1; } else if (rHex6.test(color)) { - txt = color.substring(1); - code = parseInt(txt, 16); + const txt = color.substring(1); + const code = parseInt(txt, 16); this.r = (code & 0xFF0000) >> 16; this.g = (code & 0xFF00) >> 8; this.b = code & 0xFF; this.a = 1; } else if (rRGB.test(color)) { - match = color.match(rRGB); + const match = color.match(rRGB); this.r = match[1] | 0; this.g = match[2] | 0; this.b = match[3] | 0; this.a = match[4] ? +match[4] : 1; } else if (rHSL.test(color)) { - match = color.match(rHSL); + const match = color.match(rHSL); const h = +match[1] / 360; const s = +match[2] / 100; diff --git a/lib/plugins/tag/blockquote.js b/lib/plugins/tag/blockquote.js index 5bf72f6f2d..5c8a5062a6 100644 --- a/lib/plugins/tag/blockquote.js +++ b/lib/plugins/tag/blockquote.js @@ -25,24 +25,23 @@ module.exports = ctx => function blockquoteTag(args, content) { let title = ''; let footer = ''; let result = ''; - let match; if (str) { if (rFullCiteWithTitle.test(str)) { - match = str.match(rFullCiteWithTitle); + const match = str.match(rFullCiteWithTitle); author = match[1]; source = match[2] + match[3]; title = ctx.config.titlecase ? titlecase(match[4]) : match[4]; } else if (rFullCite.test(str)) { - match = str.match(rFullCite); + const match = str.match(rFullCite); author = match[1]; source = match[2] + match[3]; } else if (rAuthorTitle.test(str)) { - match = str.match(rAuthorTitle); + const match = str.match(rAuthorTitle); author = match[1]; title = ctx.config.titlecase ? titlecase(match[2]) : match[2]; } else if (rAuthor.test(str)) { - match = str.match(rAuthor); + const match = str.match(rAuthor); author = match[1]; } From 2782ac74b7d8d1fa20107a23f3c9af1e8f44eddb Mon Sep 17 00:00:00 2001 From: segayuu Date: Wed, 24 Oct 2018 10:49:07 +0900 Subject: [PATCH 05/12] let to const --- lib/hexo/index.js | 9 ++------- lib/hexo/locals.js | 8 +------- lib/hexo/multi_config_path.js | 8 +------- lib/hexo/post.js | 7 +------ lib/plugins/console/config.js | 11 ++--------- .../before_post_render/backtick_code_block.js | 7 +------ lib/plugins/helper/list_categories.js | 5 +---- lib/plugins/tag/img.js | 14 ++++++-------- lib/plugins/tag/pullquote.js | 8 ++------ 9 files changed, 17 insertions(+), 60 deletions(-) diff --git a/lib/hexo/index.js b/lib/hexo/index.js index 4145259171..6e39723ddb 100644 --- a/lib/hexo/index.js +++ b/lib/hexo/index.js @@ -355,19 +355,14 @@ Hexo.prototype._generate = function(options) { const path = route.format(item.path); const data = item.data; - let layout = item.layout; newRouteList.push(path); - if (!layout) { + if (!item.layout) { return route.set(path, data); } - if (Array.isArray(layout)) { - layout = layout.filter((item, index, self) => self.indexOf(item) === index); - } else { - layout = [layout]; - } + const layout = Array.isArray(item.layout) ? item.layout.filter((item, index, self) => self.indexOf(item) === index) : [item.layout]; const locals = new Locals(path, data); const layoutLength = layout.length; diff --git a/lib/hexo/locals.js b/lib/hexo/locals.js index c3d227482e..0ae994dc45 100644 --- a/lib/hexo/locals.js +++ b/lib/hexo/locals.js @@ -24,13 +24,7 @@ Locals.prototype.set = function(name, value) { if (typeof name !== 'string') throw new TypeError('name must be a string!'); if (value == null) throw new TypeError('value is required!'); - let getter; - - if (typeof value === 'function') { - getter = value; - } else { - getter = () => value; - } + const getter = typeof value === 'function' ? value : () => value; this.getters[name] = getter; this.cache[name] = null; diff --git a/lib/hexo/multi_config_path.js b/lib/hexo/multi_config_path.js index 6982f3f1d3..6076aecf1e 100644 --- a/lib/hexo/multi_config_path.js +++ b/lib/hexo/multi_config_path.js @@ -37,13 +37,7 @@ module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) { const combinedConfig = {}; let count = 0; for (let i = 0; i < numPaths; i++) { - let configPath = ''; - - if (pathFn.isAbsolute(paths[i])) { - configPath = paths[i]; - } else { - configPath = pathFn.join(base, paths[i]); - } + const configPath = pathFn.isAbsolute(paths[i]) ? paths[i] : pathFn.join(base, paths[i]); if (!fs.existsSync(configPath)) { log.w(`Config file ${paths[i]} not found.`); diff --git a/lib/hexo/post.js b/lib/hexo/post.js index 0e74128d0a..c5ff3d0d6d 100644 --- a/lib/hexo/post.js +++ b/lib/hexo/post.js @@ -119,14 +119,9 @@ Post.prototype._renderScaffold = function(data) { }).then(frontMatter => { const separator = yfmSplit.separator; const jsonMode = separator[0] === ';'; - let obj; // Parse front-matter - if (jsonMode) { - obj = JSON.parse(`{${frontMatter}}`); - } else { - obj = yaml.load(frontMatter); - } + const obj = jsonMode ? JSON.parse(`{${frontMatter}}`) : yaml.load(frontMatter); // Add data which are not in the front-matter for (const key of Object.keys(data)) { diff --git a/lib/plugins/console/config.js b/lib/plugins/console/config.js index 1cbf14a89c..8fdfef5222 100644 --- a/lib/plugins/console/config.js +++ b/lib/plugins/console/config.js @@ -30,15 +30,9 @@ function configConsole(args) { }).then(config => { if (!config) config = {}; - let result = ''; - setProperty(config, key, castValue(value)); - if (extname === '.json') { - result = JSON.stringify(config); - } else { - result = yaml.dump(config); - } + const result = extname === '.json' ? JSON.stringify(config) : yaml.dump(config); return fs.writeFile(configPath, result); }); @@ -58,11 +52,10 @@ function getProperty(obj, key) { function setProperty(obj, key, value) { const split = key.split('.'); let cursor = obj; - let name = ''; const lastKey = split.pop(); for (let i = 0, len = split.length; i < len; i++) { - name = split[i]; + const name = split[i]; cursor = cursor[name] = cursor[name] || {}; } diff --git a/lib/plugins/filter/before_post_render/backtick_code_block.js b/lib/plugins/filter/before_post_render/backtick_code_block.js index 3dd879181f..5667f3be1b 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.js +++ b/lib/plugins/filter/before_post_render/backtick_code_block.js @@ -35,13 +35,8 @@ function backtickCodeBlock(data) { } if (args) { - let match; + const match = rAllOptions.exec(args) || rLangCaption.exec(args); - if (rAllOptions.test(args)) { - match = args.match(rAllOptions); - } else if (rLangCaption.test(args)) { - match = args.match(rLangCaption); - } if (match) { options.lang = match[1]; diff --git a/lib/plugins/helper/list_categories.js b/lib/plugins/helper/list_categories.js index f921fa1371..11839b4b99 100644 --- a/lib/plugins/helper/list_categories.js +++ b/lib/plugins/helper/list_categories.js @@ -62,10 +62,7 @@ function listCategoriesHelper(categories, options) { } } - let additionalClassName = ''; - if (child && childrenIndicator) { - additionalClassName = ` ${childrenIndicator}`; - } + const additionalClassName = child && childrenIndicator ? ` ${childrenIndicator}` : ''; result += `
  • `; diff --git a/lib/plugins/tag/img.js b/lib/plugins/tag/img.js index 741a88796e..b3619c5283 100644 --- a/lib/plugins/tag/img.js +++ b/lib/plugins/tag/img.js @@ -34,7 +34,6 @@ module.exports = ctx => { return function imgTag(args, content) { const classes = []; - let meta = ''; let width, height, title, alt, src; let i = 0; @@ -68,14 +67,13 @@ module.exports = ctx => { } } - meta = args.join(' '); - } + const match = rMeta.exec(args.join(' ')); - // Find image title and alt - if (meta && rMeta.test(meta)) { - const match = meta.match(rMeta); - title = match[1]; - alt = match[2]; + // Find image title and alt + if (match != null) { + title = match[1]; + alt = match[2]; + } } const attrs = { diff --git a/lib/plugins/tag/pullquote.js b/lib/plugins/tag/pullquote.js index c3c347689b..8a2f945d4a 100644 --- a/lib/plugins/tag/pullquote.js +++ b/lib/plugins/tag/pullquote.js @@ -9,13 +9,9 @@ * {% endpullquote %} */ module.exports = ctx => function pullquoteTag(args, content) { - let result = ''; - args.unshift('pullquote'); - result += `
    `; - result += ctx.render.renderSync({text: content, engine: 'markdown'}); - result += '
    '; + const result = ctx.render.renderSync({text: content, engine: 'markdown'}); - return result; + return `
    ${result}
    `; }; From f8658d07bcea8d0e8bd5d437783994cde584b824 Mon Sep 17 00:00:00 2001 From: segayuu Date: Wed, 24 Oct 2018 11:08:10 +0900 Subject: [PATCH 06/12] Reduce nested if statements --- lib/extend/tag.js | 12 +++++------- lib/plugins/helper/list_categories.js | 13 +++---------- lib/plugins/tag/img.js | 11 +++-------- 3 files changed, 11 insertions(+), 25 deletions(-) diff --git a/lib/extend/tag.js b/lib/extend/tag.js index 8350c8610a..8e1b2c5fd2 100644 --- a/lib/extend/tag.js +++ b/lib/extend/tag.js @@ -33,12 +33,10 @@ Tag.prototype.register = function(name, fn, options) { } else { tag = new NunjucksAsyncTag(name, fn); } + } else if (options.ends) { + tag = new NunjucksBlock(name, fn); } else { - if (options.ends) { - tag = new NunjucksBlock(name, fn); - } else { - tag = new NunjucksTag(name, fn); - } + tag = new NunjucksTag(name, fn); } this.env.addExtension(name, tag); @@ -77,11 +75,11 @@ NunjucksTag.prototype.parse = function(parser, nodes, lexer) { NunjucksTag.prototype._parseArgs = (parser, nodes, lexer) => { const tag = parser.nextToken(); const node = new nodes.NodeList(tag.lineno, tag.colno); - let token; - const argarray = new nodes.Array(tag.lineno, tag.colno); + let token; let argitem = ''; + while ((token = parser.nextToken(true))) { if (token.type === lexer.TOKEN_WHITESPACE || token.type === lexer.TOKEN_BLOCK_END) { if (argitem !== '') { diff --git a/lib/plugins/helper/list_categories.js b/lib/plugins/helper/list_categories.js index 11839b4b99..24c4d69d56 100644 --- a/lib/plugins/helper/list_categories.js +++ b/lib/plugins/helper/list_categories.js @@ -20,7 +20,6 @@ function listCategoriesHelper(categories, options) { const showCurrent = options.show_current || false; const suffix = options.suffix || ''; const childrenIndicator = options.hasOwnProperty('children_indicator') ? options.children_indicator : false; - let result = ''; const self = this; function prepareQuery(parent) { @@ -55,11 +54,7 @@ function listCategoriesHelper(categories, options) { } // special case: category page - if (!isCurrent && self.page.base) { - if (self.page.base.startsWith(cat.path)) { - isCurrent = true; - } - } + isCurrent = isCurrent || (self.page.base && self.page.base.startsWith(cat.path)); } const additionalClassName = child && childrenIndicator ? ` ${childrenIndicator}` : ''; @@ -108,12 +103,10 @@ function listCategoriesHelper(categories, options) { } if (style === 'list') { - result += `
      ${hierarchicalList(0)}
    `; - } else { - result += flatList(0); + return `
      ${hierarchicalList(0)}
    `; } - return result; + return flatList(0); } module.exports = listCategoriesHelper; diff --git a/lib/plugins/tag/img.js b/lib/plugins/tag/img.js index b3619c5283..49c8922a14 100644 --- a/lib/plugins/tag/img.js +++ b/lib/plugins/tag/img.js @@ -39,18 +39,13 @@ module.exports = ctx => { // Find image URL and class name for (const len = args.length; i < len; i++) { - let item = args[i]; + const item = args[i]; - if (rUrl.test(item)) { + if (rUrl.test(item) || item[0] === '/') { src = makeUrl(item); break; } else { - if (item[0] === '/') { - src = makeUrl(item); - break; - } else { - classes.push(item); - } + classes.push(item); } } From 30a973f026b3d844263d01b17c2cd62137f4e08e Mon Sep 17 00:00:00 2001 From: segayuu Date: Wed, 24 Oct 2018 11:09:48 +0900 Subject: [PATCH 07/12] reduce path namespace object --- lib/hexo/multi_config_path.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/hexo/multi_config_path.js b/lib/hexo/multi_config_path.js index 6076aecf1e..617c832194 100644 --- a/lib/hexo/multi_config_path.js +++ b/lib/hexo/multi_config_path.js @@ -1,27 +1,26 @@ 'use strict'; -const pathFn = require('path'); +const { join, extname, isAbsolute, resolve } = require('path'); const fs = require('hexo-fs'); const merge = require('lodash/merge'); const yml = require('js-yaml'); module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) { const log = ctx.log; - - const defaultPath = pathFn.join(base, '_config.yml'); - let paths; + const defaultPath = join(base, '_config.yml'); if (!configPaths) { log.w('No config file entered.'); - return pathFn.join(base, '_config.yml'); + return join(base, '_config.yml'); } + let paths; // determine if comma or space separated if (configPaths.includes(',')) { paths = configPaths.replace(' ', '').split(','); } else { // only one config - let configPath = pathFn.isAbsolute(configPaths) ? configPaths : pathFn.resolve(base, configPaths); + let configPath = isAbsolute(configPaths) ? configPaths : resolve(base, configPaths); if (!fs.existsSync(configPath)) { log.w(`Config file ${configPaths} not found, using default.`); @@ -37,7 +36,7 @@ module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) { const combinedConfig = {}; let count = 0; for (let i = 0; i < numPaths; i++) { - const configPath = pathFn.isAbsolute(paths[i]) ? paths[i] : pathFn.join(base, paths[i]); + const configPath = isAbsolute(paths[i]) ? paths[i] : join(base, paths[i]); if (!fs.existsSync(configPath)) { log.w(`Config file ${paths[i]} not found.`); @@ -46,7 +45,7 @@ module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) { // files read synchronously to ensure proper overwrite order const file = fs.readFileSync(configPath); - const ext = pathFn.extname(paths[i]).toLowerCase(); + const ext = extname(paths[i]).toLowerCase(); if (ext === '.yml') { merge(combinedConfig, yml.load(file)); @@ -67,7 +66,7 @@ module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) { log.i('Config based on', count, 'files'); const multiconfigRoot = outputDir || base; - const outputPath = pathFn.join(multiconfigRoot, '_multiconfig.yml'); + const outputPath = join(multiconfigRoot, '_multiconfig.yml'); log.d(`Writing _multiconfig.yml to ${outputPath}`); From 91fdb682060299002acc157ed2d6e0913d3f5f66 Mon Sep 17 00:00:00 2001 From: segayuu Date: Wed, 24 Oct 2018 11:13:49 +0900 Subject: [PATCH 08/12] Delay variable declaration --- lib/models/cache.js | 3 ++- lib/models/category.js | 6 ++++-- lib/plugins/helper/list_posts.js | 3 ++- lib/plugins/helper/open_graph.js | 3 ++- lib/plugins/helper/paginator.js | 2 +- lib/plugins/tag/blockquote.js | 3 +-- lib/plugins/tag/img.js | 4 +++- 7 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/models/cache.js b/lib/models/cache.js index 0e383ccc4e..b9fde49757 100644 --- a/lib/models/cache.js +++ b/lib/models/cache.js @@ -13,7 +13,6 @@ module.exports = ctx => { Cache.static('compareFile', function(id, hashFn, statFn) { const cache = this.findById(id); const self = this; - let mtime; // If cache does not exist, then it must be a new file. We have to get both // file hash and stats. @@ -27,6 +26,8 @@ module.exports = ctx => { }); } + let mtime; + // Get file stats return statFn(id).then(stats => { mtime = stats.mtime; diff --git a/lib/models/category.js b/lib/models/category.js index 0c3afa271c..22273dee2e 100644 --- a/lib/models/category.js +++ b/lib/models/category.js @@ -11,17 +11,19 @@ module.exports = ctx => { }); Category.virtual('slug').get(function() { - const map = ctx.config.category_map || {}; let name = this.name; - let str = ''; if (!name) return; + let str = ''; + if (this.parent) { const parent = ctx.model('Category').findById(this.parent); str += `${parent.slug}/`; } + const map = ctx.config.category_map || {}; + name = map[name] || name; str += slugize(name, {transform: ctx.config.filename_case}); diff --git a/lib/plugins/helper/list_posts.js b/lib/plugins/helper/list_posts.js index 648063558c..7bc759ac85 100644 --- a/lib/plugins/helper/list_posts.js +++ b/lib/plugins/helper/list_posts.js @@ -15,7 +15,6 @@ function listPostsHelper(posts, options) { const transform = options.transform; const separator = options.hasOwnProperty('separator') ? options.separator : ', '; const amount = options.amount || 6; - let result = ''; const self = this; // Sort the posts @@ -24,6 +23,8 @@ function listPostsHelper(posts, options) { // Limit the number of posts if (amount) posts = posts.limit(amount); + let result = ''; + if (style === 'list') { result += `
      `; diff --git a/lib/plugins/helper/open_graph.js b/lib/plugins/helper/open_graph.js index e9d5fbc54a..a7b05722f1 100644 --- a/lib/plugins/helper/open_graph.js +++ b/lib/plugins/helper/open_graph.js @@ -46,7 +46,6 @@ function openGraphHelper(options = {}) { const twitterCard = options.twitter_card || 'summary'; const updated = options.updated !== false ? options.updated || page.updated : false; const language = options.language || page.lang || page.language || config.language; - let result = ''; if (!Array.isArray(images)) images = [images]; @@ -72,6 +71,8 @@ function openGraphHelper(options = {}) { }); } + let result = ''; + if (description) { result += meta('description', description, false); } diff --git a/lib/plugins/helper/paginator.js b/lib/plugins/helper/paginator.js index e57295f89d..4c03ebec96 100644 --- a/lib/plugins/helper/paginator.js +++ b/lib/plugins/helper/paginator.js @@ -13,7 +13,6 @@ function paginatorHelper(options = {}) { const prevNext = options.hasOwnProperty('prev_next') ? options.prev_next : true; const transform = options.transform; const self = this; - let result = ''; if (!current) return ''; @@ -27,6 +26,7 @@ function paginatorHelper(options = {}) { return `${transform ? transform(i) : i}`; } + let result = ''; // Display the link to the previous page if (prevNext && current > 1) { result += ``; diff --git a/lib/plugins/tag/blockquote.js b/lib/plugins/tag/blockquote.js index 5c8a5062a6..04c779125d 100644 --- a/lib/plugins/tag/blockquote.js +++ b/lib/plugins/tag/blockquote.js @@ -24,7 +24,6 @@ module.exports = ctx => function blockquoteTag(args, content) { let source = ''; let title = ''; let footer = ''; - let result = ''; if (str) { if (rFullCiteWithTitle.test(str)) { @@ -55,7 +54,7 @@ module.exports = ctx => function blockquoteTag(args, content) { } } - result += '
      '; + let result = '
      '; result += ctx.render.renderSync({text: content, engine: 'markdown'}); if (footer) result += `
      ${footer}
      `; result += '
      '; diff --git a/lib/plugins/tag/img.js b/lib/plugins/tag/img.js index 49c8922a14..8cbcbcf80d 100644 --- a/lib/plugins/tag/img.js +++ b/lib/plugins/tag/img.js @@ -34,7 +34,7 @@ module.exports = ctx => { return function imgTag(args, content) { const classes = []; - let width, height, title, alt, src; + let src; let i = 0; // Find image URL and class name @@ -52,6 +52,8 @@ module.exports = ctx => { // Delete image URL and class name from arguments args = args.slice(i + 1); + let width, height, title, alt; + // Find image width and height if (args.length) { if (!/\D+/.test(args[0])) { From 77c57dc1deeb8418f530d210b287972983464eb3 Mon Sep 17 00:00:00 2001 From: segayuu Date: Wed, 24 Oct 2018 11:23:24 +0900 Subject: [PATCH 09/12] Change the declaration part of the counter variable into on the loop. --- lib/plugins/tag/img.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/plugins/tag/img.js b/lib/plugins/tag/img.js index 8cbcbcf80d..ec51cda51e 100644 --- a/lib/plugins/tag/img.js +++ b/lib/plugins/tag/img.js @@ -35,27 +35,26 @@ module.exports = ctx => { return function imgTag(args, content) { const classes = []; let src; - let i = 0; // Find image URL and class name - for (const len = args.length; i < len; i++) { + for (let i = 0, len = args.length; i < len; i++) { const item = args[i]; if (rUrl.test(item) || item[0] === '/') { src = makeUrl(item); + + // Delete image URL and class name from arguments + args = args.slice(i + 1); break; } else { classes.push(item); } } - // Delete image URL and class name from arguments - args = args.slice(i + 1); - let width, height, title, alt; // Find image width and height - if (args.length) { + if (args && args.length) { if (!/\D+/.test(args[0])) { width = args.shift(); From e4fba52b33dd3a398e229128bf2269f79522c59f Mon Sep 17 00:00:00 2001 From: segayuu Date: Wed, 24 Oct 2018 11:31:36 +0900 Subject: [PATCH 10/12] Changed not to use Array#slice() --- lib/plugins/tag/img.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/plugins/tag/img.js b/lib/plugins/tag/img.js index ec51cda51e..eeb19c2f67 100644 --- a/lib/plugins/tag/img.js +++ b/lib/plugins/tag/img.js @@ -37,14 +37,10 @@ module.exports = ctx => { let src; // Find image URL and class name - for (let i = 0, len = args.length; i < len; i++) { - const item = args[i]; - + while (args.length > 0) { + const item = args.shift(); if (rUrl.test(item) || item[0] === '/') { src = makeUrl(item); - - // Delete image URL and class name from arguments - args = args.slice(i + 1); break; } else { classes.push(item); From 8ae17002f207bcfd17e4e8fdd561da63e62b99c2 Mon Sep 17 00:00:00 2001 From: segayuu Date: Wed, 24 Oct 2018 11:34:01 +0900 Subject: [PATCH 11/12] Use Bluebird#tap() --- lib/plugins/generator/asset.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/plugins/generator/asset.js b/lib/plugins/generator/asset.js index dce1b0c947..f816677ce3 100644 --- a/lib/plugins/generator/asset.js +++ b/lib/plugins/generator/asset.js @@ -9,9 +9,8 @@ function assetGenerator(locals) { const self = this; function process(name) { - return Promise.filter(self.model(name).toArray(), asset => fs.exists(asset.source).then(exist => { - if (exist) return exist; - return asset.remove().thenReturn(exist); + return Promise.filter(self.model(name).toArray(), asset => fs.exists(asset.source).tap(exist => { + if (!exist) return asset.remove(); })).map(asset => { const source = asset.source; let path = asset.path; From a2587167edfc7e3cccfa81996cbc31cead683a26 Mon Sep 17 00:00:00 2001 From: segayuu Date: Mon, 5 Nov 2018 10:23:41 +0900 Subject: [PATCH 12/12] Fix test error --- lib/hexo/load_config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/hexo/load_config.js b/lib/hexo/load_config.js index a20f22e5d0..6d9d3f890b 100644 --- a/lib/hexo/load_config.js +++ b/lib/hexo/load_config.js @@ -49,7 +49,7 @@ function findConfigPath(path) { const { dir, name } = parse(path); return fs.readdir(dir).then(files => { - const item = files.find(item => item.startsWith(basename)); - if (item != null) return pathFn.join(dirname, item); + const item = files.find(item => item.startsWith(name)); + if (item != null) return join(dir, item); }); }