From 54251601d8852661625ef56000d3b3bfcc6f23ac Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 10 Jan 2020 15:58:00 +0800 Subject: [PATCH 1/9] feat(helper): add prop, setProp() & getProp() --- lib/extend/helper.js | 12 ++++++++++ test/scripts/extend/helper.js | 42 ++++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/lib/extend/helper.js b/lib/extend/helper.js index 420ce89229..9c130b8b1a 100644 --- a/lib/extend/helper.js +++ b/lib/extend/helper.js @@ -3,6 +3,7 @@ class Helper { constructor() { this.store = {}; + this.prop = {}; } list() { @@ -19,6 +20,17 @@ class Helper { this.store[name] = fn; } + + setProp(name, prop) { + if (!name) throw new TypeError('name is required'); + if (!prop) throw new TypeError('prop is required'); + + this.prop[name] = prop; + } + + getProp(name) { + return this.prop[name]; + } } module.exports = Helper; diff --git a/test/scripts/extend/helper.js b/test/scripts/extend/helper.js index bf96500c89..35a2b72019 100644 --- a/test/scripts/extend/helper.js +++ b/test/scripts/extend/helper.js @@ -2,10 +2,13 @@ describe('Helper', () => { const Helper = require('../../../lib/extend/helper'); + let h; - it('register()', () => { - const h = new Helper(); + beforeEach(() => { + h = new Helper(); + }); + it('register()', () => { // name, fn h.register('test', () => {}); @@ -31,18 +34,45 @@ describe('Helper', () => { }); it('list()', () => { - const h = new Helper(); - h.register('test', () => {}); h.list().should.have.keys(['test']); }); it('get()', () => { - const h = new Helper(); - h.register('test', () => {}); h.get('test').should.exist; }); + + it('setProp()', () => { + // name prop + h.setProp('foo', 'bar'); + + h.prop.foo.should.eql('bar'); + + // no name + try { + h.setProp(); + } catch (err) { + err.should.be + .instanceOf(TypeError) + .property('message', 'name is required'); + } + + // no prop + try { + h.setProp('test'); + } catch (err) { + err.should.be + .instanceOf(TypeError) + .property('message', 'prop is required'); + } + }); + + it('getProp()', () => { + h.setProp('foo', 'bar'); + + h.getProp('foo').should.eql('bar'); + }); }); From dbb0ba243b1880abb770f1611150170992865bf1 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 10 Jan 2020 16:26:52 +0800 Subject: [PATCH 2/9] feat(meta_generator_helper): utilize prop --- lib/plugins/helper/index.js | 2 +- lib/plugins/helper/meta_generator.js | 11 ++++++----- test/scripts/helpers/meta_generator.js | 8 +++++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/plugins/helper/index.js b/lib/plugins/helper/index.js index 90c40d428a..1957d782ea 100644 --- a/lib/plugins/helper/index.js +++ b/lib/plugins/helper/index.js @@ -43,7 +43,7 @@ module.exports = ctx => { helper.register('list_tags', require('./list_tags')); helper.register('list_posts', require('./list_posts')); - helper.register('meta_generator', require('./meta_generator')); + helper.register('meta_generator', require('./meta_generator')(ctx)); helper.register('open_graph', require('./open_graph')); diff --git a/lib/plugins/helper/meta_generator.js b/lib/plugins/helper/meta_generator.js index 20b7613939..03767b9e74 100755 --- a/lib/plugins/helper/meta_generator.js +++ b/lib/plugins/helper/meta_generator.js @@ -1,7 +1,8 @@ 'use strict'; -function metaGeneratorHelper() { - return ``; -} - -module.exports = metaGeneratorHelper; +module.exports = ctx => { + return () => { + if (!ctx.extend.helper.getProp('meta_generator')) ctx.extend.helper.setProp('meta_generator', true); + return ``; + }; +}; diff --git a/test/scripts/helpers/meta_generator.js b/test/scripts/helpers/meta_generator.js index 98d7b75175..6c4e525a10 100755 --- a/test/scripts/helpers/meta_generator.js +++ b/test/scripts/helpers/meta_generator.js @@ -4,13 +4,19 @@ describe('meta_generator', () => { const Hexo = require('../../../lib/hexo'); const hexo = new Hexo(); - const metaGeneratorHelper = require('../../../lib/plugins/helper/meta_generator').bind(hexo); + const metaGeneratorHelper = require('../../../lib/plugins/helper/meta_generator')(hexo); + + before(() => hexo.init()); it('default', () => { const { version } = hexo; const versionType = typeof version; + should.not.exist(hexo.extend.helper.getProp('meta_generator')); + versionType.should.not.eql('undefined'); metaGeneratorHelper().should.eql(``); + + hexo.extend.helper.getProp('meta_generator').should.eql(true); }); }); From e19dc4327cf745e39dbcca79a6569d73ed1ff2a3 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 10 Jan 2020 16:38:20 +0800 Subject: [PATCH 3/9] feat(meta_generator): no inject if helper is used --- lib/plugins/filter/after_render/meta_generator.js | 8 ++++---- test/scripts/filters/meta_generator.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/plugins/filter/after_render/meta_generator.js b/lib/plugins/filter/after_render/meta_generator.js index 516cdbba59..98ae11f155 100644 --- a/lib/plugins/filter/after_render/meta_generator.js +++ b/lib/plugins/filter/after_render/meta_generator.js @@ -1,13 +1,13 @@ 'use strict'; function hexoMetaGeneratorInject(data) { - const { config } = this; + const { config, version } = this; + if (!config.meta_generator + || this.extend.helper.getProp('meta_generator') || data.match(/]+[\s]+)name=['|"]?generator['|"]?/i)) return; - const hexoGeneratorTag = ``; - - return data.replace(/(?!<\/head>).+?<\/head>/s, str => str.replace('', `${hexoGeneratorTag}`)); + return data.replace(/(?!<\/head>).+?<\/head>/s, str => str.replace('', ``)); } module.exports = hexoMetaGeneratorInject; diff --git a/test/scripts/filters/meta_generator.js b/test/scripts/filters/meta_generator.js index 6703bf0f55..ce4df92eb6 100644 --- a/test/scripts/filters/meta_generator.js +++ b/test/scripts/filters/meta_generator.js @@ -4,6 +4,7 @@ describe('Meta Generator', () => { const Hexo = require('../../../lib/hexo'); const hexo = new Hexo(); const metaGenerator = require('../../../lib/plugins/filter/after_render/meta_generator').bind(hexo); + const metaGeneratorHelper = require('../../../lib/plugins/helper/meta_generator')(hexo); const cheerio = require('cheerio'); it('default', () => { @@ -71,4 +72,13 @@ describe('Meta Generator', () => { result.should.eql(expected); }); + + it('no inject if meta_generator() helper is used', () => { + const content = ''; + hexo.config.meta_generator = true; + metaGeneratorHelper(); + const result = metaGenerator(content); + + should.not.exist(result); + }); }); From 88e1c3abe33b4e5301fce7b74859d497eecd505f Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 10 Jan 2020 17:15:04 +0800 Subject: [PATCH 4/9] feat(meta_generator-filter): cache if inject is needed --- lib/plugins/filter/after_render/meta_generator.js | 15 ++++++++++++--- package.json | 1 + test/scripts/filters/meta_generator.js | 9 ++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/plugins/filter/after_render/meta_generator.js b/lib/plugins/filter/after_render/meta_generator.js index 98ae11f155..7a1d276736 100644 --- a/lib/plugins/filter/after_render/meta_generator.js +++ b/lib/plugins/filter/after_render/meta_generator.js @@ -1,11 +1,20 @@ 'use strict'; +const { Cache } = require('hexo-util'); +const cache = new Cache(); + function hexoMetaGeneratorInject(data) { const { config, version } = this; - if (!config.meta_generator - || this.extend.helper.getProp('meta_generator') - || data.match(/]+[\s]+)name=['|"]?generator['|"]?/i)) return; + const needInject = cache.apply('need-inject', () => { + if (!config.meta_generator + || this.extend.helper.getProp('meta_generator') + || data.match(/]+[\s]+)name=['|"]?generator['|"]?/i)) return false; + + return true; + }); + + if (!needInject) return; return data.replace(/(?!<\/head>).+?<\/head>/s, str => str.replace('', ``)); } diff --git a/package.json b/package.json index 94b6ce5780..ff3eb91029 100644 --- a/package.json +++ b/package.json @@ -71,6 +71,7 @@ "chai": "^4.1.2", "chai-as-promised": "^7.1.1", "cheerio": "0.22.0", + "decache": "^4.5.1", "eslint": "^6.0.1", "eslint-config-hexo": "^4.1.0", "hexo-renderer-marked": "^2.0.0", diff --git a/test/scripts/filters/meta_generator.js b/test/scripts/filters/meta_generator.js index ce4df92eb6..f849a654ca 100644 --- a/test/scripts/filters/meta_generator.js +++ b/test/scripts/filters/meta_generator.js @@ -1,12 +1,19 @@ 'use strict'; +const decache = require('decache'); + describe('Meta Generator', () => { const Hexo = require('../../../lib/hexo'); const hexo = new Hexo(); - const metaGenerator = require('../../../lib/plugins/filter/after_render/meta_generator').bind(hexo); + let metaGenerator; const metaGeneratorHelper = require('../../../lib/plugins/helper/meta_generator')(hexo); const cheerio = require('cheerio'); + beforeEach(async () => { + await decache('../../../lib/plugins/filter/after_render/meta_generator'); + metaGenerator = require('../../../lib/plugins/filter/after_render/meta_generator').bind(hexo); + }); + it('default', () => { const content = ''; const result = metaGenerator(content); From 1f12bf0755332eba5826dad2d200301225726c36 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 12 Jan 2020 10:55:25 +0800 Subject: [PATCH 5/9] test(helper): add should.fail() to try block --- test/scripts/extend/helper.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/scripts/extend/helper.js b/test/scripts/extend/helper.js index 35a2b72019..ce57145876 100644 --- a/test/scripts/extend/helper.js +++ b/test/scripts/extend/helper.js @@ -17,6 +17,7 @@ describe('Helper', () => { // no fn try { h.register('test'); + should.fail(); } catch (err) { err.should.be .instanceOf(TypeError) @@ -26,6 +27,7 @@ describe('Helper', () => { // no name try { h.register(); + should.fail(); } catch (err) { err.should.be .instanceOf(TypeError) @@ -54,6 +56,7 @@ describe('Helper', () => { // no name try { h.setProp(); + should.fail(); } catch (err) { err.should.be .instanceOf(TypeError) @@ -63,6 +66,7 @@ describe('Helper', () => { // no prop try { h.setProp('test'); + should.fail(); } catch (err) { err.should.be .instanceOf(TypeError) From 42d9667b9ee91f90e86b293f973a1408dd739793 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sun, 12 Jan 2020 11:02:17 +0800 Subject: [PATCH 6/9] refactor(meta_generator): use ctx.version --- lib/plugins/helper/meta_generator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/helper/meta_generator.js b/lib/plugins/helper/meta_generator.js index 03767b9e74..ff48459cb2 100755 --- a/lib/plugins/helper/meta_generator.js +++ b/lib/plugins/helper/meta_generator.js @@ -3,6 +3,6 @@ module.exports = ctx => { return () => { if (!ctx.extend.helper.getProp('meta_generator')) ctx.extend.helper.setProp('meta_generator', true); - return ``; + return ``; }; }; From d4bd4909d33d38f722c0efbb91782d5039dca4ae Mon Sep 17 00:00:00 2001 From: Sukka Date: Tue, 14 Jan 2020 08:14:12 +0800 Subject: [PATCH 7/9] test(meta_generaror): remove useless async/await --- test/scripts/filters/meta_generator.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/scripts/filters/meta_generator.js b/test/scripts/filters/meta_generator.js index f849a654ca..1db0cdef32 100644 --- a/test/scripts/filters/meta_generator.js +++ b/test/scripts/filters/meta_generator.js @@ -9,8 +9,8 @@ describe('Meta Generator', () => { const metaGeneratorHelper = require('../../../lib/plugins/helper/meta_generator')(hexo); const cheerio = require('cheerio'); - beforeEach(async () => { - await decache('../../../lib/plugins/filter/after_render/meta_generator'); + beforeEach(() => { + decache('../../../lib/plugins/filter/after_render/meta_generator'); metaGenerator = require('../../../lib/plugins/filter/after_render/meta_generator').bind(hexo); }); From b050139ebe8d615c7be5bba57dc5d2b2409a380e Mon Sep 17 00:00:00 2001 From: SukkaW Date: Wed, 15 Jan 2020 15:27:59 +0800 Subject: [PATCH 8/9] perf(meta_generator): cache tag string --- lib/plugins/filter/after_render/meta_generator.js | 6 ++++-- lib/plugins/helper/meta_generator.js | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/plugins/filter/after_render/meta_generator.js b/lib/plugins/filter/after_render/meta_generator.js index 7a1d276736..f4e40989d4 100644 --- a/lib/plugins/filter/after_render/meta_generator.js +++ b/lib/plugins/filter/after_render/meta_generator.js @@ -2,9 +2,10 @@ const { Cache } = require('hexo-util'); const cache = new Cache(); +let metaGeneratorTag = ''; function hexoMetaGeneratorInject(data) { - const { config, version } = this; + const { config } = this; const needInject = cache.apply('need-inject', () => { if (!config.meta_generator @@ -15,8 +16,9 @@ function hexoMetaGeneratorInject(data) { }); if (!needInject) return; + metaGeneratorTag = metaGeneratorTag || ``; - return data.replace(/(?!<\/head>).+?<\/head>/s, str => str.replace('', ``)); + return data.replace(/(?!<\/head>).+?<\/head>/s, str => str.replace('', metaGeneratorTag + '')); } module.exports = hexoMetaGeneratorInject; diff --git a/lib/plugins/helper/meta_generator.js b/lib/plugins/helper/meta_generator.js index ff48459cb2..ed02158134 100755 --- a/lib/plugins/helper/meta_generator.js +++ b/lib/plugins/helper/meta_generator.js @@ -1,8 +1,11 @@ 'use strict'; +let metaGeneratorTag = ''; + module.exports = ctx => { return () => { if (!ctx.extend.helper.getProp('meta_generator')) ctx.extend.helper.setProp('meta_generator', true); - return ``; + metaGeneratorTag = metaGeneratorTag || ``; + return metaGeneratorTag; }; }; From 7eede38e0ef75ba034987bc932d3e99b915b0e5f Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 18 Jan 2020 20:20:43 +0800 Subject: [PATCH 9/9] refactor(meta_generator): change prop name --- lib/plugins/filter/after_render/meta_generator.js | 2 +- lib/plugins/helper/meta_generator.js | 2 +- test/scripts/helpers/meta_generator.js | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/plugins/filter/after_render/meta_generator.js b/lib/plugins/filter/after_render/meta_generator.js index f4e40989d4..1ff000cea1 100644 --- a/lib/plugins/filter/after_render/meta_generator.js +++ b/lib/plugins/filter/after_render/meta_generator.js @@ -9,7 +9,7 @@ function hexoMetaGeneratorInject(data) { const needInject = cache.apply('need-inject', () => { if (!config.meta_generator - || this.extend.helper.getProp('meta_generator') + || this.extend.helper.getProp('meta_generator_used') || data.match(/]+[\s]+)name=['|"]?generator['|"]?/i)) return false; return true; diff --git a/lib/plugins/helper/meta_generator.js b/lib/plugins/helper/meta_generator.js index ed02158134..757b19689b 100755 --- a/lib/plugins/helper/meta_generator.js +++ b/lib/plugins/helper/meta_generator.js @@ -4,7 +4,7 @@ let metaGeneratorTag = ''; module.exports = ctx => { return () => { - if (!ctx.extend.helper.getProp('meta_generator')) ctx.extend.helper.setProp('meta_generator', true); + if (!ctx.extend.helper.getProp('meta_generator_used')) ctx.extend.helper.setProp('meta_generator_used', true); metaGeneratorTag = metaGeneratorTag || ``; return metaGeneratorTag; }; diff --git a/test/scripts/helpers/meta_generator.js b/test/scripts/helpers/meta_generator.js index 6c4e525a10..839ebf08ff 100755 --- a/test/scripts/helpers/meta_generator.js +++ b/test/scripts/helpers/meta_generator.js @@ -12,11 +12,11 @@ describe('meta_generator', () => { const { version } = hexo; const versionType = typeof version; - should.not.exist(hexo.extend.helper.getProp('meta_generator')); + should.not.exist(hexo.extend.helper.getProp('meta_generator_used')); versionType.should.not.eql('undefined'); metaGeneratorHelper().should.eql(``); - hexo.extend.helper.getProp('meta_generator').should.eql(true); + hexo.extend.helper.getProp('meta_generator_used').should.eql(true); }); });