Skip to content

Commit

Permalink
Merge pull request #3981 from curbengh/hexo-locals-class
Browse files Browse the repository at this point in the history
refactor(hexo-locals): Class syntax
  • Loading branch information
curbengh authored Dec 18, 2019
2 parents 5a503ff + 3378541 commit fa59eb2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
31 changes: 16 additions & 15 deletions lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,29 +350,30 @@ class Hexo {
}

_generateLocals() {
const { config, theme } = this;
const { config, env, theme, theme_dir } = this;
const ctx = { config: { url: this.config.url } };
const localsObj = this.locals.toObject();

if (config.theme_config) {
theme.config = deepMerge(theme.config, config.theme_config);
}

function Locals(path, locals) {
this.page = typeof locals === 'object' ? locals : {};
if (this.page.path == null) this.page.path = path;

this.path = path;
this.url = full_url_for.call(ctx, path);
class Locals {
constructor(path, locals) {
this.page = typeof locals === 'object' ? locals : {};
if (this.page.path == null) this.page.path = path;
this.path = path;
this.url = full_url_for.call(ctx, path);
this.config = config;
this.theme = deepMerge(config, theme.config);
this._ = _;
this.layout = 'layout';
this.env = env;
this.view_dir = join(theme_dir, 'layout') + sep;
this.site = localsObj;
}
}

Locals.prototype.config = config;
Locals.prototype.theme = deepMerge(config, theme.config);
Locals.prototype._ = _;
Locals.prototype.layout = 'layout';
Locals.prototype.env = this.env;
Locals.prototype.view_dir = join(this.theme_dir, 'layout') + sep;
Locals.prototype.site = this.locals.toObject();

return Locals;
}

Expand Down
10 changes: 7 additions & 3 deletions test/scripts/hexo/hexo.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,22 @@ describe('Hexo', () => {
const hexo = new Hexo(__dirname);
hexo.theme.config = { a: { b: 1, c: 2 } };
hexo.config.theme_config = { a: { b: 3 } };
const theme = hexo._generateLocals().prototype.theme;
const Locals = hexo._generateLocals();
const { theme } = new Locals();

Object.prototype.hasOwnProperty.call(theme.a, 'c').should.eql(true);
theme.a.b.should.eql(3);
});

it('theme_config - null theme.config', () => {
const hexo = new Hexo(__dirname);
hexo.theme.config = null;
hexo.config = { a: 1, b: 2 };
hexo.config.theme_config = { c: 3 };
const theme = hexo._generateLocals().prototype.theme;
const Locals = hexo._generateLocals();
const { theme } = new Locals();

Object.prototype.hasOwnProperty.call(theme, 'c').should.eql(true);
theme.c.should.eql(3);
});

it('call()', () => hexo.call('test', {foo: 'bar'}).then(data => {
Expand Down

0 comments on commit fa59eb2

Please sign in to comment.