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

perf(meta_generator): drop cheerio #3671

Merged
merged 7 commits into from
Aug 21, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 5 additions & 12 deletions lib/plugins/filter/meta_generator.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
'use strict';

let cheerio;
const hexoGeneratorTag = '<meta name="generator" content="Hexo %s" />';

function hexoMetaGeneratorInject(data) {
const { config } = this;
if (!config.meta_generator) return;

if (!cheerio) cheerio = require('cheerio');
const $ = cheerio.load(data, {decodeEntities: false});
if (!config.meta_generator ||
data.match(/<meta\s+name=['|"]?generator['|"]?/i)) return;

if (!($('meta[name="generator"]').length > 0) &&
$('head').contents().length > 0) {
$('head').prepend(hexoGeneratorTag.replace('%s', this.version));
const hexoGeneratorTag = '<meta name="generator" content="Hexo %s">'
.replace('%s', this.version);

return $.html();
}
return data.replace('</title>', '</title>' + hexoGeneratorTag);
}

module.exports = hexoMetaGeneratorInject;
16 changes: 8 additions & 8 deletions test/scripts/filters/meta_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ describe('Meta Generator', () => {
const cheerio = require('cheerio');

it('default', () => {
const content = '<head><link></head>';
const content = '<head><title>foo</title></head>';
const result = metaGenerator(content);

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

it('empty <head>', () => {
const content = '<head></head>';
it('disable meta_generator', () => {
const content = '<head><title>foo</title></head>';
hexo.config.meta_generator = false;
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');
});

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

const resultType = typeof result;
Expand Down
2 changes: 2 additions & 0 deletions test/scripts/hexo/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ describe('Render', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo(pathFn.join(__dirname, 'render_test'));

hexo.config.meta_generator = false;

const body = [
'name:',
' first: John',
Expand Down