/contentful-metalsmith

A plugin for Metalsmith that pulls content from the Contentful API

Primary LanguageJavaScript

contentful-metalsmith

Build Status Coverage Status

A Metalsmith' plugin to generate files using content from Contentful

About

This plugin for metalsmith allows you to build a static site using the data stored at Contentful. It is built on top of the Contentful JavaScript Client.

Example

To get an idea on how this works, you can check out an example blog site which is completely generated using contentful-metalsmith.

Getting started

Install

$ npm install contentful-metalsmith --save

Configure required globals

When using the metalsmith cli you can edit your metalsmith.json and add contentful-metalsmith in the plugins section.

// metalsmith.json

{
  "source": "src",
  "destination": "build",
  "plugins": {
    "contentful-metalsmith": {
      "access_token": "YOUR_CONTENTFUL_ACCESS_TOKEN",
      "space_id": "YOUR_CONTENTFUL_SPACE_ID",
      "entry_key": "_key",
      "entry_extension": "md",
      "contentful": {
        "content_type": "2wKn6yEnZewu2SCCkus4as"
      }
    }
  }
}

When you use the JavaScript Api add contentful-metalsmith to the used plugins.

metalsmith.source('src')
metalsmith.destination('build')

metalsmith.use(require('contentful-metalsmith')({
	'access_token': 'YOUR_CONTENTFUL_ACCESS_TOKEN',
	'entry_key': '_key',
	'entry_extension': 'md',
	'contentful': {
		'content_type': '2wKn6yEnZewu2SCCkus4as'
	}
}))

Global parameters:

  • acccess_token
  • space_id
  • entry_key
  • entry_extension

You can find the access_token and space_id in your app at APIs -> Content delivery API Keys.


To read more on all global parameters and settings read the global settings documentation.

Create files based on the files defined in your source folder

We're considering that you use metalsmith-layouts for file rendering. That for the layout key is used for rendered source files and child templates.

source/posts.html

---
title: metalsmith-contentful file
contentful:
  content_type: post
  layout: post.html
layout: posts.html
---

[OPTIONAL CONTENT]

layouts/posts.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>

layouts/post.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>

  {{contents}}
</body>
</html>

This example will

  • 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

To read more on source file parameters and settings read the source file documentation.

License

MIT