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/lib/plugins/filter/after_render/meta_generator.js b/lib/plugins/filter/after_render/meta_generator.js index 516cdbba59..1ff000cea1 100644 --- a/lib/plugins/filter/after_render/meta_generator.js +++ b/lib/plugins/filter/after_render/meta_generator.js @@ -1,13 +1,24 @@ 'use strict'; +const { Cache } = require('hexo-util'); +const cache = new Cache(); +let metaGeneratorTag = ''; + function hexoMetaGeneratorInject(data) { const { config } = this; - if (!config.meta_generator - || data.match(/]+[\s]+)name=['|"]?generator['|"]?/i)) return; - const hexoGeneratorTag = ``; + const needInject = cache.apply('need-inject', () => { + if (!config.meta_generator + || this.extend.helper.getProp('meta_generator_used') + || data.match(/]+[\s]+)name=['|"]?generator['|"]?/i)) return false; + + return true; + }); + + if (!needInject) return; + metaGeneratorTag = metaGeneratorTag || ``; - return data.replace(/
(?!<\/head>).+?<\/head>/s, str => str.replace('', `${hexoGeneratorTag}`)); + return data.replace(/(?!<\/head>).+?<\/head>/s, str => str.replace('', metaGeneratorTag + '')); } module.exports = hexoMetaGeneratorInject; 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..757b19689b 100755 --- a/lib/plugins/helper/meta_generator.js +++ b/lib/plugins/helper/meta_generator.js @@ -1,7 +1,11 @@ 'use strict'; -function metaGeneratorHelper() { - return ``; -} +let metaGeneratorTag = ''; -module.exports = metaGeneratorHelper; +module.exports = ctx => { + return () => { + if (!ctx.extend.helper.getProp('meta_generator_used')) ctx.extend.helper.setProp('meta_generator_used', true); + metaGeneratorTag = metaGeneratorTag || ``; + return metaGeneratorTag; + }; +}; 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/extend/helper.js b/test/scripts/extend/helper.js index bf96500c89..ce57145876 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', () => {}); @@ -14,6 +17,7 @@ describe('Helper', () => { // no fn try { h.register('test'); + should.fail(); } catch (err) { err.should.be .instanceOf(TypeError) @@ -23,6 +27,7 @@ describe('Helper', () => { // no name try { h.register(); + should.fail(); } catch (err) { err.should.be .instanceOf(TypeError) @@ -31,18 +36,47 @@ 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(); + should.fail(); + } catch (err) { + err.should.be + .instanceOf(TypeError) + .property('message', 'name is required'); + } + + // no prop + try { + h.setProp('test'); + should.fail(); + } catch (err) { + err.should.be + .instanceOf(TypeError) + .property('message', 'prop is required'); + } + }); + + it('getProp()', () => { + h.setProp('foo', 'bar'); + + h.getProp('foo').should.eql('bar'); + }); }); diff --git a/test/scripts/filters/meta_generator.js b/test/scripts/filters/meta_generator.js index 6703bf0f55..1db0cdef32 100644 --- a/test/scripts/filters/meta_generator.js +++ b/test/scripts/filters/meta_generator.js @@ -1,11 +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(() => { + 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); @@ -71,4 +79,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); + }); }); diff --git a/test/scripts/helpers/meta_generator.js b/test/scripts/helpers/meta_generator.js index 98d7b75175..839ebf08ff 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_used')); + versionType.should.not.eql('undefined'); metaGeneratorHelper().should.eql(``); + + hexo.extend.helper.getProp('meta_generator_used').should.eql(true); }); });