mirror of
https://github.com/vale981/metalsmith-layouts
synced 2025-03-06 01:51:38 -05:00
68 lines
No EOL
1.8 KiB
JavaScript
68 lines
No EOL
1.8 KiB
JavaScript
|
|
var consolidate = require('consolidate');
|
|
var debug = require('debug')('metalsmith-templates');
|
|
var each = require('async').each;
|
|
var extend = require('extend');
|
|
var join = require('path').join;
|
|
var match = require('multimatch');
|
|
|
|
/**
|
|
* Expose `plugin`.
|
|
*/
|
|
|
|
module.exports = plugin;
|
|
|
|
/**
|
|
* Metalsmith plugin to run files through any template in a template `dir`.
|
|
*
|
|
* @param {String or Object} options
|
|
* @property {String} engine
|
|
* @property {String} pattern (optional)
|
|
* @property {String} directory (optional)
|
|
* @property {String} inPlace (optional)
|
|
* @return {Function}
|
|
*/
|
|
|
|
function plugin(opts){
|
|
opts = opts || {};
|
|
if ('string' == typeof opts) opts = { engine: opts };
|
|
if (!opts.engine) throw new Error('"engine" option required');
|
|
|
|
var engine = opts.engine;
|
|
var dir = opts.directory || 'templates';
|
|
var pattern = opts.pattern;
|
|
var inPlace = opts.inPlace;
|
|
|
|
return function(files, metalsmith, done){
|
|
var metadata = metalsmith.metadata();
|
|
|
|
each(Object.keys(files), convert, done);
|
|
|
|
function convert(file, done){
|
|
debug('checking file: %s', file);
|
|
var data = files[file];
|
|
var clone = extend({}, metadata, data);
|
|
var tmpl;
|
|
var render;
|
|
|
|
if (pattern && !match(file, pattern)[0]) return done();
|
|
if (!inPlace && !data.template) return done();
|
|
|
|
if (inPlace) {
|
|
tmpl = clone.contents.toString();
|
|
render = consolidate[engine].render;
|
|
} else {
|
|
tmpl = metalsmith.join(dir, data.template);
|
|
render = consolidate[engine];
|
|
clone.contents = clone.contents.toString();
|
|
}
|
|
|
|
render(tmpl, clone, function(err, str){
|
|
if (err) return done(err);
|
|
data.contents = new Buffer(str);
|
|
debug('converted file: %s', file);
|
|
done();
|
|
});
|
|
}
|
|
};
|
|
} |