added patternExtension property to provide method for limiting which files are considered patterns

This commit is contained in:
Micah Godbolt 2017-02-01 23:53:04 -08:00
parent ed5ec42ed1
commit abff1127a9
11 changed files with 42 additions and 5 deletions

3
.gitignore vendored
View file

@ -1 +1,2 @@
node_modules
node_modules
debug.log

View file

@ -18,7 +18,7 @@ module.exports = readPartials;
* @param {Object} metalsmith
* @return {Object}
*/
function readPartials(partialsPath, layoutsPath, metalsmith) {
function readPartials(partialsPath, partialExtension, layoutsPath, metalsmith) {
var partialsAbs = path.isAbsolute(partialsPath) ? partialsPath : path.join(metalsmith.path(), partialsPath);
var layoutsAbs = path.isAbsolute(layoutsPath) ? layoutsPath : path.join(metalsmith.path(), layoutsPath);
var files = read(partialsAbs);
@ -35,8 +35,9 @@ function readPartials(partialsPath, layoutsPath, metalsmith) {
var name = path.join(fileInfo.dir, fileInfo.name);
var partialAbs = path.join(partialsAbs, name);
var partialPath = path.relative(layoutsAbs, partialAbs);
partials[name.replace(/\\/g, '/')] = partialPath;
if (!partialExtension || fileInfo.ext == partialExtension) {
partials[name.replace(/\\/g, '/')] = partialPath;
}
}
return partials;

View file

@ -29,6 +29,7 @@ var settings = [
'directory',
'engine',
'partials',
'partialExtension',
'pattern',
'rename',
'exposeConsolidate'
@ -42,6 +43,7 @@ var settings = [
* @property {String} directory (optional)
* @property {String} engine
* @property {String} partials (optional)
* @property {String} partialExtension (optional)
* @property {String} pattern (optional)
* @property {Boolean} rename (optional)
* @return {Function}
@ -75,6 +77,7 @@ function plugin(opts){
var def = opts.default;
var dir = opts.directory || 'layouts';
var engine = opts.engine;
var partialExtension = opts.partialExtension;
var partials = opts.partials;
var pattern = opts.pattern;
var rename = opts.rename;
@ -94,7 +97,7 @@ function plugin(opts){
*/
if (partials) {
if (typeof partials === 'string') {
params.partials = readPartials(partials, dir, metalsmith);
params.partials = readPartials(partials, partialExtension, dir, metalsmith);
} else {
params.partials = partials;
}

View file

@ -0,0 +1,3 @@
<p>Nav</p>
<p>Content</p>

View file

@ -0,0 +1,3 @@
<p>Nav</p>
<p>Content</p>

View file

@ -0,0 +1,2 @@
{{>nested/nav}}
{{{contents}}}

View file

@ -0,0 +1 @@
foo: bar

View file

@ -0,0 +1 @@
<p>Nav</p>

View file

@ -0,0 +1,4 @@
---
layout: layout.html
---
<p>Content</p>

View file

@ -135,6 +135,24 @@ describe('metalsmith-layouts', function(){
});
});
it('should use partialExtension to limit the matching partial files', function(done){
// Without partialExtension the nav.config.yaml would cause an error
var instance = Metalsmith('test/fixtures/partials-partialExtension')
.use(layouts({
engine: 'handlebars',
partials: 'layouts/partials',
partialExtension: '.html'
}));
instance.build(function(err){
if (err) {
return done(err);
}
equal('test/fixtures/partials-partialExtension/expected', 'test/fixtures/partials-partialExtension/build');
done();
});
});
it('should find partials in subdirectories correctly', function(done){
// This test would only fail on Windows if readPartials did not
// replace backslashes in partial names