diff --git a/lib/hexo/load_config.js b/lib/hexo/load_config.js index 61e1132be6..258bdab0c7 100644 --- a/lib/hexo/load_config.js +++ b/lib/hexo/load_config.js @@ -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); diff --git a/test/scripts/hexo/load_config.js b/test/scripts/hexo/load_config.js index cd68a08779..a985db11ff 100644 --- a/test/scripts/hexo/load_config.js +++ b/test/scripts/hexo/load_config.js @@ -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';