From 3eef703cdac9c9ca7da1145b8a81f13b6b6bfd34 Mon Sep 17 00:00:00 2001 From: Stefan Judis Date: Sun, 30 Oct 2016 14:43:36 +0100 Subject: [PATCH] feat: add support for global meta data in child files #34 (#35) --- index.js | 2 ++ lib/processor.js | 19 ++++++++++--------- lib/processor.spec.js | 7 ++++++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 2ea7d34..0fd04af 100644 --- a/index.js +++ b/index.js @@ -20,6 +20,8 @@ function plugin (options) { * @param {Function} done success callback */ return function (files, metalsmith, done) { + options.metadata = metalsmith.metadata() + return new Promise(resolve => { resolve(Object.keys(files)) }) diff --git a/lib/processor.js b/lib/processor.js index 38e8d17..e3f5163 100644 --- a/lib/processor.js +++ b/lib/processor.js @@ -90,16 +90,17 @@ function mapEntriesForFile (entries, file, options) { * * @param {Object} file file read by metalsmith * @param {Array} entries entries fetched from contentful + * @param {Object} options file options * * @return {Object} file mapping object */ -function processEntriesForFile (file, entries) { - const options = file.contentful +function processEntriesForFile (file, entries, options) { + const contentfulOptions = file.contentful const files = {} files[file._fileName] = file - if (options.entry_id) { + if (contentfulOptions.entry_id) { validator.validateSingleEntryForFile(entries[0], file) file.data = entries[0] @@ -116,20 +117,20 @@ function processEntriesForFile (file, entries) { file.data = { entries, contentTypes } } - if (options.entry_template) { + if (contentfulOptions.entry_template) { return entries.reduce((fileMap, entry) => { - fileMap[ entry._fileName ] = { + fileMap[ entry._fileName ] = Object.assign({ // `contents` need to be defined because there // might be other plugins that expect it contents: '', data: entry, id: entry.sys.id, - contentType: options.content_type, - layout: options.entry_template, + contentType: contentfulOptions.content_type, + layout: contentfulOptions.entry_template, _fileName: entry._fileName, _parentFileName: file._fileName - } + }, options.metadata) return fileMap }, files) @@ -163,7 +164,7 @@ function processFile (file, options) { return client.getEntries(query) .then(entries => mapEntriesForFile(entries.items, file, options)) - .then(entries => processEntriesForFile(file, entries)) + .then(entries => processEntriesForFile(file, entries, options)) .then(entries => getCommonContentForSpace(entries, options)) } diff --git a/lib/processor.spec.js b/lib/processor.spec.js index 904515d..9d1388e 100644 --- a/lib/processor.spec.js +++ b/lib/processor.spec.js @@ -178,7 +178,10 @@ test('processor.processFile - resolves correctly for multiple entries', t => { }, { access_token: 'global-bar', host: 'global-baz', - space_id: 'global-foo' + space_id: 'global-foo', + metadata: { + global: 'meta' + } }).then(fileMap => { t.deepEqual( fileMap['article/john-doe.html'], @@ -188,6 +191,7 @@ test('processor.processFile - resolves correctly for multiple entries', t => { id: entriesToBeReturned[0].sys.id, contentType: 'article', layout: 'template.html', + global: 'meta', _fileName: 'article/john-doe.html', _parentFileName: 'awesome-file.html' } @@ -201,6 +205,7 @@ test('processor.processFile - resolves correctly for multiple entries', t => { id: entriesToBeReturned[1].sys.id, contentType: 'article', layout: 'template.html', + global: 'meta', _fileName: 'article/jane-doe.html', _parentFileName: 'awesome-file.html' }