Add rename option

This commit is contained in:
ismay 2016-02-14 15:09:30 +01:00
parent 8706169ec7
commit a4dd05abac
14 changed files with 132 additions and 1 deletions

View file

@ -6,6 +6,7 @@ var debug = require('debug')('metalsmith-layouts');
var each = require('async').each; var each = require('async').each;
var extend = require('extend'); var extend = require('extend');
var omit = require('lodash.omit'); var omit = require('lodash.omit');
var path = require('path');
/** /**
* Helpers * Helpers
@ -23,7 +24,14 @@ module.exports = plugin;
* *
* Options supported by metalsmith-layouts * 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`. * 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} engine
* @property {String} partials (optional) * @property {String} partials (optional)
* @property {String} pattern (optional) * @property {String} pattern (optional)
* @property {Boolean} rename (optional)
* @return {Function} * @return {Function}
*/ */
function plugin(opts){ function plugin(opts){
@ -63,6 +72,7 @@ function plugin(opts){
var engine = opts.engine; var engine = opts.engine;
var partials = opts.partials; var partials = opts.partials;
var pattern = opts.pattern; var pattern = opts.pattern;
var rename = opts.rename;
// Move all unrecognised options to params // Move all unrecognised options to params
var params = omit(opts, settings); var params = omit(opts, settings);
@ -112,6 +122,15 @@ function plugin(opts){
var str = metalsmith.path(dir, data.layout || def); var str = metalsmith.path(dir, data.layout || def);
var render = consolidate[engine]; 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){ render(str, clone, function(err, str){
if (err) { if (err) {
return done(err); return done(err);
@ -119,6 +138,8 @@ function plugin(opts){
data.contents = new Buffer(str); data.contents = new Buffer(str);
debug('converted file: %s', file); debug('converted file: %s', file);
files[file] = data;
done(); done();
}); });
} }

View file

@ -0,0 +1,7 @@
<html>
<body>
The content.
</body>
</html>

View file

@ -0,0 +1,7 @@
<html>
<body>
The content.
</body>
</html>

View file

@ -0,0 +1,5 @@
<html>
<body>
{{contents}}
</body>
</html>

View file

@ -0,0 +1,5 @@
---
layout: layout.hbs
---
The content.

View file

@ -0,0 +1,7 @@
<html>
<body>
The content.
</body>
</html>

View file

@ -0,0 +1,7 @@
<html>
<body>
The content.
</body>
</html>

View file

@ -0,0 +1,5 @@
<html>
<body>
{{contents}}
</body>
</html>

View file

@ -0,0 +1,5 @@
---
layout: layout.hbs
---
The content.

View file

@ -0,0 +1,7 @@
<html>
<body>
The content.
</body>
</html>

View file

@ -0,0 +1,7 @@
<html>
<body>
The content.
</body>
</html>

View file

@ -0,0 +1,5 @@
<html>
<body>
{{contents}}
</body>
</html>

View file

@ -0,0 +1,5 @@
---
layout: layout.hbs
---
The content.

View file

@ -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();
});
});
}); });