contentful-metalsmith
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -75,3 +75,3 @@ # Global settings | ||
--- | ||
title: Post overview of entries including "rabbit" | ||
title: Articles with certain start and end dates | ||
contentful: | ||
@@ -85,1 +85,27 @@ content_type: post | ||
``` | ||
### `common` *(optional)* | ||
The results of queries placed in this property will be made available in all templates | ||
For example, find your five latest entries so you can include them in various templates: | ||
```javascript | ||
{ | ||
// ... | ||
"common": { | ||
"latest": { | ||
"content_type": "post", | ||
"limit": 5, | ||
"order": "sys.createdAt" | ||
} | ||
}, | ||
// ... | ||
} | ||
``` | ||
In templates you can then access `common.latest` to get the raw results of the above query: | ||
``` | ||
The first latest post's title, in handlebars syntax: {{ common.latest.items.0.fields.title }} | ||
``` |
@@ -13,2 +13,3 @@ 'use strict' | ||
* @param {String} spaceId space id | ||
* @param {String} host host | ||
* | ||
@@ -30,2 +31,44 @@ * @return {Object} contentful client | ||
/** | ||
* Fetch common content for a certain space. | ||
* | ||
* @param {Object} entries entries fetched from contentful | ||
* @param {Object} options plugin config | ||
* | ||
* @return {Object} file mapping object with common content added | ||
*/ | ||
function getCommonContentForSpace (entries, options) { | ||
if (!options.common) { | ||
return entries | ||
} | ||
const client = getContentfulClient(options.access_token, options.space_id, options.host) | ||
const commonQueries = [] | ||
const commonIds = [] | ||
for (let id in options.common) { | ||
commonQueries.push(client.getEntries(util.getEntriesQuery(options.common[id], options.filterTransforms))) | ||
commonIds.push(id) | ||
} | ||
// First, execute all common queries. | ||
return Promise.all(commonQueries).then(commonContent => { | ||
return new Promise((resolve, reject) => { | ||
// Store the results in an object using the configured IDs. | ||
const commonsObj = commonIds.reduce((prev, curr, index) => { | ||
prev[curr] = commonContent[index] | ||
return prev | ||
}, {}) | ||
// Assign common query results to each entry. | ||
for (let entry in entries) { | ||
entries[entry].common = commonsObj | ||
} | ||
resolve(entries) | ||
}) | ||
}) | ||
} | ||
/** | ||
* Enrich all fetched entries with additional properties | ||
@@ -126,2 +169,3 @@ * | ||
.then(entries => processEntriesForFile(file, entries)) | ||
.then(entries => getCommonContentForSpace(entries, options)) | ||
} | ||
@@ -128,0 +172,0 @@ |
@@ -46,3 +46,3 @@ { | ||
}, | ||
"version": "0.4.0" | ||
"version": "0.5.0" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29601
378