From 034632b09910f413cd07e6f6f6b83c90a76ab1eb Mon Sep 17 00:00:00 2001 From: SukkaW Date: Mon, 6 Jul 2020 17:57:28 +0800 Subject: [PATCH] perf: minor optimizations --- lib/plugins/filter/before_generate/render_post.js | 8 ++++---- lib/workers/backtick_codeblock.js | 10 ++++------ lib/workers/backtick_codeblock_worker.js | 4 +--- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/plugins/filter/before_generate/render_post.js b/lib/plugins/filter/before_generate/render_post.js index 637a6820a6..2a6662265f 100644 --- a/lib/plugins/filter/before_generate/render_post.js +++ b/lib/plugins/filter/before_generate/render_post.js @@ -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([ diff --git a/lib/workers/backtick_codeblock.js b/lib/workers/backtick_codeblock.js index 986899c1ca..0e6713915d 100644 --- a/lib/workers/backtick_codeblock.js +++ b/lib/workers/backtick_codeblock.js @@ -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; @@ -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. diff --git a/lib/workers/backtick_codeblock_worker.js b/lib/workers/backtick_codeblock_worker.js index 5d71a64799..4ad85fc8fa 100644 --- a/lib/workers/backtick_codeblock_worker.js +++ b/lib/workers/backtick_codeblock_worker.js @@ -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)); });