Skip to content

Commit

Permalink
perf(meta_generator): drop cheerio (#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 tomap committed Aug 21, 2019
1 parent ec99001 commit 651f34b
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);

This comment has been minimized.

Copy link
@SukkaW

SukkaW Aug 31, 2019

Member

@curbengh After reviewing this, I am thinking could we use template string instead of replace()?

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

Even without template string is ok:

const hexoGeneratorTag = '<meta name="generator" content="Hexo ' + this.version '">'

This comment has been minimized.

Copy link
@tomap

tomap Nov 16, 2019

Contributor

We could also drop this meta generator and ask every theme owner to include this string in their theme (that would also be faster during generation, wouldn't it?)

This comment has been minimized.

Copy link
@SukkaW

SukkaW Nov 16, 2019

Member

@tomap Yeah, that's why I implement #3782 , a helper to generate meta_generaror tag. We can add a notice at https://github.com/hexojs/hexo-theme-unit-test


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 651f34b

Please sign in to comment.