-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(load_theme_config): support alternate theme config
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
const { join, parse } = require('path'); | ||
const tildify = require('tildify'); | ||
const { exists, readdir } = require('hexo-fs'); | ||
const { magenta } = require('chalk'); | ||
const { deepMerge } = require('hexo-util'); | ||
|
||
module.exports = ctx => { | ||
if (!ctx.env.init) return; | ||
if (!ctx.config.theme) return; | ||
|
||
let configPath = join(ctx.base_dir, `_config.${String(ctx.config.theme)}.yml`); | ||
|
||
return exists(configPath).then(exist => { | ||
return exist ? configPath : findConfigPath(configPath); | ||
}).then(path => { | ||
if (!path) return; | ||
|
||
configPath = path; | ||
return ctx.render.render({ path }); | ||
}).then(config => { | ||
if (!config || typeof config !== 'object') return; | ||
|
||
ctx.log.debug('Second Theme Config loaded: %s', magenta(tildify(configPath))); | ||
|
||
// ctx.config.theme_config should have highest piority | ||
// If ctx.config.theme_config exists, then merge it with _config.[theme].yml | ||
// If ctx.config.theme_config doesn't exist, set it to _config.[theme].yml | ||
ctx.config.theme_config = ctx.config.theme_config | ||
? deepMerge(config, ctx.config.theme_config) : config; | ||
}); | ||
}; | ||
|
||
function findConfigPath(path) { | ||
const { dir, name } = parse(path); | ||
|
||
return readdir(dir).then(files => { | ||
const item = files.find(item => item.startsWith(name)); | ||
if (item != null) return join(dir, item); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
|
||
const { join } = require('path'); | ||
const { mkdirs, unlink, writeFile, rmdir } = require('hexo-fs'); | ||
|
||
describe('Load alternate theme config', () => { | ||
const Hexo = require('../../../lib/hexo'); | ||
const hexo = new Hexo(join(__dirname, 'config_test'), {silent: true}); | ||
const loadThemeConfig = require('../../../lib/hexo/load_theme_config'); | ||
|
||
hexo.env.init = true; | ||
|
||
before(() => mkdirs(hexo.base_dir).then(() => hexo.init())); | ||
|
||
after(() => rmdir(hexo.base_dir)); | ||
|
||
beforeEach(() => { | ||
hexo.config.theme_config = { foo: { bar: 'ahhhhhh' } }; | ||
hexo.config.theme = 'test_theme'; | ||
}); | ||
|
||
it('_config.[theme].yml does not exist', () => loadThemeConfig(hexo).then(() => { | ||
hexo.config.theme_config = {}; | ||
})); | ||
|
||
it('_config.[theme].yml exists', () => { | ||
const configPath = join(hexo.base_dir, '_config.test_theme.yml'); | ||
|
||
return writeFile(configPath, 'bar: 1').then(() => loadThemeConfig(hexo)).then(() => { | ||
hexo.config.theme_config.bar.should.eql(1); | ||
}).finally(() => unlink(configPath)); | ||
}); | ||
|
||
it('_config.[theme].json exists', () => { | ||
const configPath = join(hexo.base_dir, '_config.test_theme.json'); | ||
|
||
return writeFile(configPath, '{"baz": 3}').then(() => loadThemeConfig(hexo)).then(() => { | ||
hexo.config.theme_config.baz.should.eql(3); | ||
}).finally(() => unlink(configPath)); | ||
}); | ||
|
||
it('_config.[theme].txt exists', () => { | ||
const configPath = join(hexo.base_dir, '_config.test_theme.txt'); | ||
|
||
return writeFile(configPath, 'qux: 1').then(() => loadThemeConfig(hexo)).then(() => { | ||
should.not.exist(hexo.config.theme_config.qux); | ||
}).finally(() => unlink(configPath)); | ||
}); | ||
|
||
it('merge config', () => { | ||
const configPath = join(hexo.base_dir, '_config.test_theme.yml'); | ||
|
||
const content = [ | ||
'foo:', | ||
' bar: yoooo', | ||
' baz: true' | ||
].join('\n'); | ||
|
||
return writeFile(configPath, content).then(() => loadThemeConfig(hexo)).then(() => { | ||
hexo.config.theme_config.foo.baz.should.eql(true); | ||
hexo.config.theme_config.foo.bar.should.eql('ahhhhhh'); | ||
hexo.config.theme_config.foo.bar.should.not.eql('yoooo'); | ||
}).finally(() => unlink(configPath)); | ||
}); | ||
}); |