mirror of
https://github.com/vale981/metalsmith-collections
synced 2025-03-05 09:21:39 -05:00
Merge pull request #24 from davidknezic/master
Allow collections through metadata alone
This commit is contained in:
commit
7dc8a7a580
7 changed files with 35 additions and 1 deletions
|
@ -63,6 +63,7 @@ My article contents...
|
|||
```
|
||||
|
||||
All of the files with a matching `collection` will be added to an array that is exposed as a key of the same name on the global Metalsmith `metadata`.
|
||||
You can omit passing any options to the plugin when matching based on a `collection` property.
|
||||
|
||||
### Collection Metadata
|
||||
|
||||
|
|
10
lib/index.js
10
lib/index.js
|
@ -36,6 +36,11 @@ function plugin(opts){
|
|||
debug('checking file: %s', file);
|
||||
var data = files[file];
|
||||
match(file, data).forEach(function(key){
|
||||
if (key && keys.indexOf(key) < 0){
|
||||
opts[key] = {};
|
||||
keys.push(key);
|
||||
}
|
||||
|
||||
metadata[key] = metadata[key] || [];
|
||||
metadata[key].push(data);
|
||||
});
|
||||
|
@ -158,7 +163,10 @@ function matcher(cols){
|
|||
var collection = data.collection;
|
||||
if (!Array.isArray(collection)) collection = [collection];
|
||||
collection.forEach(function(key){
|
||||
if (key && ~keys.indexOf(key)) matches.push(key);
|
||||
matches.push(key);
|
||||
|
||||
if (key && keys.indexOf(key) < 0)
|
||||
debug('adding new collection through metadata: %s', key);
|
||||
});
|
||||
}
|
||||
else {
|
||||
|
|
3
test/fixtures/noconfig/src/four.md
vendored
Normal file
3
test/fixtures/noconfig/src/four.md
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
collection: books
|
||||
---
|
3
test/fixtures/noconfig/src/one.md
vendored
Normal file
3
test/fixtures/noconfig/src/one.md
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
collection: books
|
||||
---
|
3
test/fixtures/noconfig/src/three.md
vendored
Normal file
3
test/fixtures/noconfig/src/three.md
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
collection: movies
|
||||
---
|
1
test/fixtures/noconfig/src/two.md
vendored
Normal file
1
test/fixtures/noconfig/src/two.md
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
Nothing
|
|
@ -211,4 +211,19 @@ describe('metalsmith-collections', function(){
|
|||
});
|
||||
});
|
||||
|
||||
it('should allow collections through metadata alone', function (done) {
|
||||
var metalsmith = Metalsmith('test/fixtures/noconfig');
|
||||
metalsmith
|
||||
.use(collections({ movies: {} }))
|
||||
.build(function(err){
|
||||
if (err) return done(err);
|
||||
var m = metalsmith.metadata();
|
||||
assert.equal(2, m.books.length);
|
||||
assert.equal(1, m.movies.length);
|
||||
assert.equal(m.collections.books, m.books);
|
||||
assert.equal(m.collections.movies, m.movies);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue