ctrl-content-manager

1.2.0 • Public • Published

ctrl-content-manager

Build Status Codacy Badge Codacy Badge

An abstraction around contentful.

Installation

Yarn is recommended for installation.

$ yarn add ctrl-content-manager

But you can still use npm:

$ npm install --save ctrl-content-manager

Usage

const _ = require('lodash')
const CtrlContentManager = require('ctrl-content-manager')
 
const cm = new CtrlContentManager({
  accessTokens: {
    production: 'contentful_production_token'
  , preview: 'contentful_preview_token'
  }
, fetchInterval: 60 * 60 * 1000
, isProduction: process.env.NODE_ENV === 'production'
, space: 'contentful_space_id'
, parsing: {
    keyed: {
      tags: 'title'
    , authors: 'name'
    }
  , singles: [
      'globalElements'
    , 'blogPostElements'
    ]
  }
})
 
cm.startFetchInterval()
 
// Psuedo blog server code
http.get('/blog/?slug', (request, reply) {
  // Get URL Slug.
  const slug = request.params.slug
 
  // Lookup the blog post by slug.
  const blogPost = cm.find('blogPosts', {slug})
 
  // If that post doesn't exist, show a 404 page with 3 random post suggestions.
  if (!blogPost) {
    return reply('404-template', {
      suggestedBlogs: _.sampleSize(cm.get('blogPosts'), 3)
    })
  }
 
  // Otherwise
  // Get generic blog post elements. (E.g., nav buttons, colors, section titles)
  const blogPostElements = cm.get('blogPostElements')
  // Get the page title suffix. (E.g., ' - My Blog')
  const titleSuffix = cm.get('globalElements.titleSuffix')
  // Get tags that aren't already tagged on this post.
  const otherTags = _.omit(cm.get('tags'), _.map(blogPost.tags, 'title'))
  // Get authors that aren't this post's author.
  const otherAuthors = _.omit(cm.get('authors'), blogPost.author.name)
 
  // Render and serve the blog post.
  return reply('blog-post-template', {
    blogPost
  , blogPostElements
  , titleSuffix
  , otherTags
  , otherAuthors
  })
})

Methods

Full documentation to come soon!

License

Copyright (c) 2016 Martin Experiments LLC

MIT (https://www.opensource.org/licenses/MIT)

Package Sidebar

Install

npm i ctrl-content-manager

Weekly Downloads

1

Version

1.2.0

License

MIT

Last publish

Collaborators

  • suitupalex