roots-contentful
Advanced tools
Comparing version 0.0.8 to 0.0.9
115
lib/index.js
@@ -1,4 +0,4 @@ | ||
// Generated by CoffeeScript 1.9.3 | ||
// Generated by CoffeeScript 1.10.0 | ||
(function() { | ||
var RootsUtil, S, W, _, contentful, errors, path, pluralize, querystring; | ||
var RootsUtil, S, W, _, contentful, errors, hosts, path, pluralize, querystring; | ||
@@ -27,2 +27,7 @@ _ = require('lodash'); | ||
hosts = { | ||
develop: 'preview.contentful.com', | ||
production: 'cdn.contentful.com' | ||
}; | ||
module.exports = function(opts) { | ||
@@ -34,2 +39,3 @@ var RootsContentful, client; | ||
client = contentful.createClient({ | ||
host: hosts[process.env.CONTENTFUL_ENV] || (opts.preview ? hosts.develop : void 0) || hosts.production, | ||
accessToken: opts.access_token, | ||
@@ -39,3 +45,3 @@ space: opts.space_id | ||
return RootsContentful = (function() { | ||
var asset_view_helper, compile_entries, configure_content, fetch_content, format_content, format_entry, get_all_content, reconfigure_alt_type_config, set_locals, set_urls; | ||
var asset_view_helper, compile_entries, configure_content, fetch_content, format_content, format_entry, get_all_content, reconfigure_alt_type_config, set_locals, set_urls, sort_entries, transform_entries, write_entries; | ||
@@ -56,3 +62,3 @@ function RootsContentful(roots) { | ||
RootsContentful.prototype.setup = function() { | ||
return configure_content(opts.content_types)["with"](this).then(get_all_content).tap(set_urls).tap(set_locals).tap(compile_entries); | ||
return configure_content(opts.content_types)["with"](this).then(get_all_content).tap(set_urls).then(transform_entries).then(sort_entries).tap(set_locals).tap(compile_entries).tap(write_entries); | ||
}; | ||
@@ -122,9 +128,7 @@ | ||
get_all_content = function(types) { | ||
return W.map(types, (function(_this) { | ||
return function(t) { | ||
return fetch_content(t).then(format_content).then(function(c) { | ||
return t.content = c; | ||
})["yield"](t); | ||
}; | ||
})(this)); | ||
return W.map(types, function(t) { | ||
return fetch_content(t).then(format_content).then(function(c) { | ||
return t.content = c; | ||
})["yield"](t); | ||
}); | ||
}; | ||
@@ -173,3 +177,5 @@ | ||
/** | ||
* Sets `_url` property on content with single entry views | ||
* Sets `_url` and `_urls` properties on content with single entry views | ||
* `_url` takes the value `null` if the content type's custom path function | ||
* returns multiple paths | ||
* @param {Array} types - content type objects | ||
@@ -183,3 +189,17 @@ * return {Promise} - promise when urls are set | ||
return W.map(t.content, function(entry) { | ||
return entry._url = "/" + (t.path(entry)) + ".html"; | ||
var p, paths; | ||
paths = t.path(entry); | ||
if (_.isString(paths)) { | ||
paths = [paths]; | ||
} | ||
entry._urls = (function() { | ||
var i, len, results; | ||
results = []; | ||
for (i = 0, len = paths.length; i < len; i++) { | ||
p = paths[i]; | ||
results.push("/" + p + ".html"); | ||
} | ||
return results; | ||
})(); | ||
return entry._url = entry._urls.length === 1 ? entry._urls[0] : null; | ||
}); | ||
@@ -207,2 +227,42 @@ } | ||
/** | ||
* Transforms every type with content with the user provided callback | ||
* @param {Array} types - Populated content type objects | ||
* @return {Promise} - promise for when compilation is finished | ||
*/ | ||
transform_entries = function(types) { | ||
return W.map(types, (function(_this) { | ||
return function(t) { | ||
if (t.transform) { | ||
W.map(t.content, function(entry) { | ||
return W(entry, t.transform); | ||
}); | ||
} | ||
return W.resolve(t); | ||
}; | ||
})(this)); | ||
}; | ||
/** | ||
* Sort every type content with the user provided callback | ||
* @param {Array} types - Populated content type objects | ||
* @return {Promise} - promise for when compilation is finished | ||
*/ | ||
sort_entries = function(types) { | ||
return W.map(types, (function(_this) { | ||
return function(t) { | ||
if (t.sort) { | ||
W.all(t.content).then(function(data) { | ||
return t.content = data.sort(t.sort); | ||
}); | ||
} | ||
return W.resolve(t); | ||
}; | ||
})(this)); | ||
}; | ||
/** | ||
* Compiles single entry views for content types | ||
@@ -222,8 +282,13 @@ * @param {Array} types - Populated content type objects | ||
template = path.join(_this.roots.root, t.template); | ||
_this.roots.config.locals.entry = entry; | ||
compiler = _.find(_this.roots.config.compilers, function(c) { | ||
return _.contains(c.extensions, path.extname(template).substring(1)); | ||
}); | ||
return compiler.renderFile(template, _this.roots.config.locals).then(function(res) { | ||
return _this.util.write((t.path(entry)) + ".html", res.result); | ||
return W.map(entry._urls, function(url) { | ||
_this.roots.config.locals.entry = _.assign({}, entry, { | ||
_url: url | ||
}); | ||
return compiler.renderFile(template, _this.roots.config.locals).then(function(res) { | ||
_this.roots.config.locals.entry = null; | ||
return _this.util.write(url, res.result); | ||
}); | ||
}); | ||
@@ -237,2 +302,20 @@ }); | ||
/** | ||
* Writes all data for type with content as json | ||
* @param {Array} types - Populated content type objects | ||
* @return {Promise} - promise for when compilation is finished | ||
*/ | ||
write_entries = function(types) { | ||
return W.map(types, (function(_this) { | ||
return function(t) { | ||
if (!t.write) { | ||
return W.resolve(); | ||
} | ||
return _this.util.write(t.write, JSON.stringify(t.content)); | ||
}; | ||
})(this)); | ||
}; | ||
/** | ||
* View helper for accessing the actual url from a Contentful asset | ||
@@ -239,0 +322,0 @@ * and appends any query string params |
{ | ||
"name": "roots-contentful", | ||
"description": "An extension for using roots with the API-driven Contentful CMS", | ||
"version": "0.0.8", | ||
"author": "joshrowley", | ||
"version": "0.0.9", | ||
"author": "Carrot Creative", | ||
"bugs": { | ||
@@ -18,10 +18,10 @@ "url": "https://github.com/carrot/roots-contentful/issues" | ||
"devDependencies": { | ||
"chai": "2.x", | ||
"chai-as-promised": "5.0.x", | ||
"coffee-script": "1.9.x", | ||
"chai": "3.4.1", | ||
"chai-as-promised": "5.1.0", | ||
"coffee-script": "1.10.0", | ||
"coveralls": "2.x", | ||
"istanbul": "0.3.x", | ||
"mocha": "2.2.x", | ||
"istanbul": "0.4.1", | ||
"mocha": "2.3.4", | ||
"mockery": "1.4.x", | ||
"roots": "3.0.x" | ||
"roots": "3.1.0" | ||
}, | ||
@@ -28,0 +28,0 @@ "directories": { |
@@ -39,2 +39,5 @@ # Roots Contentful | ||
path: (e) -> "blogging/#{e.category}/#{slugify(e.title)}" | ||
write: 'data.json' | ||
sort: compareFunction | ||
transform: transformFunction | ||
press_links: | ||
@@ -101,2 +104,6 @@ id: 'xxxxxx' | ||
#### preview | ||
Optional. (Boolean) Allows you use the Contentful Preview API. Also able to be accessed by setting the environment variable `CONTENTFUL_ENV` to `"develop"` (preview api) or `"production"` (default cdn). | ||
#### content_types | ||
@@ -108,3 +115,3 @@ | ||
### Configuring a `content_type` | ||
### Configuring a `content_type` | ||
Each object in the content_types array can have the following properties: | ||
@@ -132,3 +139,37 @@ | ||
If the function returns an array of paths, the given entry is compiled to multiple files. The array of paths can be accessed with `entry._urls`. For instance, the configuration below outputs each blog post entry into two folders, `en` and `fr`, for i18n purposes: | ||
```coffee | ||
content_types: | ||
blog_posts: | ||
id: 'xxxxxx' | ||
template: 'views/_post.jade' | ||
path: (e) -> ("#{lang}/#{slugify(e.title)}" for lang in ['en', 'fr']) | ||
``` | ||
#### write | ||
Optional. Provide the relative path to the output file that will hold the JSON data of the current content type. The top level JSON object will be an array. | ||
#### transform | ||
Optional. Provide a function to transform (map) every entry in a content type. The transformed data is then added to the jade locals variable and written to JSON (if the write property is provided). Transform can also return a [when.js](https://github.com/cujojs/when) promise. | ||
```coffee | ||
transformFunction = (entry) -> | ||
delete entry.myProperty | ||
entry | ||
``` | ||
#### sort | ||
Optional. Provide a standard compare function that will sort all the data of a content type. The data is sorted __after__ it is transformed and __before__ it is provided to the jade locals variable or written as JSON. | ||
```coffee | ||
compareFunction = (a, b) -> | ||
# 0 => a and b are equal | ||
# -1 or less => a is before b | ||
# 1 or more => a is after b | ||
a.number - b.number | ||
``` | ||
### Asset Image Helper | ||
@@ -135,0 +176,0 @@ |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
287
184
24271
7
1