Skip to content

Commit

Permalink
Merge pull request #3315 from weyusi/fix-meta
Browse files Browse the repository at this point in the history
fix: compatibility with cheerio 1.0.0+ (#3313)
  • Loading branch information
yoshinorin committed Oct 28, 2018
2 parents bbc2f04 + 4d071c4 commit 51808d5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/plugins/filter/meta_generator.js
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions test/scripts/filters/index.js
Expand Up @@ -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');
Expand Down
26 changes: 26 additions & 0 deletions 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 = '<head><link></head>';
const result = metaGenerator(content);

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

it('empty <head>', () => {
const content = '<head></head>';
const result = metaGenerator(content);

// meta generator should not be prepended if <head> tag is empty
// see https://github.com/hexojs/hexo/pull/3315
const resultType = typeof result;
resultType.should.eql('undefined');
});
});

0 comments on commit 51808d5

Please sign in to comment.