Merge pull request #17 from segmentio/metadata

Added the ability to add metadata to collections
This commit is contained in:
Anthony Short 2014-08-04 18:29:58 -07:00
commit c2c3937747
5 changed files with 71 additions and 2 deletions

View file

@ -3,6 +3,8 @@ var debug = require('debug')('metalsmith-collections');
var extend = require('extend'); var extend = require('extend');
var Matcher = require('minimatch').Minimatch; var Matcher = require('minimatch').Minimatch;
var unique = require('uniq'); var unique = require('uniq');
var read = require('fs').readFileSync;
var loadMetadata = require('load-metadata').sync;
/** /**
* Expose `plugin`. * Expose `plugin`.
@ -90,6 +92,19 @@ function plugin(opts){
}); });
}); });
/**
* Add collection metadata
*/
keys.forEach(function(key){
debug('adding metadata: %s', key);
var settings = opts[key];
var col = metadata[key];
col.metadata = (typeof settings.metadata === 'string') ?
loadMetadata(settings.metadata) :
settings.metadata;
});
/** /**
* Add them grouped together to the global metadata. * Add them grouped together to the global metadata.
*/ */

View file

@ -9,10 +9,11 @@
"debug": "~0.7.4", "debug": "~0.7.4",
"extend": "~1.2.1", "extend": "~1.2.1",
"minimatch": "^0.2.14", "minimatch": "^0.2.14",
"uniq": "0.0.2" "uniq": "0.0.2",
"read-metadata": "0.0.2"
}, },
"devDependencies": { "devDependencies": {
"metalsmith": "0.x", "metalsmith": "0.x",
"mocha": "1.x" "mocha": "1.x"
} }
} }

3
test/fixtures/metadata/metadata.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"name": "Batman"
}

1
test/fixtures/metadata/metadata.yaml vendored Normal file
View file

@ -0,0 +1 @@
name: Batman

View file

@ -145,4 +145,53 @@ describe('metalsmith-collections', function(){
done(); done();
}); });
}); });
it('should add metadata objects to collections', function (done) {
var metalsmith = Metalsmith('test/fixtures/basic');
metalsmith
.use(collections({
articles: {
metadata: { name: 'Batman' }
}
}))
.build(function(err){
if (err) return done(err);
var m = metalsmith.metadata();
assert.equal('Batman', m.articles.metadata.name);
done();
});
});
it('should load collection metadata from a JSON file', function (done) {
var metalsmith = Metalsmith('test/fixtures/basic');
metalsmith
.use(collections({
articles: {
metadata: 'test/fixtures/metadata/metadata.json'
}
}))
.build(function(err){
if (err) return done(err);
var m = metalsmith.metadata();
assert.equal('Batman', m.articles.metadata.name);
done();
});
});
it('should load collection metadata from a YAML file', function (done) {
var metalsmith = Metalsmith('test/fixtures/basic');
metalsmith
.use(collections({
articles: {
metadata: 'test/fixtures/metadata/metadata.yaml'
}
}))
.build(function(err){
if (err) return done(err);
var m = metalsmith.metadata();
assert.equal('Batman', m.articles.metadata.name);
done();
});
});
}); });