Skip to content

Commit

Permalink
Merge ce86ca9 into eca3cf2
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Sep 1, 2019
2 parents eca3cf2 + ce86ca9 commit 9a54447
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/plugins/filter/meta_generator.js
Expand Up @@ -7,7 +7,7 @@ function hexoMetaGeneratorInject(data) {

const hexoGeneratorTag = `<meta name="generator" content="Hexo ${this.version}">`;

return data.replace('</title>', '</title>' + hexoGeneratorTag);
return data.replace(/<head>(?!<\/head>).+?<\/head>/, (str) => str.replace('</head>', `${hexoGeneratorTag}</head>`));
}

module.exports = hexoMetaGeneratorInject;
39 changes: 36 additions & 3 deletions test/scripts/filters/meta_generator.js
Expand Up @@ -7,15 +7,16 @@ describe('Meta Generator', () => {
const cheerio = require('cheerio');

it('default', () => {
const content = '<head><title>foo</title></head>';
const content = '<head><link></head>';
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 = '<head><title>foo</title></head>';
const content = '<head><link></head>';
hexo.config.meta_generator = false;
const result = metaGenerator(content);

Expand All @@ -24,12 +25,44 @@ describe('Meta Generator', () => {
});

it('no duplicate generator tag', () => {
const content = '<head><title>foo</title>'
const content = '<head><link>'
+ '<meta name="generator" content="foo"></head>';
hexo.config.meta_generator = true;
const result = metaGenerator(content);

const resultType = typeof result;
resultType.should.eql('undefined');
});

it('ignore empty head tag', () => {
const content = '<head></head>'
+ '<head><link></head>'
+ '<head></head>';
hexo.config.meta_generator = true;
const result = metaGenerator(content);

const $ = cheerio.load(result);
$('meta[name="generator"]').length.should.eql(1);

const expected = '<head></head>'
+ '<head><link><meta name="generator" content="Hexo ' + hexo.version + '"></head>'
+ '<head></head>';
result.should.eql(expected);
});

it('apply to first non-empty head tag only', () => {
const content = '<head></head>'
+ '<head><link></head>'
+ '<head><link></head>';
hexo.config.meta_generator = true;
const result = metaGenerator(content);

const $ = cheerio.load(result);
$('meta[name="generator"]').length.should.eql(1);

const expected = '<head></head>'
+ '<head><link><meta name="generator" content="Hexo ' + hexo.version + '"></head>'
+ '<head><link></head>';
result.should.eql(expected);
});
});

0 comments on commit 9a54447

Please sign in to comment.