diff --git a/lib/index.js b/lib/index.js index 53d3c19..b6c639a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,6 +6,7 @@ var debug = require('debug')('metalsmith-layouts'); var each = require('async').each; var extend = require('extend'); var omit = require('lodash.omit'); +var path = require('path'); /** * Helpers @@ -23,7 +24,14 @@ module.exports = plugin; * * Options supported by metalsmith-layouts */ -var settings = ['default', 'directory', 'engine', 'partials', 'pattern']; +var settings = [ + 'default', + 'directory', + 'engine', + 'partials', + 'pattern', + 'rename' +]; /** * Metalsmith plugin to run files through any layout in a layout `dir`. @@ -34,6 +42,7 @@ var settings = ['default', 'directory', 'engine', 'partials', 'pattern']; * @property {String} engine * @property {String} partials (optional) * @property {String} pattern (optional) + * @property {Boolean} rename (optional) * @return {Function} */ function plugin(opts){ @@ -63,6 +72,7 @@ function plugin(opts){ var engine = opts.engine; var partials = opts.partials; var pattern = opts.pattern; + var rename = opts.rename; // Move all unrecognised options to params var params = omit(opts, settings); @@ -112,6 +122,15 @@ function plugin(opts){ var str = metalsmith.path(dir, data.layout || def); var render = consolidate[engine]; + // Rename file if necessary + var fileInfo; + if (rename) { + delete files[file]; + fileInfo = path.parse(file); + file = path.join(fileInfo.dir, fileInfo.name + '.html'); + debug('renamed file to: %s', file); + } + render(str, clone, function(err, str){ if (err) { return done(err); @@ -119,6 +138,8 @@ function plugin(opts){ data.contents = new Buffer(str); debug('converted file: %s', file); + + files[file] = data; done(); }); } diff --git a/test/fixtures/rename-option-default/build/index.hbs b/test/fixtures/rename-option-default/build/index.hbs new file mode 100644 index 0000000..ca1d7a1 --- /dev/null +++ b/test/fixtures/rename-option-default/build/index.hbs @@ -0,0 +1,7 @@ + + + +The content. + + + diff --git a/test/fixtures/rename-option-default/expected/index.hbs b/test/fixtures/rename-option-default/expected/index.hbs new file mode 100644 index 0000000..ca1d7a1 --- /dev/null +++ b/test/fixtures/rename-option-default/expected/index.hbs @@ -0,0 +1,7 @@ + + + +The content. + + + diff --git a/test/fixtures/rename-option-default/layouts/layout.hbs b/test/fixtures/rename-option-default/layouts/layout.hbs new file mode 100644 index 0000000..904d5b4 --- /dev/null +++ b/test/fixtures/rename-option-default/layouts/layout.hbs @@ -0,0 +1,5 @@ + + + {{contents}} + + diff --git a/test/fixtures/rename-option-default/src/index.hbs b/test/fixtures/rename-option-default/src/index.hbs new file mode 100644 index 0000000..17e447b --- /dev/null +++ b/test/fixtures/rename-option-default/src/index.hbs @@ -0,0 +1,5 @@ +--- +layout: layout.hbs +--- + +The content. diff --git a/test/fixtures/rename-option-nested/build/folder/index.html b/test/fixtures/rename-option-nested/build/folder/index.html new file mode 100644 index 0000000..ca1d7a1 --- /dev/null +++ b/test/fixtures/rename-option-nested/build/folder/index.html @@ -0,0 +1,7 @@ + + + +The content. + + + diff --git a/test/fixtures/rename-option-nested/expected/folder/index.html b/test/fixtures/rename-option-nested/expected/folder/index.html new file mode 100644 index 0000000..ca1d7a1 --- /dev/null +++ b/test/fixtures/rename-option-nested/expected/folder/index.html @@ -0,0 +1,7 @@ + + + +The content. + + + diff --git a/test/fixtures/rename-option-nested/layouts/layout.hbs b/test/fixtures/rename-option-nested/layouts/layout.hbs new file mode 100644 index 0000000..904d5b4 --- /dev/null +++ b/test/fixtures/rename-option-nested/layouts/layout.hbs @@ -0,0 +1,5 @@ + + + {{contents}} + + diff --git a/test/fixtures/rename-option-nested/src/folder/index.hbs b/test/fixtures/rename-option-nested/src/folder/index.hbs new file mode 100644 index 0000000..17e447b --- /dev/null +++ b/test/fixtures/rename-option-nested/src/folder/index.hbs @@ -0,0 +1,5 @@ +--- +layout: layout.hbs +--- + +The content. diff --git a/test/fixtures/rename-option/build/index.html b/test/fixtures/rename-option/build/index.html new file mode 100644 index 0000000..ca1d7a1 --- /dev/null +++ b/test/fixtures/rename-option/build/index.html @@ -0,0 +1,7 @@ + + + +The content. + + + diff --git a/test/fixtures/rename-option/expected/index.html b/test/fixtures/rename-option/expected/index.html new file mode 100644 index 0000000..ca1d7a1 --- /dev/null +++ b/test/fixtures/rename-option/expected/index.html @@ -0,0 +1,7 @@ + + + +The content. + + + diff --git a/test/fixtures/rename-option/layouts/layout.hbs b/test/fixtures/rename-option/layouts/layout.hbs new file mode 100644 index 0000000..904d5b4 --- /dev/null +++ b/test/fixtures/rename-option/layouts/layout.hbs @@ -0,0 +1,5 @@ + + + {{contents}} + + diff --git a/test/fixtures/rename-option/src/index.hbs b/test/fixtures/rename-option/src/index.hbs new file mode 100644 index 0000000..17e447b --- /dev/null +++ b/test/fixtures/rename-option/src/index.hbs @@ -0,0 +1,5 @@ +--- +layout: layout.hbs +--- + +The content. diff --git a/test/index.js b/test/index.js index 2ba982f..ab2e42f 100644 --- a/test/index.js +++ b/test/index.js @@ -149,4 +149,42 @@ describe('metalsmith-layouts', function(){ }); }); + it('should not change file extension by default', function(done) { + Metalsmith('test/fixtures/rename-option-default') + .use(layouts({ + engine: 'handlebars' + })) + .build(function (err) { + if (err) return done(err); + equal('test/fixtures/rename-option-default/expected', 'test/fixtures/rename-option-default/build'); + done(); + }); + }); + + it('should change file extension when rename option is set to true', function(done) { + Metalsmith('test/fixtures/rename-option') + .use(layouts({ + engine: 'handlebars', + rename: true + })) + .build(function (err) { + if (err) return done(err); + equal('test/fixtures/rename-option/expected', 'test/fixtures/rename-option/build'); + done(); + }); + }); + + it('should change file extension for nested files when rename option is set to true', function(done) { + Metalsmith('test/fixtures/rename-option-nested') + .use(layouts({ + engine: 'handlebars', + rename: true + })) + .build(function (err) { + if (err) return done(err); + equal('test/fixtures/rename-option-nested/expected', 'test/fixtures/rename-option-nested/build'); + done(); + }); + }); + });