mirror of
https://github.com/vale981/metalsmith-collections
synced 2025-03-04 17:01:41 -05:00
Merge pull request #17 from segmentio/metadata
Added the ability to add metadata to collections
This commit is contained in:
commit
c2c3937747
5 changed files with 71 additions and 2 deletions
15
lib/index.js
15
lib/index.js
|
@ -3,6 +3,8 @@ var debug = require('debug')('metalsmith-collections');
|
|||
var extend = require('extend');
|
||||
var Matcher = require('minimatch').Minimatch;
|
||||
var unique = require('uniq');
|
||||
var read = require('fs').readFileSync;
|
||||
var loadMetadata = require('load-metadata').sync;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
|
|
@ -9,10 +9,11 @@
|
|||
"debug": "~0.7.4",
|
||||
"extend": "~1.2.1",
|
||||
"minimatch": "^0.2.14",
|
||||
"uniq": "0.0.2"
|
||||
"uniq": "0.0.2",
|
||||
"read-metadata": "0.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"metalsmith": "0.x",
|
||||
"mocha": "1.x"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
3
test/fixtures/metadata/metadata.json
vendored
Normal file
3
test/fixtures/metadata/metadata.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"name": "Batman"
|
||||
}
|
1
test/fixtures/metadata/metadata.yaml
vendored
Normal file
1
test/fixtures/metadata/metadata.yaml
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
name: Batman
|
|
@ -145,4 +145,53 @@ describe('metalsmith-collections', function(){
|
|||
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();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue