Multimatch: Added

Updated:
- Packages
- Unit tests
- Documentation
- To use npm scripts - npm test will run tests
This commit is contained in:
Andrew Goodricke 2016-10-29 19:22:50 -03:00
parent caa1c45423
commit 8c36d634bd
7 changed files with 52 additions and 25 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
node_modules node_modules
test/**/build

View file

@ -1,8 +0,0 @@
node_modules: package.json
@npm install
test: node_modules
@./node_modules/.bin/mocha --reporter spec
.PHONY: test

View file

@ -18,7 +18,7 @@ A [Metalsmith](https://github.com/segmentio/metalsmith) plugin that lets you gro
There are two ways to create collections: There are two ways to create collections:
- **by pattern** - this is just passing a globing pattern that will group any files that match into the same collection. - **by pattern** - this is just passing a globing pattern that will group any files that match into the same collection. The passed pattern can be a single pattern (as a string) or an array of globing patterns. For more information read the [multimatch patterns documentation](https://www.npmjs.com/package/multimatch#how-multiple-patterns-work).
- **by metadata** - this is adding a specific `collection` metadata field to each item that you want to add to a collection. - **by metadata** - this is adding a specific `collection` metadata field to each item that you want to add to a collection.
The simplest way to create a collection is to use a pattern to match the files you want to group together: The simplest way to create a collection is to use a pattern to match the files you want to group together:

View file

@ -1,7 +1,7 @@
var debug = require('debug')('metalsmith-collections'); var debug = require('debug')('metalsmith-collections');
var extend = require('extend'); var extend = require('extend');
var Matcher = require('minimatch').Minimatch; var multimatch = require('multimatch');
var unique = require('uniq'); var unique = require('uniq');
var read = require('fs').readFileSync; var read = require('fs').readFileSync;
var loadMetadata = require('read-metadata').sync; var loadMetadata = require('read-metadata').sync;
@ -143,6 +143,7 @@ function normalize(options){
for (var key in options) { for (var key in options) {
var val = options[key]; var val = options[key];
if ('string' == typeof val) options[key] = { pattern: val }; if ('string' == typeof val) options[key] = { pattern: val };
if (val instanceof Array) options[key] = { pattern: val };
} }
return options; return options;
@ -161,8 +162,14 @@ function matcher(cols){
keys.forEach(function(key){ keys.forEach(function(key){
var opts = cols[key]; var opts = cols[key];
if (!opts.pattern) return; if (!opts.pattern) {
matchers[key] = new Matcher(opts.pattern); return;
}
matchers[key] = {
match: function(file) {
return multimatch(file, opts.pattern)
}
};
}); });
return function(file, data){ return function(file, data){
@ -170,18 +177,23 @@ function matcher(cols){
if (data.collection) { if (data.collection) {
var collection = data.collection; var collection = data.collection;
if (!Array.isArray(collection)) collection = [collection]; if (!Array.isArray(collection)) {
collection = [collection];
}
collection.forEach(function(key){ collection.forEach(function(key){
matches.push(key); matches.push(key);
if (key && keys.indexOf(key) < 0) if (key && keys.indexOf(key) < 0) {
debug('adding new collection through metadata: %s', key); debug('adding new collection through metadata: %s', key);
}
}); });
} }
for (var key in matchers){ for (var key in matchers){
var m = matchers[key]; var m = matchers[key];
if (m && m.match(file)) matches.push(key); if (m.match(file).length) {
matches.push(key);
}
} }
data.collection = unique(matches); data.collection = unique(matches);

View file

@ -6,14 +6,18 @@
"license": "MIT", "license": "MIT",
"main": "lib/index.js", "main": "lib/index.js",
"dependencies": { "dependencies": {
"debug": "~0.7.4", "debug": "^2.2.0",
"extend": "~1.2.1", "extend": "^3.0.0",
"minimatch": "^0.2.14", "multimatch": "^2.1.0",
"uniq": "0.0.2", "read-metadata": "^1.0.0",
"read-metadata": "0.0.2" "uniq": "^1.0.1"
}, },
"devDependencies": { "devDependencies": {
"metalsmith": "0.x", "metalsmith": "2.x",
"mocha": "1.x" "mocha": "^3.1.2"
},
"scripts": {
"pretest": "npm install --silent --progress=false",
"test": "mocha --reporter spec"
} }
} }

1
test/fixtures/pattern/src/four.md vendored Normal file
View file

@ -0,0 +1 @@
4

View file

@ -27,7 +27,7 @@ describe('metalsmith-collections', function(){
})) }))
.build(function(err){ .build(function(err){
if (err) return done(err); if (err) return done(err);
assert.equal(3, metalsmith.metadata().articles.length); assert.equal(4, metalsmith.metadata().articles.length);
done(); done();
}); });
}); });
@ -40,7 +40,24 @@ describe('metalsmith-collections', function(){
})) }))
.build(function(err){ .build(function(err){
if (err) return done(err); if (err) return done(err);
assert.equal(3, metalsmith.metadata().articles.length); assert.equal(4, metalsmith.metadata().articles.length);
done();
});
});
it('should take an array of patterns', function(done){
var metalsmith = Metalsmith('test/fixtures/pattern');
metalsmith
.use(collections({
blogs: ['*.md', '!one.md', '!two.md', '!four.md'],
pages: { pattern: ['four.md'] }
}))
.build(function(err, files){
if (err) return done(err);
assert.equal(1, metalsmith.metadata().blogs.length, 'length blogs');
assert.equal(1, metalsmith.metadata().pages.length, 'length page');
assert.equal(files['three.md'].collection, 'blogs', 'collection blogs');
assert.equal(files['four.md'].collection, 'pages', 'collection page');
done(); done();
}); });
}); });