npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

contentful-metalsmith-feeds

v1.0.0

Published

Metalsmith plugin to generate RSS and JSON feeds from Contentful posts.

Downloads

1

Readme

contentful-metalsmith-feeds

Metalsmith plugin to generate RSS and JSON feeds from Contentful posts.

This works in tandem with contentful-metalsmith. You’ll need to install and use that plugin along with this one.

https://npmjs.org/package/contentful-metalsmith-feeds

⚠️ NOTE:

This might not be the most necessary plugin ever. I (Tyler) wrote it to serve a specific need I had on Unstuck. It's opinionated to work for the sites of that site. Much of the feed creation–JSON and RSS–is pulled from other Metalsmith plugins.

Use this with caution. If more people end up using it, we can always expand it to make it flexible for more use cases.

Main known issues; Not all RSS and JSON feed items can be configured. The keys and child elements are limited to the small set I needed for Unstuck.

Installation

yarn add contentful-metalsmith-feeds

or

npm install contentful-metalsmith-feeds

Usage

Usage assumes you’ve installed and configured contentful-metalsmith#getting-started according to its configure docs.

Here is an example minimum Metalsmith build script, build.js:

const metalsmith = require('metalsmith');
const contentful = require('contentful-metalsmith');
const contentfulFeeds = require('contentful-metalsmith-feeds');

metalsmith(__dirname)
  .source('./src')
  .destination('./build')
  .use(contentful({
    access_token: 'YOUR_CONTENTFUL_ACCESS_TOKEN',
    space_id: 'YOUR_CONTENTFUL_SPACE_ID'
  }))
  .use(contentfulFeeds({
    contentType: 'YOUR_CONTENTFUL_CONTENT_TYPE',
    title: 'My Website'
  }))
  .build(err => {
    if (err) {
      console.log(err);
    }
  });

Configuration

In the example above we send title as an option to contentfulFeeds. title is a required setting, if it is not provided, the build will fail.

In addition to passing configuration values to the function, you can also use the common site object in metadata.

Here's an expanded example of build.js specifying more configuration options via metadata:

const metalsmith = require('metalsmith');
const contentful = require('contentful-metalsmith');
const contentfulFeeds = require('contentful-metalsmith-feeds');

metalsmith(__dirname)
  .source('./src')
  .destination('./build')
  .metadata({
    site: {
      title: 'My Website',
      url: 'https://mywebsite.net',
      description: 'This is a short description of the site and feeds.'
    }
  })
  .use(contentful({
    access_token: 'YOUR_CONTENTFUL_ACCESS_TOKEN',
    space_id: 'YOUR_CONTENTFUL_SPACE_ID'
  }))
  .use(contentfulFeeds({
    contentType: 'YOUR_CONTENTFUL_CONTENT_TYPE'
  }))
  .build(err => {
    if (err) {
      console.log(err);
    }
  });

Why use metadata.site?

We use the site object in metadata because it's a common pattern in Metalsmith plugins.

Which config method takes precedence?

Config values passed to the function take precedence over metadata.site.

Take this partial build.js as an example:

...
.metadata({
  site: {
    title: 'My Website'
  }
})
...
.use(contentfulFeeds({
  contentType: 'YOUR_CONTENTFUL_CONTENT_TYPE',
  title: 'The Real Title of My Website'
}))
...

"The Real Title of My Website" will be the title used for the generated feeds. The same is true for any metadata.site configuration options.

Configuration Options

Options that can be included in either metadata.site or in the plugin function as described above:

title

Required Used as top-level title and <title> of JSON and RSS feeds, respectively.

url

Used as top-level home_page_url and <link> of JSON and RSS feeds, respectively. This will also be the base of top-level feed_url in the JSON feed.

description

Used as top-level description and <description> of JSON and RSS feeds, respectively.

Options that can only be included in the plugin function:

contentType

Required The Contentful contentType id. This plugin is opinionated about what the content model of this type should be. This should be a standard "blog post" type object.

json.destination

Default: feed.json The name of the file created for the JSON Feed. This will also be the path portion of top-level feed_url.

Example usage:

...
.use(contentfulFeeds({
  contentType: 'YOUR_CONTENTFUL_CONTENT_TYPE',
  url: 'https://mysite.org',
  json: {
    destination: 'mySiteFeed.json'
  }
}))
...

This will produce the file mySiteFeed.json and set feed_url: https://mysite.org/mySiteFeed.json.

rss.destination

Default: rss.xml The name of the file created for the RSS Feed.

Example usage:

...
.use(contentfulFeeds({
  contentType: 'YOUR_CONTENTFUL_CONTENT_TYPE',
  rss: {
    destination: 'mySiteFeed.xml'
  }
}))
...

This will produce the file mySiteFeed.xml.

marked

Custom settings for marked. The plugin uses marked to parse post contents.

Example usage:

...
.use(contentfulFeeds({
  contentType: 'YOUR_CONTENTFUL_CONTENT_TYPE',
  marked: {
    smartypants: true,
    gfm: true,
    tables: true
  }
}))
...

Expected Content Model

As mentioned, this plugin is opinionated. It is expecting content from Contentful to have a specific shape with at least the following keys:

{
  "data": {
    "fields": {
      "author": [
        {
          "fields": {
            "name": "Author Name"
          }
        }
      ],
      "body": "The **markdown** contents of the blog post.",
      "date": "2017-01-12",
      "title": "The Post Title",
      "slug": "the-post-title"
    }
  }
}