Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

contentful-metalsmith

Package Overview
Dependencies
Maintainers
5
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

contentful-metalsmith - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

.eslintrc

49

package.json
{
"name": "contentful-metalsmith",
"version": "0.1.0",
"description": "A Metalsmith's plugin to get content from Contentful",
"main": "lib/index.js",
"main": "index.js",
"repository": {
"type": "git",
"url": "git@github.com:contentful/contentful-metalsmith.git"
"url": "https://github.com/contentful-labs/contentful-metalsmith.git"
},
"author": "Contentful GmbH",
"license": "MIT",
"bugs": {
"url": "https://github.com/contentful/contentful-metalsmith/issues"
"engines": {
"node": ">=4"
},
"homepage": "https://github.com/contentful/contentful-metalsmith",
"dependencies": {
"async": "^0.9.0",
"contentful": "^0.1.2",
"debug": "^1.0.3",
"lodash": "^2.4.1"
}
}
"contentful": "^3.5.0",
"lodash.pick": "^4.4.0",
"lodash.template": "^4.4.0",
"slug-component": "^1.1.0"
},
"devDependencies": {
"ava": "^0.16.0",
"coveralls": "^2.11.12",
"cz-conventional-changelog": "^1.2.0",
"eslint": "^3.3.1",
"eslint-config-standard": "^6.0.0-beta.3",
"eslint-plugin-promise": "^2.0.1",
"eslint-plugin-standard": "^2.0.0",
"handlebars": "^4.0.5",
"metalsmith": "^2.2.0",
"metalsmith-layouts": "^1.6.5",
"nyc": "^8.1.0",
"proxyquire": "^1.7.10",
"semantic-release": "^4.3.5"
},
"scripts": {
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"lint": "eslint \"**/*.js\"",
"test": "npm run lint && nyc ava **/*.spec.js",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"version": "0.2.0"
}
# contentful-metalsmith
[![Build Status](https://travis-ci.org/contentful-labs/contentful-metalsmith.svg?branch=master)](https://travis-ci.org/contentful-labs/contentful-metalsmith)
[![Coverage Status](https://coveralls.io/repos/github/contentful-labs/contentful-metalsmith/badge.svg?branch=refactor)](https://coveralls.io/github/contentful-labs/contentful-metalsmith?branch=refactor)
A Metalsmith's plugin to get content from [Contentful](http://www.contentful.com)
A Metalsmith' plugin to generate files using content from [Contentful](http://www.contentful.com)
## About
This plugin for [metalsmith](http://www.metalsmith.io) allows you to build a static site using the data stored at [Contentful](http://www.contentful.com). This
plugin is built on top of the [Contentful JavaScript Client](https://github.com/contentful/contentful.js).
This plugin for [metalsmith](http://www.metalsmith.io) allows you to build a static site using the data stored at [Contentful](http://www.contentful.com). It is built on top of the [Contentful JavaScript Client](https://github.com/contentful/contentful.js).
## TL;DR
## Getting started
1. Install it
### Install
```javascript
$ npm install contentful-metalsmith
```
2. Configure it (example for [metalsmith CLI](https://github.com/segmentio/metalsmith#cli))
```bash
$ npm install contentful-metalsmith
```
```javascript
$ vim metalsmith.json
### Configure required globals
----
When you use metalsmith using the [cli](https://github.com/metalsmith/metalsmith#cli) edit your `metalsmith.json` and add `contentful-metalsmith` in the plugins section.
{
"source": "src",
"destination": "build",
"plugins": {
...,
"contentful-metalsmith": {
"accessToken" : "YOUR_CONTENTFUL_ACCESS_TOKEN"
},
...
}
```javascript
// metalsmith.json
{
"source": "src",
"destination": "build",
"plugins": {
"contentful-metalsmith": {
"access_token": "YOUR_CONTENTFUL_ACCESS_TOKEN",
"space_id": "YOUR_CONTENTFUL_SPACE_ID"
}
```
}
}
```
3. Create a source file
When you use the [JavaScript Api](https://github.com/metalsmith/metalsmith#api) add `contentful-metalsmith` to the used plugins.
```html
```javascript
metalsmith.source('src')
metalsmith.destination('build')
---
title: OMG metalsmith-contentful
contentful:
space_id: AN_SPACE_ID
template: entries.html
---
metalsmith.use(require('contentful-metalsmith')({ 'access_token' : 'YOUR_CONTENTFUL_ACCESS_TOKEN' }))
```
[OPTIONAL CONTENT]
```
**Global parameters:**
4. Create the template (handlebarsjs on this case)
- `acccess_token`
- `space_id`
```html
<!doctype html>
You can find the `access_token` and `space_id` in your [app](https://app.contentful.com) at `APIs -> Content delivery API Keys`.
<html lang="en">
<head>
<meta charset="utf-8">
------------------------------
<title>Contentful-metalsmith plugin example</title>
<meta name="description" content="No description">
<meta name="author" content="Contentful">
To read more on all global parameters and settings read the [global settings documentation](./docs/global-settings.md).
</head>
### Create files based on the files defined in your source folder
<body>
We're considering that you use [metalsmith-layouts](https://github.com/superwolff/metalsmith-layouts) for file rendering. That for the `layout` key is used for rendered source files and child templates.
{{contents}}
<table>
<thead>
<tr>
<th>Type</th>
<th>Id</th>
<th>Updated</th>
</tr>
</thead>
<tbody>
{{#each contentful.entries}}
<tr>
<td>
{{this.data.sys.contentType.sys.id}}
</td>
<td>
{{this.data.sys.id}}
</td>
<td>
{{this.data.sys.updatedAt}}
</td>
</tr>
{{/each}}
</tbody>
</table>
</body>
</html>
```
*`source/posts.html`*
5. Enjoy
```markdown
---
title: metalsmith-contentful file
contentful:
content_type: post
layout: post.html
layout: posts.html
---
# Usage
[OPTIONAL CONTENT]
```
The first thing that you have to do to use this plugin is to install and configure it (see the TL;DR section for that). Once you have done this you can create and setup source files to fetch data from [Contentful](http://www.contentful.com).
*`layouts/posts.html`*
## Setup a source file
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{title}}</title>
<meta name="description" content="No description">
<meta name="author" content="Contentful">
<link rel="stylesheet" href="scss/style.css?v=1.0">
</head>
<body>
<ul>
<!-- available data fetched from contentful -->
{{#each data.entries }}
<li>
<h2>{{fields.title}}</h2>
<p>{{fields.description}}</p>
<p><a href="{{_fileName}}">Read more</a></p>
</li>
{{/each}}
</ul>
{{contents}}
</body>
</html>
```
To fetch data from [Contentful](http://www.contentful.com) you have to include some extra metadata in a metalsmith source file. The available configuration parameters are the following:
*`layouts/post.html`*
* `space_id` (**required**), the id of the space from where you want to get entries.
* `entry_template` (optional), the template that will be used to render each individual entry.
* `filter` (optional), this parameter has to include some of the [filtering options](https://www.contentful.com/developers/documentation/content-delivery-api/http/#search) offered by the [Contentful's Content Delivery API](https://www.contentful.com/developers/documentation/content-delivery-api/).
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{data.fields.title}}</title>
<meta name="description" content="No description">
<meta name="author" content="Contentful">
<link rel="stylesheet" href="scss/style.css?v=1.0">
</head>
<body>
<h1>{{data.fields.title}}<h1>
<p>{{data.fields.description}}</p>
All this parameters have to be nested under the key `contentful`.
An example:
```yaml
---
title: OMG metalsmith-contentful
contentful:
space_id: cfexampleapi
content_type: cat
filter:
sys.id[in]: 'finn,jake'
entry_template: entry.html
template: example.html
---
{{contents}}
</body>
</html>
```
## Using the fetched entries on the templates
**This example will**
We have to make a distinction between two types of templates:
- render `posts.html` providing data of the entries of content type `post`
- render several single files with the template `post.html` providing the data of a particular post
* The template rendered for the source file.
* And the template rendered for each individual entry.
------------------------------
In the context of the template rendered for the source file you will have access to a property named `contentful`. This property holds the following properties:
To read more on source file parameters and settings read the [source file documentation](./docs/source-file-settings.md).
* `entries`, an array with all the fetched entries. The structure of each of this entry objects will be the same as the explained below for the entry template.
* `contentTypes`, an object with the id of the fetched [contentTypes](https://www.contentful.com/developers/documentation/content-delivery-api/http/#content-types) as keys. Under each key there will be an array with all the entries belonging to that particular contentType.
In the context of the template rendered for an individual entry you will have access to the following properties under the property `data`:
* `id`, a shortcut to the entry's id.
* `contentType`, a shortcut to the entry's contentType.
* `data`, the body of the entry as returned by [Contentful's Content Delivery API](https://www.contentful.com/developers/documentation/content-delivery-api/)
# License
MIT
MIT

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc