diff --git a/lib/plugins/filter/meta_generator.js b/lib/plugins/filter/meta_generator.js index 1e5ee3bb33..541eab5e8b 100644 --- a/lib/plugins/filter/meta_generator.js +++ b/lib/plugins/filter/meta_generator.js @@ -7,7 +7,7 @@ function hexoMetaGeneratorInject(data) { if (!cheerio) cheerio = require('cheerio'); const $ = cheerio.load(data, {decodeEntities: false}); - if (!($('meta[name="generator"]').length > 0)) { + if (!($('meta[name="generator"]').length > 0) && $('head').contents().length > 0) { $('head').prepend(hexoGeneratorTag.replace('%s', this.version)); return $.html(); diff --git a/test/scripts/filters/index.js b/test/scripts/filters/index.js index a6433bd6a5..d899acf7b1 100644 --- a/test/scripts/filters/index.js +++ b/test/scripts/filters/index.js @@ -5,6 +5,7 @@ describe('Filters', () => { require('./excerpt'); require('./external_link'); require('./i18n_locals'); + require('./meta_generator'); require('./new_post_path'); require('./post_permalink'); require('./render_post'); diff --git a/test/scripts/filters/meta_generator.js b/test/scripts/filters/meta_generator.js new file mode 100644 index 0000000000..2ae4745c26 --- /dev/null +++ b/test/scripts/filters/meta_generator.js @@ -0,0 +1,26 @@ +'use strict'; + +describe('Meta Generator', () => { + const Hexo = require('../../../lib/hexo'); + const hexo = new Hexo(); + const metaGenerator = require('../../../lib/plugins/filter/meta_generator').bind(hexo); + const cheerio = require('cheerio'); + + it('default', () => { + const content = ''; + const result = metaGenerator(content); + + const $ = cheerio.load(result); + $('meta[name="generator"]').length.should.eql(1); + }); + + it('empty ', () => { + const content = ''; + const result = metaGenerator(content); + + // meta generator should not be prepended if tag is empty + // see https://github.com/hexojs/hexo/pull/3315 + const resultType = typeof result; + resultType.should.eql('undefined'); + }); +});