feat(config): support layout specific config

This commit is contained in:
ppoffice 2019-12-26 11:27:36 -05:00
parent dfccd95d49
commit dd09bbe55b
2 changed files with 35 additions and 14 deletions

View File

@ -1,12 +1,25 @@
const createPostSchema = require('hexo/lib/models/post'); const fs = require('fs');
const createPageSchema = require('hexo/lib/models/page'); const path = require('path');
const yaml = require('js-yaml');
module.exports = hexo => { module.exports = hexo => {
const RESERVED_KEYS = { const RESERVED_KEYS = {
post: Object.keys(createPostSchema(hexo).paths), post: Object.keys(require('hexo/lib/models/post')(hexo).paths),
page: Object.keys(createPageSchema(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) { function getExtraConfig(source, reservedKeys) {
const result = {}; const result = {};
for (const key in source) { for (const key in source) {
@ -31,13 +44,21 @@ module.exports = hexo => {
locals.helper._p = locals._p; locals.helper._p = locals._p;
} }
// site config already merged into theme config in hexo/lib/hexo/index.js#Hexo.prototype._generateLocals() const page = locals.page;
locals.config = Object.assign({}, Object.getPrototypeOf(locals).theme); if (page) {
// merge page configs if ((page.layout !== 'page' || page.layout !== 'post') && ALTERNATIVE_CONFIG[page.layout]) {
if (locals.page.__post === true) { // load alternative config if exists
Object.assign(locals.config, getExtraConfig(locals.page, RESERVED_KEYS.page)); locals.config = Object.assign({}, Object.getPrototypeOf(locals).config, ALTERNATIVE_CONFIG[page.layout])
} else if (locals.page.__page === true) { } else {
Object.assign(locals.config, getExtraConfig(locals.page, RESERVED_KEYS.page)); // 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; return locals;

View File

@ -39,18 +39,18 @@ module.exports = class extends Component {
{/* Date */} {/* Date */}
<time class="level-item has-text-grey" dateTime={date_xml(page.date)}>{date(page.date)}</time> <time class="level-item has-text-grey" dateTime={date_xml(page.date)}>{date(page.date)}</time>
{/* Categories */} {/* Categories */}
{page.categories && page.categories.length ? <div class="level-item"> {page.categories && page.categories.length ? <span class="level-item">
{(() => { {(() => {
const categories = []; const categories = [];
page.categories.forEach((category, i) => { page.categories.forEach((category, i) => {
categories.push(<a class="has-link-grey" href={category.url}>{category.name}</a>); categories.push(<a class="has-link-grey" href={category.url}>{category.name}</a>);
if (i < page.categories.length - 1) { if (i < page.categories.length - 1) {
categories.push(' / '); categories.push(<span>&nbsp;/&nbsp;</span>);
} }
}); });
return categories; return categories;
})()} })()}
</div> : null} </span> : null}
{/* Read time */} {/* Read time */}
{article && article.readtime && article.readtime === true ? <span class="level-item has-text-grey"> {article && article.readtime && article.readtime === true ? <span class="level-item has-text-grey">
{(() => { {(() => {