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

fix(theme): deep merge configs #3967

Merged
merged 2 commits into from
Dec 16, 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
7 changes: 6 additions & 1 deletion lib/hexo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const multiConfigPath = require('./multi_config_path');
const { sync } = require('resolve');
const full_url_for = require('../plugins/helper/full_url_for');
const { inherits } = require('util');
const { deepMerge } = require('hexo-util');

const libDir = dirname(__dirname);
const dbVersion = 1;
Expand Down Expand Up @@ -348,6 +349,10 @@ class Hexo {
const { config, theme } = this;
const ctx = { config: { url: this.config.url } };

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;
Expand All @@ -357,7 +362,7 @@ class Hexo {
}

Locals.prototype.config = config;
Locals.prototype.theme = Object.assign({}, config, theme.config, config.theme_config);
Locals.prototype.theme = deepMerge(config, theme.config);
Locals.prototype._ = _;
Locals.prototype.layout = 'layout';
Locals.prototype.env = this.env;
Expand Down
19 changes: 19 additions & 0 deletions test/scripts/hexo/hexo.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,25 @@ describe('Hexo', () => {
hexo.config_path.should.eql(pathFn.join(base_dir, '_multiconfig.yml'));
});

// Issue #3964
it('theme_config - deep clone', () => {
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;
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;
Object.prototype.hasOwnProperty.call(theme, 'c').should.eql(true);
});

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