Update: This project has not been maintained for some time and won't be maintained moving forward. While it might work with some older versions of metalsmith, it's known not to work with more recent versions.
Until now, you had to be a quite tech savvy person to manage static pages. We just changed that by integrating Contentful with Metalsmith.
Creating static sites, however, has a downside: they are tedious to create and maintain. For example, any time you change the content of one entry on Contentful you would have to go and edit the static site with data related to that entry. If you are facing this scenario, worry no more!
The plugin we built is pretty simple and doesn't require much configuration. You will need a Contentful account and at least one Space. Once you have created your account and space you will need an accessToken for that space so that the plugin can fetch its entries. This accessToken will allow the plugin to use the Content Delivery API to get data from Contentful.
With the accessToken in your possession, the next step is to configure the plugin. If you use the Metalsmith CLI you will have to do this:
Once you have configured the plugin you are good to create the source files that Metalsmith will pass to the enabled plugins to generate the static site. This source files have to have the following layout:
The FILE_METADATA part contains information that could be used by the plugins to transform the source files into the resulting static sites. The CONTENTS part is, as its name suggests, content that will be available to the plugins and that could be added to the resulting static sites. In our case we will extend the FILE_METADATA with information related to the data stored in Contentful. This information is used by the plugin to know which data to fetch using the Content Delivery API.
In its simplest form the FILE_METADATA required by the plugin includes only the spaceId. In this case the plugin will fetch all the entries from the given space. See example below:
All the space entries fetched will be available to the template used to render the source file in the
contentful.entries context property. This source file and its template are examples of such configuration.
Besides the spaceId there are other optional configuration parameters that you can use in each source file. These configuration parameters include:
The nice benefit of using Metalsmith is that is really easy for you to build new stuff on top of our plugin. All the plugins that are run after contentful-metalsmith will have access to all the information (entries, meta information, etc.) generated by our plugin. This information will be added to the metadata of each source file under the key 'contentful'. So besides our own plans for the future of the plugin (enabling sorting of the entries, use of the sync operation, etc.) you can roll your own ideas and even open a pull request to get them in the plugin if you find them interesting enough.
The plugin can be installed via npm or by cloning its repository. We have also created contentful-metalsmith-example, an example project to make it easier to understand the usage of the plugin. Note that this plugin is still in Contentful labs stage, i.e. an experimental project. So if you have plugin related requests please open an issue on Github or a support ticket.