Skip to content
This repository has been archived by the owner on Nov 26, 2021. It is now read-only.

Commit

Permalink
feat: add support for global meta data in child files #34 (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanjudis authored Oct 30, 2016
1 parent 3b0ab12 commit 3eef703
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand Down
19 changes: 10 additions & 9 deletions lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
Expand Down Expand Up @@ -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))
}

Expand Down
7 changes: 6 additions & 1 deletion lib/processor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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'
}
Expand All @@ -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'
}
Expand Down

0 comments on commit 3eef703

Please sign in to comment.