From abff1127a92d4918c386346259592669c9670ae4 Mon Sep 17 00:00:00 2001 From: Micah Godbolt Date: Wed, 1 Feb 2017 23:53:04 -0800 Subject: [PATCH] added patternExtension property to provide method for limiting which files are considered patterns --- .gitignore | 3 ++- lib/helpers/read-partials.js | 7 ++++--- lib/index.js | 5 ++++- .../partials-partialExtension/build/index.html | 3 +++ .../expected/index.html | 3 +++ .../layouts/layout.html | 2 ++ .../layouts/partials/nested/nav.config.yaml | 1 + .../layouts/partials/nested/nav.html | 1 + .../partials-partialExtension/src/index.html | 4 ++++ .../layouts/partials/nested/nav.config.yaml | 0 test/index.js | 18 ++++++++++++++++++ 11 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/partials-partialExtension/build/index.html create mode 100644 test/fixtures/partials-partialExtension/expected/index.html create mode 100644 test/fixtures/partials-partialExtension/layouts/layout.html create mode 100644 test/fixtures/partials-partialExtension/layouts/partials/nested/nav.config.yaml create mode 100644 test/fixtures/partials-partialExtension/layouts/partials/nested/nav.html create mode 100644 test/fixtures/partials-partialExtension/src/index.html delete mode 100644 test/fixtures/partials-subdirectories/layouts/partials/nested/nav.config.yaml diff --git a/.gitignore b/.gitignore index b512c09..4d51494 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +debug.log \ No newline at end of file diff --git a/lib/helpers/read-partials.js b/lib/helpers/read-partials.js index 0b22f08..18ab248 100644 --- a/lib/helpers/read-partials.js +++ b/lib/helpers/read-partials.js @@ -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; diff --git a/lib/index.js b/lib/index.js index 7ec72d3..8d8316d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; } diff --git a/test/fixtures/partials-partialExtension/build/index.html b/test/fixtures/partials-partialExtension/build/index.html new file mode 100644 index 0000000..3706802 --- /dev/null +++ b/test/fixtures/partials-partialExtension/build/index.html @@ -0,0 +1,3 @@ +

Nav

+

Content

+ diff --git a/test/fixtures/partials-partialExtension/expected/index.html b/test/fixtures/partials-partialExtension/expected/index.html new file mode 100644 index 0000000..3706802 --- /dev/null +++ b/test/fixtures/partials-partialExtension/expected/index.html @@ -0,0 +1,3 @@ +

Nav

+

Content

+ diff --git a/test/fixtures/partials-partialExtension/layouts/layout.html b/test/fixtures/partials-partialExtension/layouts/layout.html new file mode 100644 index 0000000..0d619fa --- /dev/null +++ b/test/fixtures/partials-partialExtension/layouts/layout.html @@ -0,0 +1,2 @@ +{{>nested/nav}} +{{{contents}}} diff --git a/test/fixtures/partials-partialExtension/layouts/partials/nested/nav.config.yaml b/test/fixtures/partials-partialExtension/layouts/partials/nested/nav.config.yaml new file mode 100644 index 0000000..7daacd5 --- /dev/null +++ b/test/fixtures/partials-partialExtension/layouts/partials/nested/nav.config.yaml @@ -0,0 +1 @@ +foo: bar \ No newline at end of file diff --git a/test/fixtures/partials-partialExtension/layouts/partials/nested/nav.html b/test/fixtures/partials-partialExtension/layouts/partials/nested/nav.html new file mode 100644 index 0000000..8a1b726 --- /dev/null +++ b/test/fixtures/partials-partialExtension/layouts/partials/nested/nav.html @@ -0,0 +1 @@ +

Nav

diff --git a/test/fixtures/partials-partialExtension/src/index.html b/test/fixtures/partials-partialExtension/src/index.html new file mode 100644 index 0000000..f5e7012 --- /dev/null +++ b/test/fixtures/partials-partialExtension/src/index.html @@ -0,0 +1,4 @@ +--- +layout: layout.html +--- +

Content

diff --git a/test/fixtures/partials-subdirectories/layouts/partials/nested/nav.config.yaml b/test/fixtures/partials-subdirectories/layouts/partials/nested/nav.config.yaml deleted file mode 100644 index e69de29..0000000 diff --git a/test/index.js b/test/index.js index 2fe951b..efa95ba 100644 --- a/test/index.js +++ b/test/index.js @@ -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