Custom Layout Keys

This commit is contained in:
Valentin Boettcher 2017-09-06 17:01:58 +02:00
parent 7b8bd9b3e1
commit 65aaa7857e
2 changed files with 11 additions and 7 deletions

View file

@ -17,7 +17,7 @@ module.exports = check;
* @param {String} pattern
* @return {Boolean}
*/
function check(files, file, pattern, def){
function check(files, file, pattern, def, layoutKey){
var data = files[file];
// Only process utf8 encoded files (so no binary)
@ -32,5 +32,5 @@ function check(files, file, pattern, def){
// Only process files with a specified layout or default template. Allow the
// default template to be cancelled by layout: false.
return 'layout' in data ? data.layout : def;
return layoutKey in data ? data[layoutKey] : def;
}

View file

@ -32,7 +32,8 @@ var settings = [
'partialExtension',
'pattern',
'rename',
'exposeConsolidate'
'exposeConsolidate',
'layoutKey'
];
/**
@ -46,6 +47,7 @@ var settings = [
* @property {String} partialExtension (optional)
* @property {String} pattern (optional)
* @property {Boolean} rename (optional)
* @property {String} layoutKey (optional)
* @return {Function}
*/
function plugin(opts){
@ -70,7 +72,7 @@ function plugin(opts){
}
if (typeof opts.exposeConsolidate === 'function') {
opts.exposeConsolidate(consolidate.requires)
opts.exposeConsolidate(consolidate.requires);
}
// Map options to local variables
@ -81,6 +83,7 @@ function plugin(opts){
var partials = opts.partials;
var pattern = opts.pattern;
var rename = opts.rename;
var layoutKey = opts.layoutKey || 'layout';
// Move all unrecognised options to params
var params = omit(opts, settings);
@ -108,14 +111,15 @@ function plugin(opts){
* Stringify files that pass the check, pass to matches
*/
Object.keys(files).forEach(function(file){
if (!check(files, file, pattern, def)) {
if (!check(files, file, pattern, def, layoutKey)) {
return;
}
debug('stringifying file: %s', file);
var data = files[file];
data.contents = data.contents.toString();
var template = metalsmith.path(dir, data.layout || def);
var template = metalsmith.path(dir, data[layoutKey] || def);
if (!templates[template]) {
debug('found new template: %s', template);
templates[template] = file;
@ -134,7 +138,7 @@ function plugin(opts){
// Deep clone params (by passing 'true')
var clonedParams = extend(true, {}, params);
var clone = extend({}, clonedParams, metadata, data);
var str = metalsmith.path(dir, data.layout || def);
var str = metalsmith.path(dir, data[layoutKey] || def);
var render = consolidate[engine];
// Rename file if necessary