Skip to content

Commit

Permalink
feat: handle config.root is not exist, coauther:stevenjoezhang @s (he…
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangtj authored Jan 29, 2021
1 parent 8075ed5 commit a62417b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/hexo/load_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module.exports = async ctx => {
ctx.log.debug('Config loaded: %s', magenta(tildify(configPath)));

ctx.config = deepMerge(ctx.config, config);
// If root is not exist, create it by config.url
if (!config.root) {
let { pathname } = new URL(ctx.config.url);
if (!pathname.endsWith('/')) pathname += '/';
ctx.config.root = pathname;
}
config = ctx.config;

validateConfig(ctx);
Expand Down
21 changes: 21 additions & 0 deletions test/scripts/hexo/load_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,27 @@ describe('Load config', () => {
}
});

it('handle root is not exist', async () => {
try {
const content = 'url: https://hexo.io/';
await writeFile(hexo.config_path, content);
await loadConfig(hexo);
hexo.config.url.should.eql('https://hexo.io');
hexo.config.root.should.eql('/');
} finally {
await unlink(hexo.config_path);
}
try {
const content = 'url: https://hexo.io/foo/';
await writeFile(hexo.config_path, content);
await loadConfig(hexo);
hexo.config.url.should.eql('https://hexo.io/foo');
hexo.config.root.should.eql('/foo/');
} finally {
await unlink(hexo.config_path);
}
});

// Deprecated: config.external_link boolean option will be removed in future
it('migrate external_link config from boolean to object - true', async () => {
const content = 'external_link: true';
Expand Down

0 comments on commit a62417b

Please sign in to comment.