feat(config): support layout specific config
parent
dfccd95d49
commit
dd09bbe55b
|
@ -1,12 +1,25 @@
|
|||
const createPostSchema = require('hexo/lib/models/post');
|
||||
const createPageSchema = require('hexo/lib/models/page');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const yaml = require('js-yaml');
|
||||
|
||||
module.exports = hexo => {
|
||||
const RESERVED_KEYS = {
|
||||
post: Object.keys(createPostSchema(hexo).paths),
|
||||
page: Object.keys(createPageSchema(hexo).paths)
|
||||
post: Object.keys(require('hexo/lib/models/post')(hexo).paths),
|
||||
page: Object.keys(require('hexo/lib/models/page')(hexo).paths)
|
||||
};
|
||||
|
||||
function getThemeConfig(extension) {
|
||||
if (fs.existsSync(path.join(hexo.theme_dir, '_config' + extension + '.yml'))) {
|
||||
return yaml.safeLoad(fs.readFileSync(path.join(hexo.theme_dir, '_config' + extension + '.yml')));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const ALTERNATIVE_CONFIG = {
|
||||
post: getThemeConfig('.post'),
|
||||
page: getThemeConfig('.page')
|
||||
}
|
||||
|
||||
function getExtraConfig(source, reservedKeys) {
|
||||
const result = {};
|
||||
for (const key in source) {
|
||||
|
@ -31,13 +44,21 @@ module.exports = hexo => {
|
|||
locals.helper._p = locals._p;
|
||||
}
|
||||
|
||||
// site config already merged into theme config in hexo/lib/hexo/index.js#Hexo.prototype._generateLocals()
|
||||
locals.config = Object.assign({}, Object.getPrototypeOf(locals).theme);
|
||||
// merge page configs
|
||||
if (locals.page.__post === true) {
|
||||
Object.assign(locals.config, getExtraConfig(locals.page, RESERVED_KEYS.page));
|
||||
} else if (locals.page.__page === true) {
|
||||
Object.assign(locals.config, getExtraConfig(locals.page, RESERVED_KEYS.page));
|
||||
const page = locals.page;
|
||||
if (page) {
|
||||
if ((page.layout !== 'page' || page.layout !== 'post') && ALTERNATIVE_CONFIG[page.layout]) {
|
||||
// load alternative config if exists
|
||||
locals.config = Object.assign({}, Object.getPrototypeOf(locals).config, ALTERNATIVE_CONFIG[page.layout])
|
||||
} else {
|
||||
// site config already merged into theme config in hexo/lib/hexo/index.js#Hexo.prototype._generateLocals()
|
||||
locals.config = Object.assign({}, Object.getPrototypeOf(locals).theme);
|
||||
}
|
||||
// merge page configs
|
||||
if (page.__post === true) {
|
||||
Object.assign(locals.config, getExtraConfig(page, RESERVED_KEYS.page));
|
||||
} else if (page.__page === true) {
|
||||
Object.assign(locals.config, getExtraConfig(page, RESERVED_KEYS.page));
|
||||
}
|
||||
}
|
||||
|
||||
return locals;
|
||||
|
|
|
@ -39,18 +39,18 @@ module.exports = class extends Component {
|
|||
{/* Date */}
|
||||
<time class="level-item has-text-grey" dateTime={date_xml(page.date)}>{date(page.date)}</time>
|
||||
{/* Categories */}
|
||||
{page.categories && page.categories.length ? <div class="level-item">
|
||||
{page.categories && page.categories.length ? <span class="level-item">
|
||||
{(() => {
|
||||
const categories = [];
|
||||
page.categories.forEach((category, i) => {
|
||||
categories.push(<a class="has-link-grey" href={category.url}>{category.name}</a>);
|
||||
if (i < page.categories.length - 1) {
|
||||
categories.push(' / ');
|
||||
categories.push(<span> / </span>);
|
||||
}
|
||||
});
|
||||
return categories;
|
||||
})()}
|
||||
</div> : null}
|
||||
</span> : null}
|
||||
{/* Read time */}
|
||||
{article && article.readtime && article.readtime === true ? <span class="level-item has-text-grey">
|
||||
{(() => {
|
||||
|
|
Loading…
Reference in New Issue