mirror of
https://github.com/vale981/metalsmith-layouts
synced 2025-03-04 17:21:37 -05:00
Add rename option
This commit is contained in:
parent
8706169ec7
commit
a4dd05abac
14 changed files with 132 additions and 1 deletions
23
lib/index.js
23
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();
|
||||
});
|
||||
}
|
||||
|
|
7
test/fixtures/rename-option-default/build/index.hbs
vendored
Normal file
7
test/fixtures/rename-option-default/build/index.hbs
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
<body>
|
||||
|
||||
The content.
|
||||
|
||||
</body>
|
||||
</html>
|
7
test/fixtures/rename-option-default/expected/index.hbs
vendored
Normal file
7
test/fixtures/rename-option-default/expected/index.hbs
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
<body>
|
||||
|
||||
The content.
|
||||
|
||||
</body>
|
||||
</html>
|
5
test/fixtures/rename-option-default/layouts/layout.hbs
vendored
Normal file
5
test/fixtures/rename-option-default/layouts/layout.hbs
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
<html>
|
||||
<body>
|
||||
{{contents}}
|
||||
</body>
|
||||
</html>
|
5
test/fixtures/rename-option-default/src/index.hbs
vendored
Normal file
5
test/fixtures/rename-option-default/src/index.hbs
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
layout: layout.hbs
|
||||
---
|
||||
|
||||
The content.
|
7
test/fixtures/rename-option-nested/build/folder/index.html
vendored
Normal file
7
test/fixtures/rename-option-nested/build/folder/index.html
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
<body>
|
||||
|
||||
The content.
|
||||
|
||||
</body>
|
||||
</html>
|
7
test/fixtures/rename-option-nested/expected/folder/index.html
vendored
Normal file
7
test/fixtures/rename-option-nested/expected/folder/index.html
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
<body>
|
||||
|
||||
The content.
|
||||
|
||||
</body>
|
||||
</html>
|
5
test/fixtures/rename-option-nested/layouts/layout.hbs
vendored
Normal file
5
test/fixtures/rename-option-nested/layouts/layout.hbs
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
<html>
|
||||
<body>
|
||||
{{contents}}
|
||||
</body>
|
||||
</html>
|
5
test/fixtures/rename-option-nested/src/folder/index.hbs
vendored
Normal file
5
test/fixtures/rename-option-nested/src/folder/index.hbs
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
layout: layout.hbs
|
||||
---
|
||||
|
||||
The content.
|
7
test/fixtures/rename-option/build/index.html
vendored
Normal file
7
test/fixtures/rename-option/build/index.html
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
<body>
|
||||
|
||||
The content.
|
||||
|
||||
</body>
|
||||
</html>
|
7
test/fixtures/rename-option/expected/index.html
vendored
Normal file
7
test/fixtures/rename-option/expected/index.html
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
<body>
|
||||
|
||||
The content.
|
||||
|
||||
</body>
|
||||
</html>
|
5
test/fixtures/rename-option/layouts/layout.hbs
vendored
Normal file
5
test/fixtures/rename-option/layouts/layout.hbs
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
<html>
|
||||
<body>
|
||||
{{contents}}
|
||||
</body>
|
||||
</html>
|
5
test/fixtures/rename-option/src/index.hbs
vendored
Normal file
5
test/fixtures/rename-option/src/index.hbs
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
layout: layout.hbs
|
||||
---
|
||||
|
||||
The content.
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue