Skip to content

Commit

Permalink
perf: minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Jul 11, 2020
1 parent f971ec9 commit 034632b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
8 changes: 4 additions & 4 deletions lib/plugins/filter/before_generate/render_post.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ function renderPostFilter(data) {
const renderPosts = model => {
const posts = model.toArray().filter(post => post.content == null);

return Promise.map(posts, post => {
return Promise.all(posts.map(post => {
return Promise.resolve(post._content).then(_content => {
return pool.run({ input: _content, highlightCfg: this.config.highlight, prismjsCfg: this.config.prismjs });
}).then(content => {
post.content = content;
post.site = { data };

return this.post.render(post.full_source, post).then(() => post.save());
});
});
return this.post.render(post.full_source, post);
}).then(() => post.save());
}));
};

return Promise.all([
Expand Down
10 changes: 4 additions & 6 deletions lib/workers/backtick_codeblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ function backtickCodeBlock(input, hljsCfg = {}, prismCfg = {}) {
if (!input.includes('```') && !input.includes('~~~')) return input;

return input.replace(rBacktick, ($0, start, $2, _args, _content, end) => {

let content = _content.replace(/\n$/, '');

// neither highlight or prismjs is enabled, return escaped content directly.
if (!hljsCfg.enable && !prismCfg.enable) return escapeSwigTag($0);

// Extract language and caption of code blocks
let content = _content.replace(/\n$/, '');

// Extrace langauge and caption of code blocks
const args = _args.split('=').shift();
let lang, caption;

Expand All @@ -42,8 +41,7 @@ function backtickCodeBlock(input, hljsCfg = {}, prismCfg = {}) {
if (start.includes('>')) {
// heading of last line is already removed by the top RegExp "rBacktick"
const depth = start.split('>').length - 1;
const regexp = new RegExp(`^([^\\S\\r\\n]*>){0,${depth}}([^\\S\\r\\n]|$)`, 'mg');
content = content.replace(regexp, '');
content = content.replace(new RegExp(`^([^\\S\\r\\n]*>){0,${depth}}([^\\S\\r\\n]|$)`, 'mg'), '');
}

// Since prismjs have better performance, so prismjs should have higher priority.
Expand Down
4 changes: 1 addition & 3 deletions lib/workers/backtick_codeblock_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,5 @@ const { isMainThread, parentPort } = require('worker_threads');
if (isMainThread) throw new Error('It is not a worker, it is now at Main Thread.');

parentPort.on('message', ({ input, highlightCfg, prismjsCfg }) => {
const result = backtickCodeBlock(input, highlightCfg, prismjsCfg);

parentPort.postMessage(result);
parentPort.postMessage(backtickCodeBlock(input, highlightCfg, prismjsCfg));
});

0 comments on commit 034632b

Please sign in to comment.