Skip to content

Commit

Permalink
perf(meta_generator): drop cheerio (hexojs#3671)
Browse files Browse the repository at this point in the history
* refactor(meta_generator): drop cheerio

* fix(meta_generator): append to </title>

* test(meta_generator): remove irrelevant head tag test

* test(render): disable meta_generator

* fix(meta_generator): do not append if there is existing tag,

regardless the value of 'content'

* test(open_generator): test existing generator tag

* test(meta_generator): tag should be added only once

Previous commit only works on post, not page
This reverts commit 325f303.
  • Loading branch information
curbengh authored and Thomas Parisot committed Jan 17, 2020
1 parent 96d6556 commit 7a413f7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
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

0 comments on commit 7a413f7

Please sign in to comment.