From fd451bf4d96c1138a2cbab59bd7ae798c706d7fe Mon Sep 17 00:00:00 2001 From: Anthony Short Date: Tue, 29 Jul 2014 15:45:15 -0700 Subject: [PATCH] Added the ability to add metadata to collections --- lib/index.js | 22 +++++++++++++ package.json | 3 +- test/fixtures/metadata/metadata.json | 3 ++ test/fixtures/metadata/metadata.yaml | 1 + test/index.js | 49 ++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/metadata/metadata.json create mode 100644 test/fixtures/metadata/metadata.yaml diff --git a/lib/index.js b/lib/index.js index 3230189..71aaa92 100644 --- a/lib/index.js +++ b/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 yaml = require('yaml-js'); /** * Expose `plugin`. @@ -90,6 +92,26 @@ function plugin(opts){ }); }); + /** + * Add collection metadata + */ + + keys.forEach(function(key){ + debug('adding metadata: %s', key); + var settings = opts[key]; + var col = metadata[key]; + var fileName = settings.metadata; + if (!fileName) return; + if (typeof fileName === 'string') { + var data = read(fileName, 'utf-8'); + var ext = fileName.substr(fileName.lastIndexOf('.') + 1); + col.metadata = ext === 'json' ? JSON.parse(data) : yaml.load(data); + } + else { + col.metadata = settings.metadata; + } + }); + /** * Add them grouped together to the global metadata. */ diff --git a/package.json b/package.json index 2f9b2e6..33fb125 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "debug": "~0.7.4", "extend": "~1.2.1", "minimatch": "^0.2.14", - "uniq": "0.0.2" + "uniq": "0.0.2", + "yaml-js": "0.0.7" }, "devDependencies": { "metalsmith": "0.x", diff --git a/test/fixtures/metadata/metadata.json b/test/fixtures/metadata/metadata.json new file mode 100644 index 0000000..563c57c --- /dev/null +++ b/test/fixtures/metadata/metadata.json @@ -0,0 +1,3 @@ +{ + "name": "Batman" +} \ No newline at end of file diff --git a/test/fixtures/metadata/metadata.yaml b/test/fixtures/metadata/metadata.yaml new file mode 100644 index 0000000..b23f583 --- /dev/null +++ b/test/fixtures/metadata/metadata.yaml @@ -0,0 +1 @@ +name: Batman \ No newline at end of file diff --git a/test/index.js b/test/index.js index 0f51a77..531522d 100644 --- a/test/index.js +++ b/test/index.js @@ -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(); + }); + }); + });