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): avoid unnecessary check #4208

Merged
merged 1 commit into from
Apr 1, 2020
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
17 changes: 12 additions & 5 deletions lib/plugins/filter/after_render/meta_generator.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
'use strict';

let NEED_INJECT = true;
let META_GENERATOR_TAG;

function hexoMetaGeneratorInject(data) {
const { config } = this;
if (!config.meta_generator
|| data.match(/<meta([\s]+|[\s]+[^<>]+[\s]+)name=['|"]?generator['|"]?/i)) return;
if (!NEED_INJECT) return;

if (!this.config.meta_generator
|| data.match(/<meta([\s]+|[\s]+[^<>]+[\s]+)name=['|"]?generator['|"]?/i)) {
NEED_INJECT = false;
return;
}

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

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

module.exports = hexoMetaGeneratorInject;
9 changes: 8 additions & 1 deletion test/scripts/filters/meta_generator.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
'use strict';

const decache = require('decache');

describe('Meta Generator', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo();
const metaGenerator = require('../../../lib/plugins/filter/after_render/meta_generator').bind(hexo);
let metaGenerator;
const cheerio = require('cheerio');

beforeEach(() => {
decache('../../../lib/plugins/filter/after_render/meta_generator');
metaGenerator = require('../../../lib/plugins/filter/after_render/meta_generator').bind(hexo);
});

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