diff --git a/lib/plugins/filter/meta_generator.js b/lib/plugins/filter/meta_generator.js
index 1a203bba54..8440b9b0fe 100644
--- a/lib/plugins/filter/meta_generator.js
+++ b/lib/plugins/filter/meta_generator.js
@@ -7,7 +7,7 @@ function hexoMetaGeneratorInject(data) {
const hexoGeneratorTag = ``;
- return data.replace('', '' + hexoGeneratorTag);
+ return data.replace(/
(?!<\/head>).+?<\/head>/, (str) => str.replace('', `${hexoGeneratorTag}`));
}
module.exports = hexoMetaGeneratorInject;
diff --git a/test/scripts/filters/meta_generator.js b/test/scripts/filters/meta_generator.js
index 94d8caa071..03814a4e63 100644
--- a/test/scripts/filters/meta_generator.js
+++ b/test/scripts/filters/meta_generator.js
@@ -7,15 +7,16 @@ describe('Meta Generator', () => {
const cheerio = require('cheerio');
it('default', () => {
- const content = 'foo';
+ const content = '';
const result = metaGenerator(content);
const $ = cheerio.load(result);
$('meta[name="generator"]').length.should.eql(1);
+ $('meta[name="generator"]').attr('content').should.eql(`Hexo ${hexo.version}`);
});
it('disable meta_generator', () => {
- const content = 'foo';
+ const content = '';
hexo.config.meta_generator = false;
const result = metaGenerator(content);
@@ -24,7 +25,7 @@ describe('Meta Generator', () => {
});
it('no duplicate generator tag', () => {
- const content = 'foo'
+ const content = ''
+ '';
hexo.config.meta_generator = true;
const result = metaGenerator(content);
@@ -32,4 +33,36 @@ describe('Meta Generator', () => {
const resultType = typeof result;
resultType.should.eql('undefined');
});
+
+ it('ignore empty head tag', () => {
+ const content = ''
+ + ''
+ + '';
+ hexo.config.meta_generator = true;
+ const result = metaGenerator(content);
+
+ const $ = cheerio.load(result);
+ $('meta[name="generator"]').length.should.eql(1);
+
+ const expected = ''
+ + ''
+ + '';
+ result.should.eql(expected);
+ });
+
+ it('apply to first non-empty head tag only', () => {
+ const content = ''
+ + ''
+ + '';
+ hexo.config.meta_generator = true;
+ const result = metaGenerator(content);
+
+ const $ = cheerio.load(result);
+ $('meta[name="generator"]').length.should.eql(1);
+
+ const expected = ''
+ + ''
+ + '';
+ result.should.eql(expected);
+ });
});