• This repository has been archived on 08/May/2024
  • Stars
    star
    145
  • Rank 253,143 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 10 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Contentful Middleman is an extension to use the Middleman static site generator (Ruby) together with the API-driven Contentful CMS.

Contentful Middleman

Build Status

Contentful provides a content infrastructure for digital teams to power content in websites, apps, and devices. Unlike a CMS, Contentful was built to integrate with the modern software stack. It offers a central hub for structured content, powerful management and delivery APIs, and a customizable web app that enable developers and content creators to ship digital products faster.

Contentful Middleman is a Middleman extension to use the Middleman static site generator together with Contentful. It is powered by the Contentful Ruby Gem.

Experience the power of Middleman while staying sane as a developer by letting end-users edit content in a web-based interface.

The main release works for Middleman v4 starting on version 4.0.0.

If you are using Middleman v3, use older releases. Latest stable release is 3.0.0

Installation

Add the following line to the Gemfile of your Middleman project:

gem "contentful_middleman"

Then as usual, run:

bundle install

Usage

Run middleman contentful in your terminal. This will fetch entries for the configured spaces and content types and put the resulting data in the local data folder as yaml files.

--rebuild option

The contentful command has a --rebuild option which will trigger a rebuild of your site only if there were changes between the last and the current import.

Configuration

To configure the extension, add the following configuration block to Middleman's config.rb:

activate :contentful do |f|
  f.space         = SPACE
  f.access_token  = ACCESS_TOKEN
  f.cda_query     = QUERY
  f.content_types = CONTENT_TYPES_MAPPINGS
end
Parameter Description
space Hash with an user choosen name for the space as key and the space id as value.
access_token Contentful Delivery API access token.
cda_query Hash describing query configuration. See contentful.rb for more info (look for filter options there). Note that by default only 100 entries will be fetched, this can be configured to up to 1000 entries using the limit option. Example: f.cda_query = { limit: 1000 }.
client_options Hash describing client configuration. See contentful.rb for more info. This option should commonly be used to change Rate Limit Management, Include Resolution, Logging and Proxies.
content_types Hash describing the mapping applied to entries of the imported content types.
default_locale String with the value for the default locale for your space. Defaults to 'en-US'.
use_preview_api Boolean to toggle the used API. Set it to false to use cdn.contentful.com (default value). Set it to true to use preview.contentful.com. More info in the documentation
all_entries Boolean to toggle multiple requests to the API for getting over 1000 entries. This uses a naive approach and can get rate limited. When using this, have in mind adding an order in your :cda_query . Default order is order: 'sys.createdAt'.
all_entries_page_size Integer amount of items per page for :all_entries requests, allowing for smaller page sizes on content heavy requests.
rebuild_on_webhook Boolean to toggle Webhook server. Server will run in port 5678, and will be expecting to receive Contentful Webhook calls on /receive.
webhook_timeout Integer (in seconds) for wait time after Webhook received for rebuilding. Only used if :rebuild_on_webhook is true. Defaults to 300 seconds.
webhook_controller Class for handling Webhook response, defaults to ::ContentfulMiddleman::WebhookHandler.
rich_text_mappings Hash with 'nodeType' => RendererClass pairs determining overrides for the RichTextRenderer library configuration.
base_path String with path to your Middleman Application, defaults to current directory. Path is relative to your current location.
destination String with path within your base path under which to store the output yaml files. Defaults to data.

You can activate the extension multiple times to import entries from different spaces.

Entry mapping

The extension will transform every fetched entry before storing it as a yaml file in the local data folder. If a custom mapper is not specified a default one will be used.

The default mapper will map fields, assets and linked entries.

Custom mappers

You can create your own mappers if you need so. The only requirements for a class to behave as a mapper are an initializer and a map(context, entry) instance method.

The initializer takes two parameters:

  • A Contentful::Array of all entries for the current content model
  • A Middleman::Configuration::ConfigurationManager object containing the Contentful configuration options set in config.rb

See BackrefMapper for an example use of entries.

See the Base mapper for an example use of options.

The map method takes two parameters:

  • A context object. All properties set on this object will be written to the yaml file
  • An entry

Following is an example of such custom mapper:

class MyAwesomeMapper
  def initialize(entries, options)
    @entries = entries
    @options = options
  end

  def map(context, entry)
    context.slug = entry.title.parameterize
    #... more transformations
  end
end

If you don't want to map all the fields by hand inherit from the Base mappper:

class MyAwesomeMapper < ContentfulMiddleman::Mapper::Base
  def map(context, entry)
    super
    # After calling super the context object
    # will have a property for every field in the
    # entry
  end
end

There's also an example back-reference mapper in the examples directory for adding back-references onto entries that are linked to by other entries.

Multiple Mappers

If you want to process a Content Type with multiple mappers, you can use the Composite Design Pattern. The Mapper code should look something similar to the following.

Then you can attach as many Custom Mappers as you want to that one.

class CompositeMapper < ContentfulMiddleman::Mapper::Base
  @@mappers = []
  def self.mappers
    @@mappers
  end

  def map(context, entry)
    super
    mappers.each do |m|
      m.map(context, entry)
    end
  end
end

Then in your config.rb file:

CompositeMapper.mappers << YourMapper.new
CompositeMapper.mappers << OtherMapper.new

activate :contentful do |f|
  '... your config here ...'
  f.content_types = {content_type_name_you_want_to_map: {mapper: CompositeMapper, id: 'content_type_id'}}
end

NOTE: This kind of Composite Mapper is static, therefore if you want to have multiple combinations of mappers for multiple entries, you'd need to write code a bit differently.

Rich Text BETA

To render rich text in your views, you can use the rich_text view helper.

An example using erb:

<% data.my_space.my_type.each do |_, entry| %>
  <%= rich_text(entry.rich_field) %>
<% end %>

This will output the generated HTML generated by the RichTextRenderer library.

Adding custom renderers

When using rich text, if you're planning to embed entries, then you need to create your custom renderer for them. You can read how create your own renderer classes here.

To configure the mappings, you need to add them in your activate block like follows:

activate :contentful do |f|
  # ... all the regular config ...
  f.rich_text_mappings = { 'embedded-entry-block' => MyCustomRenderer }
end

You can also add renderers for all other types of nodes if you want to have more granular control over the rendering.

Using the helper with multiple activated Contentful extensions

In case you have multiple activated extensions, and have different mapping configurations for them. You can specify which extension instance you want to pull the configuration from when using the helper.

The helper receives an additional optional parameter for the extension instance. By default it is 0, indicating the first activated extension.

The instances are sequentially numbered in order of activation, starting from 0.

So, if for example you have 2 active instances with different configuration, to use the second instance configuration, you should call the helper as: rich_text(entry.rich_field, 1).

Configuration: examples

activate :contentful do |f|
  f.space         = {partners: 'space-id'}
  f.access_token  = 'some_access_token'
  f.cda_query     = { content_type: 'content-type-id', include: 1 }
  f.content_types = { partner: 'content-type-id'}
end

The above configuration does the following:

  • Sets the alias partners to the space with id some-id
  • Sets the alias partner to the content type with id content-type-id
  • Uses the default mapper to transform partner entries into yaml files (no mapper specified for the partner content type)

Entries fetched using this configuration will be stored as yaml files in data/partners/partner/ENTRY_ID.yaml.

class Mapper
  def map(context, entry)
    context.title = "#{entry.title}-title"
    #...
  end
end

activate :contentful do |f|
  f.space         = {partners: 'space-id'}
  f.access_token  = 'some_access_token'
  f.cda_query     = { content_type: '1EVL9Bl48Euu28QEOa44ai', include: 1 }
  f.content_types = { partner: {mapper: Mapper, id: 'content-type-id'}}
end

The above configuration is the same as the previous one only that this time we are setting a custom mapper for the entries belonging to the partner content type.

Using imported entries in templates

Middleman will load all the yaml files stored in the local data folder. This lets you use all the imported data into your templates.

Consider that we have data stored under data/partners/partner. Then in our templates we could use that data like this:

<h1>Partners</h1>
<ol>
  <% data.partners.partner.each do |id, partner| %>
    <li><%= partner["name"] %></li>
  <% end %>
</ol>

Rendering Markdown:

If you want to use markdown in your content types you manually have to render this to markdown. Depending on the markdown library you need to transform the data. For Kramdown this would be:

<%= Kramdown::Document.new(data).to_html %>

Locales

If you have localized entries, and want to display content for multiple locales. You can now include locale: '*' in your CDA query.

Then you have the following methods of accessing locales:

  • Manual access

You can access your localized fields by fetching the locale directly from the data

<h1>Partners</h1>
<ol>
  <% data.partners.partner.each do |id, partner| %>
    <li><%= partner["name"]['en-US'] %></li>
  <% end %>
</ol>
  • Entry Helper

You can also map an specific locale for all entry fields using localize_entry

<h1>Partners</h1>
<ol>
  <% data.partners.partner.each do |id, partner| %>
    <% localized_partner = localize_entry(partner, 'es') %>
    <li><%= localized_partner["name"] %></li>
  <% end %>
</ol>
  • Generic Field Helper

The localize helper will map an specific locale to a field of your entry

<h1>Partners</h1>
<ol>
  <% data.partners.partner.each do |id, partner| %>
    <li>Value Field: <%= localize(partner, 'name', 'en-US') %></li>
    <li>Array Field: <%= localize(partner, 'phones', 'es') %></li>
  <% end %>
</ol>
  • Specific Field Type Helper

Or, you can use localize_value or localize_array if you want more granularity.

This method is discouraged, as localize achieves the same goal and is a field-type agnostic wrapper of these methods.

<h1>Partners</h1>
<ol>
  <% data.partners.partner.each do |id, partner| %>
    <li>Value Field: <%= localize_value(partner['name'], 'en-US') %></li>
    <li>Array Field: <%= localize_array(partner['phones'], 'es') %></li>
  <% end %>
</ol>

If your fields are not localized, the value of the field will be returned.

In case of the field being localized but no value being set for a given entry, it will use a fallback locale, by default is en-US but can be specified as an additional parameter in all the mentioned calls.

Preview API Helper

You can use the #with_preview helper to try your Preview API content without having to generate the entire data structures.

This generates a new Preview Contentful Client and has a cache that will store your objects in memory until they are considered to need refresh.

It can be used like a Contentful Client:

<% with_preview(space: 'cfexampleapi', access_token: 'b4c0n73n7fu1') do |preview| %>
  <% entry = preview.entry('nyancat') %>

  <p>Name: <%= entry.name %></p>
<% end %>

If you want to clear the cache to force a refresh:

<% with_preview(space: 'cfexampleapi', access_token: 'b4c0n73n7fu1') do |preview| %>
  <% preview.clear_cache %>
<% end %>

Caching Rules

  • Every preview client will be cached by Space/Access Token combination
  • Only entry, entries, asset and assets will be cached
  • Every call will be cached by it's query parameters and ID (if ID is applicable)
  • Each call will be considered, by default, stale after 3 tries or 2 hours
  • Cache can be cleared by calling #clear_cache, this applies per preview client

Caching Configuration

You can configure :tries and :expires_in in the #with_preview call like this:

<% with_preview(
     space: 'cfexampleapi',
     access_token: 'b4c0n73n7fu1',
     tries: 20,                                                      # Set Tries to 20 before stale
     expires_in: ContentfulMiddleman::Tools::PreviewProxy.minutes(5) # Set Expiration to 5 minutes
   ) do |preview| %>
  <!-- do your stuff -->
<% end %>

Platform Specific Deployment Caveats

For platform specific issues, please look into the DEPLOYING document. This document is expected to grow with user contributions. Feel free to add your own discoveries to that file by issuing a Pull Request.

More Repositories

1

contentful.js

JavaScript library for Contentful's Delivery API (node & browser)
TypeScript
1,174
star
2

rich-text

Libraries for handling and rendering Rich Text πŸ“„
TypeScript
529
star
3

the-example-app.nodejs

Example app for Contentful in node.js
JavaScript
431
star
4

forma-36

A design system by Contentful
TypeScript
329
star
5

contentful-cli

The official Contentful command line interface. Use Contentful features straight from the command line!
JavaScript
322
star
6

contentful-migration

🚚 Migration tooling for contentful
TypeScript
320
star
7

contentful-management.js

JavaScript library for Contentful's Management API (node & browser)
TypeScript
264
star
8

extensions

Repository providing samples using the UI Extensions SDK
JavaScript
201
star
9

contentful.swift

A delightful Swift interface to Contentful's content delivery API.
Swift
195
star
10

starter-gatsby-blog

Gatsby starter for a Contentful project from the community.
JavaScript
194
star
11

cf-graphql

Generate a GraphQL schema out of your Contentful space
JavaScript
185
star
12

blog-in-5-minutes

Vue
170
star
13

contentful-export

This tool allows you to export a Contentful space to a JSON dump
JavaScript
160
star
14

field-editors

React components and extensions for building Contentful entry editor
TypeScript
148
star
15

Stargate

A communication channel from your Mac to your watch.
Swift
135
star
16

contentful.rb

Ruby client for the Contentful Content Delivery API
Ruby
135
star
17

apps

Apps on the Contentful Marketplace and resources to build them
TypeScript
132
star
18

ui-extensions-sdk

A JavaScript library to develop custom apps for Contentful
TypeScript
120
star
19

discovery-app-react

A React.js based version of the Contentful Discovery app
JavaScript
111
star
20

contentful.php

Official PHP Library for the Content Delivery API
PHP
111
star
21

contentful-import

Node module that uses the data provided by contentful-export to import it to contentful space
TypeScript
99
star
22

jekyll-contentful-data-import

Contentful Plugin for the Jekyll Static Site Generator
Ruby
99
star
23

create-contentful-app

Bootstrap a Contentful App
TypeScript
98
star
24

gallery-app-react

A React application example for the gallery space template.
JavaScript
97
star
25

contentful.net

.NET Library for Contentful's Content Delivery and Management API
C#
95
star
26

template-blog-webapp-nextjs

Next.js blog starter template
TypeScript
89
star
27

contentful-metalsmith

A plugin for Metalsmith that pulls content from the Contentful API
JavaScript
86
star
28

vault

Easy persistence of Contentful data for Android over SQLite.
Java
84
star
29

template-marketing-webapp-nextjs

Next.js marketing website starter template
TypeScript
80
star
30

contentful.java

Java SDK for Contentful's Content Delivery API
Java
74
star
31

create-contentful-extension

Create Contentful Extension is a CLI tool for developing in-app extensions without the hassle of managing build configurations.
JavaScript
71
star
32

ContentfulWatchKitExample

Example for a WatchKit app using the Contentful SDK
Swift
71
star
33

gallery-app-android

Android application example for the gallery space template.
Java
68
star
34

template-ecommerce-webapp-nextjs

Next.js ecommerce website starter template
TypeScript
63
star
35

compose-starter-helpcenter-nextjs

A sample website frontend for Compose with Next.js
TypeScript
59
star
36

live-preview

Preview SDK for both the field tagging connection + live content updates
TypeScript
56
star
37

contentful_rails

A ruby gem to help you quickly integrate Contentful into your Rails site
Ruby
52
star
38

contentful.objc

Objective-C SDK for Contentful's Content Delivery API and Content Management API.
Objective-C
50
star
39

contentful-resolve-response

Resolve items & includes of a Contentful API response into a proper object graph
JavaScript
46
star
40

contentful.py

Python client for the Contentful Content Delivery API https://www.contentful.com/developers/documentation/content-delivery-api/
Python
45
star
41

contentful-laravel

Laravel integration for the Contentful SDK.
PHP
44
star
42

contentful_model

A lightweight wrapper around the Contentful api gem, to make it behave more like ActiveRecord
Ruby
44
star
43

contentful-space-sync

Synchronize Contentful spaces
JavaScript
42
star
44

the-example-app.swift

Example app for Contentful in Swift
Swift
42
star
45

covid-19-site-template

#project-covid19
JavaScript
40
star
46

11ty-contentful-starter

Contentful 11ty starter project.
CSS
39
star
47

contentful-management.py

Python client for the Contentful Content Management API https://www.contentful.com/developers/documentation/content-management-api/
Python
36
star
48

product-catalogue-js

Product catalogue in JavaScript
JavaScript
36
star
49

contentful-management.rb

Ruby client for the Contentful Content Management API
Ruby
33
star
50

ContentfulBundle

Symfony Bundle for the Contentful SDK.
PHP
32
star
51

contribution-program

Contribute to the Contentful blog with pieces on "better ways to build websites". Get rewarded for each post you publish.
30
star
52

contentful_express_tutorial

A Simple Express js application Built on top of Contentful
JavaScript
30
star
53

rich-text-renderer.swift

Render Contentful Rich Text fields to native strings and views
Swift
29
star
54

contentful_jekyll_examples

Examples for Contentful and Jekyll Integration
29
star
55

11ty-contentful-gallery

Photo Gallery made using Contentful and 11ty.
Liquid
28
star
56

guide-app-sw

A generic guide app for shop guides
JavaScript
27
star
57

ls-postman-rest-api

26
star
58

gallery-app-ios

An iOS application example for the gallery space template.
Swift
26
star
59

contentful-management.java

Java library for using the Contentful Content Management API
Java
26
star
60

contentful-bootstrap.rb

Contentful CLI tool for getting started with Contentful
Ruby
24
star
61

contentful-graphql-playground-app

Contentful App to integrate GraphQL Playground
TypeScript
23
star
62

wordpress-exporter.rb

Adapter to extract data from Wordpress
Ruby
23
star
63

contentful-database-importer.rb

Adapter to extract data from SQL Databases https://www.contentful.com
Ruby
23
star
64

blog-app-android

Android application example for the blog space template.
Java
22
star
65

contentful-sdk-core

Core modules for the Contentful JS SDKs
TypeScript
22
star
66

contentful-persistence.swift

Simplified persistence for the Contentful Swift Library.
Swift
21
star
67

content-models

A set of example content models build with and for Contentful
19
star
68

product-catalogue-android

Android application example for the product catalogue space template.
Java
19
star
69

the-example-app.graphql.js

Example app for Contentful with GraphQL with React
JavaScript
19
star
70

boilerplate-javascript

Boilerplate project for getting started using javascript with Contentful
JavaScript
19
star
71

the-example-app.php

Example app for Contentful in PHP
PHP
18
star
72

ng-contentful

Contentful module for AngularJS, providing access to the content delivery API
JavaScript
17
star
73

contentful_middleman_examples

A useful collection of examples using the `contentful_middleman` gem
Ruby
17
star
74

the-example-app.py

Example app for Contentful in Python
Python
17
star
75

The-Learning-Demo

Working repo for the new Learning Demo being developed by Learning Services
CSS
17
star
76

contentful-core.php

Foundation library for Contentful PHP SDKs
PHP
16
star
77

guide-app-ios

A generic iOS app for shop guides, styled as a guide to Coffee places.
Objective-C
16
star
78

starter-hydrogen-store

Example store starter template built with Contentful and Hydrogen framework from Shopify
JavaScript
16
star
79

contentful_django_tutorial

A Simple Django Application using Contentful
CSS
16
star
80

contentful-action

JavaScript
15
star
81

contentful-merge

CLI to merge entries between environments
TypeScript
15
star
82

contentful-batch-libs

Library modules used by contentful batch utility CLI tools.
JavaScript
15
star
83

sveltekit-starter

Starter repo to get started with Sveltekit and Contentful
Svelte
15
star
84

contentful-scheduler.rb

Scheduling Server for Contentful entries
Ruby
15
star
85

blog-app-ios

An iOS application example for the blog space template.
Swift
15
star
86

the-example-app.kotlin

The example Android app. See how to connect to a sample space and use kotlin and Contentful unisono.
Kotlin
15
star
87

contentful-social.rb

Contentful Social Publishing Gem
Ruby
14
star
88

discovery-app-android

Contentful Discovery App for Android
Java
14
star
89

discovery-app

iOS app for previewing the content of Contentful Spaces
Objective-C
14
star
90

ls-content-as-code

Example content migration API scripts
JavaScript
13
star
91

blog-app-laravel

Example app using Contentful with Laravel.
PHP
13
star
92

slash-developers

THIS REPOSITORY IS DEPRECATED. Please do not open new PRs or issues.
HTML
13
star
93

node-apps-toolkit

A collection of helpers and utilities for creating NodeJS Contentful Apps
TypeScript
12
star
94

rich-text.php

Utilities for the Contentful Rich Text
PHP
12
star
95

the-example-app.csharp

Example app for Contentful in C#
C#
12
star
96

the-example-app.graphql.swift

The Example App and Contentful GraphQL Endpoint, using Apollo iOS, Contentful/ImageOptions
Swift
12
star
97

contentful-link-cleaner

Cleans up unresolved Entry links in Contentful spaces
JavaScript
11
star
98

tvful

Example for using the Contentful SDK for tvOS apps.
Swift
11
star
99

contentful-sdk-jsdoc

JSDoc template and config for the Contentful JS SDKs
JavaScript
10
star
100

generator.java

Code generator for Contentful models
Shell
10
star