metalsmith-layouts
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -0,1 +1,7 @@ | ||
1.3.0 - August 7, 2015 | ||
--------------------- | ||
* add swig include test | ||
* code style, dependency and readme updates | ||
* add partials option and test | ||
1.2.1 - July 25, 2015 | ||
@@ -2,0 +8,0 @@ --------------------- |
@@ -0,1 +1,4 @@ | ||
/** | ||
* Dependencies | ||
*/ | ||
var consolidate = require('consolidate'); | ||
@@ -5,18 +8,22 @@ var debug = require('debug')('metalsmith-layouts'); | ||
var extend = require('extend'); | ||
var match = require('multimatch'); | ||
var omit = require('lodash.omit'); | ||
var utf8 = require('is-utf8'); | ||
/** | ||
* Helpers | ||
*/ | ||
var check = require('./helpers/check'); | ||
var readPartials = require('./helpers/read-partials'); | ||
/** | ||
* Expose `plugin`. | ||
*/ | ||
module.exports = plugin; | ||
/** | ||
* Settings. | ||
* Settings | ||
* | ||
* Options supported by metalsmith-layouts | ||
*/ | ||
var settings = ['default', 'directory', 'engine', 'partials', 'pattern']; | ||
var settings = ['engine', 'directory', 'pattern', 'default']; | ||
/** | ||
@@ -29,2 +36,3 @@ * Metalsmith plugin to run files through any layout in a layout `dir`. | ||
* @property {String} engine | ||
* @property {String} partials (optional) | ||
* @property {String} pattern (optional) | ||
@@ -35,15 +43,35 @@ * @return {Function} | ||
function plugin(opts){ | ||
/** | ||
* Init | ||
*/ | ||
opts = opts || {}; | ||
if (typeof opts === 'string') opts = { engine: opts }; | ||
if (!opts.engine) throw new Error('"engine" option required'); | ||
// Accept string option to specify the engine | ||
if (typeof opts === 'string') { | ||
opts = {engine: opts}; | ||
} | ||
// An engine should be specified | ||
if (!opts.engine) { | ||
throw new Error('"engine" option required'); | ||
} | ||
// Throw an error for unsupported engines or typos | ||
if (!consolidate[opts.engine]) { | ||
throw new Error('Unknown template engine: "' + opts.engine + '"'); | ||
} | ||
// Map options to local variables | ||
var def = opts.default; | ||
var dir = opts.directory || 'layouts'; | ||
var engine = opts.engine; | ||
var dir = opts.directory || 'layouts'; | ||
var partials = opts.partials; | ||
var pattern = opts.pattern; | ||
var def = opts.default; | ||
// Move all unrecognised options to params | ||
var params = omit(opts, settings); | ||
// Throw an error for unsupported engines or typos | ||
if (!consolidate[engine]) throw new Error('Unknown template engine: "' + engine + '"'); | ||
/** | ||
* Main plugin function | ||
*/ | ||
return function(files, metalsmith, done){ | ||
@@ -53,13 +81,21 @@ var metadata = metalsmith.metadata(); | ||
function check(file){ | ||
var data = files[file]; | ||
var lyt = data.layout || def; | ||
if (!utf8(data.contents)) return false; | ||
if (pattern && !match(file, pattern)[0]) return false; | ||
if (!lyt) return false; | ||
return true; | ||
/** | ||
* Process any partials and pass them to consolidate as a partials object | ||
*/ | ||
if (partials) { | ||
if (typeof partials === 'string') { | ||
params.partials = readPartials(partials, dir, metalsmith); | ||
} else { | ||
params.partials = partials; | ||
} | ||
} | ||
/** | ||
* Stringify files that pass the check, and store in matches | ||
*/ | ||
Object.keys(files).forEach(function(file){ | ||
if (!check(file)) return; | ||
if (!check(files, file, pattern)) { | ||
return; | ||
} | ||
debug('stringifying file: %s', file); | ||
@@ -71,7 +107,10 @@ var data = files[file]; | ||
each(Object.keys(matches), convert, done); | ||
/** | ||
* Render files | ||
*/ | ||
function convert(file, done){ | ||
debug('converting file: %s', file); | ||
var data = files[file]; | ||
// Deep clone params (by passing 'true') | ||
var clonedParams = extend(true, {}, params); | ||
@@ -83,3 +122,6 @@ var clone = extend({}, clonedParams, metadata, data); | ||
render(str, clone, function(err, str){ | ||
if (err) return done(err); | ||
if (err) { | ||
return done(err); | ||
} | ||
data.contents = new Buffer(str); | ||
@@ -90,3 +132,8 @@ debug('converted file: %s', file); | ||
} | ||
/** | ||
* Render all matched files | ||
*/ | ||
each(Object.keys(matches), convert, done); | ||
}; | ||
} |
@@ -6,3 +6,3 @@ { | ||
"repository": "git://github.com/superwolff/metalsmith-layouts.git", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"license": "MIT", | ||
@@ -18,2 +18,3 @@ "main": "lib/index.js", | ||
"extend": "^3.0.0", | ||
"fs-readdir-recursive": "^0.1.2", | ||
"is-utf8": "^0.2.0", | ||
@@ -25,3 +26,3 @@ "lodash.omit": "^3.1.0", | ||
"assert-dir-equal": "^1.0.1", | ||
"eslint": "^0.24.1", | ||
"eslint": "^1.0.0", | ||
"handlebars": "^3.0.3", | ||
@@ -28,0 +29,0 @@ "metalsmith": "^2.0.1", |
@@ -12,5 +12,6 @@ # metalsmith-layouts | ||
* `directory`: directory for the layouts, `layouts` by default (optional) | ||
* `partials`: a folder to scan for partials, will register all found files as partials (optional) | ||
* `pattern`: only files that match this pattern will be processed (optional) | ||
Any unrecognised options will be passed on to consolidate.js. You can use this, for example, to disable caching by passing `cache: false` to consolidate. See the [consolidate.js documentation](https://github.com/tj/consolidate.js) for all available options. | ||
Any unrecognised options will be passed on to consolidate.js. You can use this, for example, to disable caching by passing `cache: false` to consolidate. Note that passing anything but a string to the `partials` option will pass the option on to consolidate. See the [consolidate.js documentation](https://github.com/tj/consolidate.js) for all available options. | ||
@@ -31,3 +32,4 @@ ## Installation | ||
"metalsmith-layouts": { | ||
"engine": "handlebars" | ||
"engine": "handlebars", | ||
"partials": "partials" | ||
} | ||
@@ -57,2 +59,3 @@ } | ||
<body> | ||
{{>nav}} | ||
{{{contents}}} | ||
@@ -63,2 +66,9 @@ </body> | ||
Partial file `partials/nav.html`: | ||
```html | ||
<!-- The partial name is the path relative to the `partials` folder, without the extension --> | ||
<nav>Nav</nav> | ||
``` | ||
Results in `dist/index.html`: | ||
@@ -73,2 +83,3 @@ | ||
<body> | ||
<nav>Nav</nav> | ||
<p>The contents</p> | ||
@@ -75,0 +86,0 @@ </body> |
@@ -11,3 +11,5 @@ var assert = require('assert'); | ||
.build(function(err){ | ||
if (err) return done(err); | ||
if (err) { | ||
return done(err); | ||
} | ||
equal('test/fixtures/basic/expected', 'test/fixtures/basic/build'); | ||
@@ -22,3 +24,5 @@ done(); | ||
.build(function(err){ | ||
if (err) return done(err); | ||
if (err) { | ||
return done(err); | ||
} | ||
equal('test/fixtures/basic/expected', 'test/fixtures/basic/build'); | ||
@@ -33,3 +37,5 @@ done(); | ||
.build(function(err){ | ||
if (err) return done(err); | ||
if (err) { | ||
return done(err); | ||
} | ||
equal('test/fixtures/pattern/expected', 'test/fixtures/pattern/build'); | ||
@@ -44,3 +50,5 @@ done(); | ||
.build(function(err){ | ||
if (err) return done(err); | ||
if (err) { | ||
return done(err); | ||
} | ||
equal('test/fixtures/pattern/expected', 'test/fixtures/pattern/build'); | ||
@@ -55,3 +63,5 @@ done(); | ||
.build(function(err){ | ||
if (err) return done(err); | ||
if (err) { | ||
return done(err); | ||
} | ||
equal('test/fixtures/directory/expected', 'test/fixtures/directory/build'); | ||
@@ -67,3 +77,5 @@ done(); | ||
.build(function(err){ | ||
if (err) return done(err); | ||
if (err) { | ||
return done(err); | ||
} | ||
equal('test/fixtures/metadata/expected', 'test/fixtures/metadata/build'); | ||
@@ -78,3 +90,5 @@ done(); | ||
.build(function(err){ | ||
if (err) return done(err); | ||
if (err) { | ||
return done(err); | ||
} | ||
equal('test/fixtures/binary/expected', 'test/fixtures/binary/build'); | ||
@@ -85,2 +99,14 @@ done(); | ||
it('should process swig includes', function(done){ | ||
Metalsmith('test/fixtures/include') | ||
.use(layouts({ engine: 'swig' })) | ||
.build(function(err){ | ||
if (err) { | ||
return done(err); | ||
} | ||
equal('test/fixtures/include/expected', 'test/fixtures/include/build'); | ||
done(); | ||
}); | ||
}); | ||
it('should be capable of processing partials multiple times', function(done){ | ||
@@ -90,9 +116,13 @@ var instance = Metalsmith('test/fixtures/partials') | ||
engine: 'handlebars', | ||
partials: { nav: 'partials/nav'} | ||
partials: {nav: 'partials/nav'} | ||
})); | ||
instance.build(function(err){ | ||
if (err) return done(err); | ||
if (err) { | ||
return done(err); | ||
} | ||
instance.build(function(err){ | ||
if (err) return done(err); | ||
if (err) { | ||
return done(err); | ||
} | ||
equal('test/fixtures/partials/expected', 'test/fixtures/partials/build'); | ||
@@ -103,2 +133,18 @@ done(); | ||
}); | ||
it('should accept a partials option', function(done){ | ||
Metalsmith('test/fixtures/partials-option') | ||
.use(layouts({ | ||
engine: 'handlebars', | ||
partials: 'partials' | ||
})) | ||
.build(function(err){ | ||
if (err) { | ||
return done(err); | ||
} | ||
equal('test/fixtures/partials-option/expected', 'test/fixtures/partials-option/build'); | ||
done(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
248317
63
307
95
8
2
+ Addedfs-readdir-recursive@^0.1.2
+ Addedfs-readdir-recursive@0.1.2(transitive)