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

refactor(meta_generator): update regex and avoid edge case #3697

Merged
merged 2 commits into from
Sep 2, 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.
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) {

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

return data.replace('</title>', '</title>' + hexoGeneratorTag);
return data.replace(/<head>(?!<\/head>).+?<\/head>/, (str) => str.replace('</head>', `${hexoGeneratorTag}</head>`));
}

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

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

const $ = cheerio.load(result);
$('meta[name="generator"]').length.should.eql(1);
$('meta[name="generator"]').attr('content').should.eql(`Hexo ${hexo.version}`);
});

it('disable meta_generator', () => {
const content = '<head><title>foo</title></head>';
const content = '<head><link></head>';
hexo.config.meta_generator = false;
const result = metaGenerator(content);

Expand All @@ -24,12 +25,44 @@ describe('Meta Generator', () => {
});

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

const resultType = typeof result;
resultType.should.eql('undefined');
});

it('ignore empty head tag', () => {
curbengh marked this conversation as resolved.
Show resolved Hide resolved
const content = '<head></head>'
+ '<head><link></head>'
+ '<head></head>';
hexo.config.meta_generator = true;
const result = metaGenerator(content);

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

const expected = '<head></head>'
+ '<head><link><meta name="generator" content="Hexo ' + hexo.version + '"></head>'
+ '<head></head>';
result.should.eql(expected);
});

it('apply to first non-empty head tag only', () => {
const content = '<head></head>'
+ '<head><link></head>'
+ '<head><link></head>';
hexo.config.meta_generator = true;
const result = metaGenerator(content);

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

const expected = '<head></head>'
+ '<head><link><meta name="generator" content="Hexo ' + hexo.version + '"></head>'
+ '<head><link></head>';
result.should.eql(expected);
});
});