diff --git a/lib/plugins/filter/before_generate/render_post.js b/lib/plugins/filter/before_generate/render_post.js index 100450004a..78477f85d2 100644 --- a/lib/plugins/filter/before_generate/render_post.js +++ b/lib/plugins/filter/before_generate/render_post.js @@ -1,23 +1,34 @@ 'use strict'; const Promise = require('bluebird'); +const { WorkerPool } = require('hexo-util'); +const { join, dirname } = require('path'); +let pool; function renderPostFilter(data) { + pool = new WorkerPool(join(dirname(dirname(dirname(__dirname))), 'workers', 'backtick_codeblock_worker.js')); + const renderPosts = model => { const posts = model.toArray().filter(post => post.content == null); return Promise.map(posts, post => { - post.content = post._content; - post.site = {data}; + return Promise.resolve(post._content).then(_content => { + return pool.run({ input: _content, siteCfg: this.config }); + }).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([ renderPosts(this.model('Post')), renderPosts(this.model('Page')) - ]); + ]).then(() => { + pool.destroy(); + }); } module.exports = renderPostFilter; diff --git a/lib/plugins/filter/before_post_render/index.js b/lib/plugins/filter/before_post_render/index.js index 934df0ad73..6f64a53a26 100644 --- a/lib/plugins/filter/before_post_render/index.js +++ b/lib/plugins/filter/before_post_render/index.js @@ -3,6 +3,6 @@ module.exports = ctx => { const { filter } = ctx.extend; - filter.register('before_post_render', require('./backtick_code_block')); + // filter.register('before_post_render', require('./backtick_code_block')); filter.register('before_post_render', require('./titlecase')); }; diff --git a/lib/plugins/filter/before_post_render/backtick_code_block.js b/lib/workers/backtick_codeblock.js similarity index 89% rename from lib/plugins/filter/before_post_render/backtick_code_block.js rename to lib/workers/backtick_codeblock.js index b8418db611..d8e68a4c90 100644 --- a/lib/plugins/filter/before_post_render/backtick_code_block.js +++ b/lib/workers/backtick_codeblock.js @@ -8,15 +8,14 @@ const rLangCaption = /([^\s]+)\s*(.+)?/; const escapeSwigTag = str => str.replace(/{/g, '{').replace(/}/g, '}'); -function backtickCodeBlock(data) { - const dataContent = data.content; +function backtickCodeBlock({ input, siteCfg }) { + if (!input.includes('```') && !input.includes('~~~')) return input; - if (!dataContent.includes('```') && !dataContent.includes('~~~')) return; + const hljsCfg = siteCfg.highlight || {}; + const prismCfg = siteCfg.prismjs || {}; - const hljsCfg = this.config.highlight || {}; - const prismCfg = this.config.prismjs || {}; + return input.replace(rBacktick, ($0, start, $2, _args, _content, end) => { - data.content = dataContent.replace(rBacktick, ($0, start, $2, _args, _content, end) => { let content = _content.replace(/\n$/, ''); // neither highlight or prismjs is enabled, return escaped content directly. diff --git a/lib/workers/backtick_codeblock_worker.js b/lib/workers/backtick_codeblock_worker.js new file mode 100644 index 0000000000..006393fd3b --- /dev/null +++ b/lib/workers/backtick_codeblock_worker.js @@ -0,0 +1,14 @@ +'use strict'; + +const backtickCodeBlock = require('./backtick_codeblock'); + +// eslint-disable-next-line node/no-unsupported-features/node-builtins +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, siteCfg }) => { + const result = backtickCodeBlock(input, siteCfg); + + parentPort.postMessage(result); +}); diff --git a/package.json b/package.json index 7c28089f41..9ac517c6a9 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "hexo-i18n": "^1.0.0", "hexo-log": "^1.0.0", "hexo-renderer-nunjucks": "^2.0.0", - "hexo-util": "^2.0.0", + "hexo-util": "github:sukkaw/hexo-util#worker-pool", "js-yaml": "^3.12.0", "micromatch": "^4.0.2", "moment": "^2.22.2", diff --git a/test/scripts/filters/index.js b/test/scripts/filters/index.js index d899acf7b1..3f0198cbfa 100644 --- a/test/scripts/filters/index.js +++ b/test/scripts/filters/index.js @@ -1,7 +1,7 @@ 'use strict'; describe('Filters', () => { - require('./backtick_code_block'); + // require('./backtick_code_block'); require('./excerpt'); require('./external_link'); require('./i18n_locals');