diff --git a/.gitignore b/.gitignore index b512c09..0599470 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +test/**/build \ No newline at end of file diff --git a/Makefile b/Makefile deleted file mode 100644 index e367278..0000000 --- a/Makefile +++ /dev/null @@ -1,8 +0,0 @@ - -node_modules: package.json - @npm install - -test: node_modules - @./node_modules/.bin/mocha --reporter spec - -.PHONY: test \ No newline at end of file diff --git a/Readme.md b/Readme.md index 43374fb..b5041a8 100644 --- a/Readme.md +++ b/Readme.md @@ -18,7 +18,7 @@ A [Metalsmith](https://github.com/segmentio/metalsmith) plugin that lets you gro 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. The simplest way to create a collection is to use a pattern to match the files you want to group together: diff --git a/lib/index.js b/lib/index.js index 8e9b6a2..153e00b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,7 +1,7 @@ var debug = require('debug')('metalsmith-collections'); var extend = require('extend'); -var Matcher = require('minimatch').Minimatch; +var multimatch = require('multimatch'); var unique = require('uniq'); var read = require('fs').readFileSync; var loadMetadata = require('read-metadata').sync; @@ -143,6 +143,7 @@ function normalize(options){ for (var key in options) { var val = options[key]; if ('string' == typeof val) options[key] = { pattern: val }; + if (val instanceof Array) options[key] = { pattern: val }; } return options; @@ -161,8 +162,14 @@ function matcher(cols){ keys.forEach(function(key){ var opts = cols[key]; - if (!opts.pattern) return; - matchers[key] = new Matcher(opts.pattern); + if (!opts.pattern) { + return; + } + matchers[key] = { + match: function(file) { + return multimatch(file, opts.pattern) + } + }; }); return function(file, data){ @@ -170,18 +177,23 @@ function matcher(cols){ if (data.collection) { var collection = data.collection; - if (!Array.isArray(collection)) collection = [collection]; + if (!Array.isArray(collection)) { + collection = [collection]; + } collection.forEach(function(key){ matches.push(key); - if (key && keys.indexOf(key) < 0) + if (key && keys.indexOf(key) < 0) { debug('adding new collection through metadata: %s', key); + } }); } for (var key in matchers){ var m = matchers[key]; - if (m && m.match(file)) matches.push(key); + if (m.match(file).length) { + matches.push(key); + } } data.collection = unique(matches); diff --git a/package.json b/package.json index 10262f9..4e5fbd7 100644 --- a/package.json +++ b/package.json @@ -6,14 +6,18 @@ "license": "MIT", "main": "lib/index.js", "dependencies": { - "debug": "~0.7.4", - "extend": "~1.2.1", - "minimatch": "^0.2.14", - "uniq": "0.0.2", - "read-metadata": "0.0.2" + "debug": "^2.2.0", + "extend": "^3.0.0", + "multimatch": "^2.1.0", + "read-metadata": "^1.0.0", + "uniq": "^1.0.1" }, "devDependencies": { - "metalsmith": "0.x", - "mocha": "1.x" + "metalsmith": "2.x", + "mocha": "^3.1.2" + }, + "scripts": { + "pretest": "npm install --silent --progress=false", + "test": "mocha --reporter spec" } } \ No newline at end of file diff --git a/test/fixtures/pattern/src/four.md b/test/fixtures/pattern/src/four.md new file mode 100644 index 0000000..bf0d87a --- /dev/null +++ b/test/fixtures/pattern/src/four.md @@ -0,0 +1 @@ +4 \ No newline at end of file diff --git a/test/index.js b/test/index.js index 6763e7a..6eb93b4 100644 --- a/test/index.js +++ b/test/index.js @@ -27,7 +27,7 @@ describe('metalsmith-collections', function(){ })) .build(function(err){ if (err) return done(err); - assert.equal(3, metalsmith.metadata().articles.length); + assert.equal(4, metalsmith.metadata().articles.length); done(); }); }); @@ -40,7 +40,24 @@ describe('metalsmith-collections', function(){ })) .build(function(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(); }); });