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

feat: add prop to helper extension #4070

Closed
wants to merge 9 commits into from
12 changes: 12 additions & 0 deletions lib/extend/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class Helper {
constructor() {
this.store = {};
this.prop = {};
}

list() {
Expand All @@ -19,6 +20,17 @@ class Helper {

this.store[name] = fn;
}

setProp(name, prop) {
if (!name) throw new TypeError('name is required');
if (!prop) throw new TypeError('prop is required');
curbengh marked this conversation as resolved.
Show resolved Hide resolved

this.prop[name] = prop;
}

getProp(name) {
return this.prop[name];
}
}

module.exports = Helper;
19 changes: 15 additions & 4 deletions lib/plugins/filter/after_render/meta_generator.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
'use strict';

const { Cache } = require('hexo-util');
const cache = new Cache();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using local val is enough.

let needInject;

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, use cache.apply here made code more readable since cache.apply support caching return value of given function.

let metaGeneratorTag = '';

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

const hexoGeneratorTag = `<meta name="generator" content="Hexo ${this.version}">`;
const needInject = cache.apply('need-inject', () => {
Copy link
Member Author

@SukkaW SukkaW Jan 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just assume meta[name="generator"] will be inserted to every page if meta_generator() is used or <meta name="generator" content="Hexo [version]"> is manually added in the theme. So it is fine to cache this.

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

return true;
});

if (!needInject) return;
metaGeneratorTag = metaGeneratorTag || `<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>', metaGeneratorTag + '</head>'));
}

module.exports = hexoMetaGeneratorInject;
2 changes: 1 addition & 1 deletion lib/plugins/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = ctx => {
helper.register('list_tags', require('./list_tags'));
helper.register('list_posts', require('./list_posts'));

helper.register('meta_generator', require('./meta_generator'));
helper.register('meta_generator', require('./meta_generator')(ctx));

helper.register('open_graph', require('./open_graph'));

Expand Down
12 changes: 8 additions & 4 deletions lib/plugins/helper/meta_generator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
'use strict';

function metaGeneratorHelper() {
return `<meta name="generator" content="Hexo ${this.env.version}">`;
}
let metaGeneratorTag = '';

module.exports = metaGeneratorHelper;
module.exports = ctx => {
return () => {
if (!ctx.extend.helper.getProp('meta_generator_used')) ctx.extend.helper.setProp('meta_generator_used', true);
metaGeneratorTag = metaGeneratorTag || `<meta name="generator" content="Hexo ${ctx.version}">`;
return metaGeneratorTag;
};
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"cheerio": "0.22.0",
"decache": "^4.5.1",
"eslint": "^6.0.1",
"eslint-config-hexo": "^4.1.0",
"hexo-renderer-marked": "^2.0.0",
Expand Down
46 changes: 40 additions & 6 deletions test/scripts/extend/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

describe('Helper', () => {
const Helper = require('../../../lib/extend/helper');
let h;

it('register()', () => {
const h = new Helper();
beforeEach(() => {
h = new Helper();
});

it('register()', () => {
// name, fn
h.register('test', () => {});

Expand All @@ -14,6 +17,7 @@ describe('Helper', () => {
// no fn
try {
h.register('test');
should.fail();
} catch (err) {
err.should.be
.instanceOf(TypeError)
Expand All @@ -23,6 +27,7 @@ describe('Helper', () => {
// no name
try {
h.register();
should.fail();
} catch (err) {
err.should.be
.instanceOf(TypeError)
Expand All @@ -31,18 +36,47 @@ describe('Helper', () => {
});

it('list()', () => {
const h = new Helper();

h.register('test', () => {});

h.list().should.have.keys(['test']);
});

it('get()', () => {
const h = new Helper();

h.register('test', () => {});

h.get('test').should.exist;
});

it('setProp()', () => {
// name prop
h.setProp('foo', 'bar');

h.prop.foo.should.eql('bar');

// no name
try {
h.setProp();
SukkaW marked this conversation as resolved.
Show resolved Hide resolved
should.fail();
} catch (err) {
err.should.be
.instanceOf(TypeError)
.property('message', 'name is required');
}

// no prop
try {
h.setProp('test');
should.fail();
} catch (err) {
err.should.be
.instanceOf(TypeError)
.property('message', 'prop is required');
}
});

it('getProp()', () => {
h.setProp('foo', 'bar');

h.getProp('foo').should.eql('bar');
});
});
19 changes: 18 additions & 1 deletion test/scripts/filters/meta_generator.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
'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 metaGeneratorHelper = require('../../../lib/plugins/helper/meta_generator')(hexo);
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 Expand Up @@ -71,4 +79,13 @@ describe('Meta Generator', () => {

result.should.eql(expected);
});

it('no inject if meta_generator() helper is used', () => {
const content = '<head><link></head>';
hexo.config.meta_generator = true;
metaGeneratorHelper();
const result = metaGenerator(content);

should.not.exist(result);
});
});
8 changes: 7 additions & 1 deletion test/scripts/helpers/meta_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ describe('meta_generator', () => {
const Hexo = require('../../../lib/hexo');
const hexo = new Hexo();

const metaGeneratorHelper = require('../../../lib/plugins/helper/meta_generator').bind(hexo);
const metaGeneratorHelper = require('../../../lib/plugins/helper/meta_generator')(hexo);

before(() => hexo.init());

it('default', () => {
const { version } = hexo;
const versionType = typeof version;

should.not.exist(hexo.extend.helper.getProp('meta_generator_used'));

versionType.should.not.eql('undefined');
metaGeneratorHelper().should.eql(`<meta name="generator" content="Hexo ${version}">`);

hexo.extend.helper.getProp('meta_generator_used').should.eql(true);
});
});