Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compatibility with cheerio 1.0.0+ (#3313) #3315

Merged
merged 3 commits into from
Oct 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/plugins/filter/meta_generator.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

describe('Meta Generator', () => {
const Hexo = require('../../../lib/hexo');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, We are migrating hexo code to ES6 syntax.
Would you please change to arrow function ? Also others.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

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');
});
});