Skip to content

Commit

Permalink
Merge 32ef029 into 3fbbe5d
Browse files Browse the repository at this point in the history
  • Loading branch information
segayuu committed Nov 5, 2018
2 parents 3fbbe5d + 32ef029 commit 938f911
Show file tree
Hide file tree
Showing 39 changed files with 126 additions and 281 deletions.
8 changes: 2 additions & 6 deletions lib/box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,8 @@ Box.prototype._readDir = function(base, fn, prefix = '') {
const self = this;
const { ignore } = self;

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 => {
Expand Down
9 changes: 3 additions & 6 deletions lib/extend/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down
12 changes: 5 additions & 7 deletions lib/extend/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 !== '') {
Expand Down
15 changes: 4 additions & 11 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,14 @@ Hexo.prototype._generate = function(options = {}) {

const path = route.format(item.path);
const { data } = item;
let { layout } = item;

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;
Expand All @@ -378,11 +373,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));
Expand Down
11 changes: 2 additions & 9 deletions lib/hexo/load_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,7 @@ function findConfigPath(path) {
const { dir, name } = parse(path);

return fs.readdir(dir).then(files => {
let item;

for (let i = 0, len = files.length; i < len; i++) {
item = files[i];

if (item.startsWith(name)) {
return join(dir, item);
}
}
const item = files.find(item => item.startsWith(basename));
if (item != null) return pathFn.join(dirname, item);
});
}
14 changes: 3 additions & 11 deletions lib/hexo/locals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -56,12 +50,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;
}
Expand Down
11 changes: 2 additions & 9 deletions lib/hexo/multi_config_path.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ const yml = require('js-yaml');

module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) {
const { log } = ctx;

const defaultPath = join(base, '_config.yml');
let paths;

if (!configPaths) {
log.w('No config file entered.');
return join(base, '_config.yml');
}

let paths;
// determine if comma or space separated
if (configPaths.includes(',')) {
paths = configPaths.replace(' ', '').split(',');
Expand All @@ -37,13 +36,7 @@ module.exports = ctx => function multiConfigPath(base, configPaths, outputDir) {
const combinedConfig = {};
let count = 0;
for (let i = 0; i < numPaths; i++) {
let configPath = '';

if (isAbsolute(paths[i])) {
configPath = paths[i];
} else {
configPath = 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.`);
Expand Down
7 changes: 1 addition & 6 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,9 @@ Post.prototype._renderScaffold = function(data) {
}).then(frontMatter => {
const { separator } = yfmSplit;
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)) {
Expand Down
3 changes: 1 addition & 2 deletions lib/hexo/register_models.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};
11 changes: 1 addition & 10 deletions lib/hexo/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,7 @@ Router.format = Router.prototype.format = path => {

Router.prototype.list = function() {
const { routes } = this;
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) {
Expand Down
9 changes: 1 addition & 8 deletions lib/hexo/scaffold.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion lib/models/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -27,6 +26,8 @@ module.exports = ctx => {
});
}

let mtime;

// Get file stats
return statFn(id).then(stats => {
mtime = stats.mtime;
Expand Down
6 changes: 4 additions & 2 deletions lib/models/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,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});

Expand Down
11 changes: 2 additions & 9 deletions lib/plugins/console/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,9 @@ function configConsole(args) {
}).then(config => {
if (!config) config = {};

let result = '';

setProperty(config, key, castValue(value));

if (ext === '.json') {
result = JSON.stringify(config);
} else {
result = yaml.dump(config);
}
const result = ext === '.json' ? JSON.stringify(config) : yaml.dump(config);

return fs.writeFile(configPath, result);
});
Expand All @@ -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] || {};
}

Expand Down
13 changes: 6 additions & 7 deletions lib/plugins/console/list/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] || {};
}
}
Expand All @@ -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({
Expand Down
3 changes: 1 addition & 2 deletions lib/plugins/console/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
7 changes: 1 addition & 6 deletions lib/plugins/filter/before_post_render/backtick_code_block.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,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];
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/filter/new_post_path.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ function newPostPathFilter(data = {}, replace) {
const newPostName = config.new_post_name;
const permalinkDefaults = config.permalink_defaults;
const { path, layout, slug } = data;
let target = '';

if (!permalink || permalink.rule !== newPostName) {
permalink = new Permalink(newPostName);
}

let target = '';

if (path) {
switch (layout) {
case 'page':
Expand All @@ -57,7 +58,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'),
Expand All @@ -69,7 +69,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];
}

Expand Down

0 comments on commit 938f911

Please sign in to comment.