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

perf: overall improvements #4783

Merged
merged 2 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 13 additions & 17 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,10 @@ class Hexo extends EventEmitter {
args = {};
}

return new Promise((resolve, reject) => {
const c = this.extend.console.get(name);
const c = this.extend.console.get(name);

if (c) {
Reflect.apply(c, this, [args]).then(resolve, reject);
} else {
reject(new Error(`Console \`${name}\` has not been registered yet!`));
}
}).asCallback(callback);
if (c) return Reflect.apply(c, this, [args]).asCallback(callback);
return Promise.reject(new Error(`Console \`${name}\` has not been registered yet!`));
}

model(name, schema) {
Expand Down Expand Up @@ -321,7 +316,8 @@ class Hexo extends EventEmitter {
]);
}).then(() => {
mergeCtxThemeConfig(this);
}).then(() => this._generate({cache: false})).asCallback(callback);
return this._generate({cache: false});
}).asCallback(callback);
}

watch(callback) {
Expand All @@ -346,7 +342,7 @@ class Hexo extends EventEmitter {
]);
}).then(() => {
mergeCtxThemeConfig(this);
}).then(() => {

this.source.on('processAfter', this._watchBox);
this.theme.on('processAfter', () => {
this._watchBox();
Expand Down Expand Up @@ -402,10 +398,8 @@ class Hexo extends EventEmitter {
return Promise.map(Object.keys(generators), key => {
const generator = generators[key];

return Reflect.apply(generator, this, [siteLocals]).then(data => {
log.debug('Generator: %s', magenta(key));
return data;
});
log.debug('Generator: %s', magenta(key));
return Reflect.apply(generator, this, [siteLocals]);
}).reduce((result, data) => {
return data ? result.concat(data) : result;
}, []);
Expand Down Expand Up @@ -434,10 +428,12 @@ class Hexo extends EventEmitter {
.thenReturn(path);
}).then(newRouteList => {
// Remove old routes
const removed = routeList.filter(item => !newRouteList.includes(item));
for (let i = 0, len = routeList.length; i < len; i++) {
const item = routeList[i];

for (let i = 0, len = removed.length; i < len; i++) {
route.remove(removed[i]);
if (!newRouteList.includes(item)) {
route.remove(item);
}
}
});
}
Expand Down
12 changes: 6 additions & 6 deletions lib/hexo/load_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ function loadModules(ctx) {
function loadScripts(ctx) {
const baseDirLength = ctx.base_dir.length;

function displayPath(path) {
return magenta(path.substring(baseDirLength));
}

return Promise.filter([
ctx.theme_script_dir,
ctx.script_dir
Expand All @@ -71,9 +67,13 @@ function loadScripts(ctx) {
const path = join(scriptDir, name);

return ctx.loadPlugin(path).then(() => {
ctx.log.debug('Script loaded: %s', displayPath(path));
ctx.log.debug('Script loaded: %s', displayPath(path, baseDirLength));
}).catch(err => {
ctx.log.error({err}, 'Script load failed: %s', displayPath(path));
ctx.log.error({err}, 'Script load failed: %s', displayPath(path, baseDirLength));
});
}));
}

function displayPath(path, baseDirLength) {
return magenta(path.substring(baseDirLength));
}
46 changes: 25 additions & 21 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ const rHexoPostRenderEscape = /<hexoPostRenderCodeBlock>([\s\S]+?)<\/hexoPostRen
const rSwigPlaceHolder = /(?:<|&lt;)!--swig\uFFFC(\d+)--(?:>|&gt;)/g;
const rCodeBlockPlaceHolder = /(?:<|&lt;)!--code\uFFFC(\d+)--(?:>|&gt;)/g;

const _escapeContent = (cache, flag, str) => `<!--${flag}\uFFFC${cache.push(str) - 1}-->`;

const _restoreContent = cache => (_, index) => {
assert(cache[index]);
const value = cache[index];
cache[index] = null;
return value;
};

const STATE_PLAINTEXT = Symbol('plaintext');
const STATE_SWIG_VAR = Symbol('swig_var');
const STATE_SWIG_COMMENT = Symbol('swig_comment');
Expand All @@ -43,17 +34,30 @@ class PostRenderEscape {
this.stored = [];
}

static escapeContent(cache, flag, str) {
return `<!--${flag}\uFFFC${cache.push(str) - 1}-->`;
}

static restoreContent(cache) {
return (_, index) => {
assert(cache[index]);
const value = cache[index];
cache[index] = null;
return value;
};
}

restoreAllSwigTags(str) {
const restored = str.replace(rSwigPlaceHolder, _restoreContent(this.stored));
const restored = str.replace(rSwigPlaceHolder, PostRenderEscape.restoreContent(this.stored));
return restored;
}

restoreCodeBlocks(str) {
return str.replace(rCodeBlockPlaceHolder, _restoreContent(this.stored));
return str.replace(rCodeBlockPlaceHolder, PostRenderEscape.restoreContent(this.stored));
}

escapeCodeBlocks(str) {
return str.replace(rHexoPostRenderEscape, (_, content) => _escapeContent(this.stored, 'code', content));
return str.replace(rHexoPostRenderEscape, (_, content) => PostRenderEscape.escapeContent(this.stored, 'code', content));
}

/**
Expand Down Expand Up @@ -101,7 +105,7 @@ class PostRenderEscape {
} else {
swig_tag_name = '';
state = STATE_PLAINTEXT;
output += _escapeContent(this.stored, 'swig', `{%${buffer}%}`);
output += PostRenderEscape.escapeContent(this.stored, 'swig', `{%${buffer}%}`);
}

buffer = '';
Expand All @@ -127,7 +131,7 @@ class PostRenderEscape {
if (char === '}' && next_char === '}') {
idx++;
state = STATE_PLAINTEXT;
output += _escapeContent(this.stored, 'swig', `{{${buffer}}}`);
output += PostRenderEscape.escapeContent(this.stored, 'swig', `{{${buffer}}}`);
buffer = '';
} else {
buffer = buffer + char;
Expand Down Expand Up @@ -157,7 +161,7 @@ class PostRenderEscape {

if (swig_full_tag_end_buffer.includes(`end${swig_tag_name}`)) {
state = STATE_PLAINTEXT;
output += _escapeContent(this.stored, 'swig', `{%${swig_full_tag_start_buffer}%}${buffer}{%${swig_full_tag_end_buffer}%}`);
output += PostRenderEscape.escapeContent(this.stored, 'swig', `{%${swig_full_tag_start_buffer}%}${buffer}{%${swig_full_tag_end_buffer}%}`);
idx = _idx;
swig_full_tag_start_buffer = '';
swig_full_tag_end_buffer = '';
Expand Down Expand Up @@ -241,7 +245,8 @@ class Post {
createAssetFolder(path, config.post_asset_folder)
]).then(() => {
ctx.emit('new', result);
}).thenReturn(result);
return result;
});
}).asCallback(callback);
}

Expand Down Expand Up @@ -271,12 +276,11 @@ class Post {
// Parse front-matter
const obj = jsonMode ? JSON.parse(`{${frontMatter}}`) : load(frontMatter);

// Add data which are not in the front-matter
for (const key of Object.keys(data)) {
if (!preservedKeys.includes(key) && obj[key] == null) {
Object.keys(data)
.filter(key => !preservedKeys.includes(key) && obj[key] == null)
.forEach(key => {
obj[key] = data[key];
}
}
});

let content = '';
// Prepend the separator
Expand Down
18 changes: 12 additions & 6 deletions lib/hexo/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,19 @@ class Render {
const ctx = this.context;
let ext = '';

return new Promise((resolve, reject) => {
if (!data) return reject(new TypeError('No input file or string!'));
if (data.text != null) return resolve(data.text);
if (!data.path) return reject(new TypeError('No input file or string!'));
let promise;

readFile(data.path).then(resolve, reject);
}).then(text => {
if (!data) return Promise.reject(new TypeError('No input file or string!'));

if (data.text != null) {
promise = Promise.resolve(data.text);
} else if (!data.path) {
return Promise.reject(new TypeError('No input file or string!'));
} else {
promise = readFile(data.path);
}

return promise.then(text => {
data.text = text;
ext = data.engine || getExtname(data.path);
if (!ext || !this.isRenderable(ext)) return text;
Expand Down