Skip to content

Commit

Permalink
Reimplement setup and serialize methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Fowler committed Jan 6, 2018
1 parent 859a104 commit 257dae4
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 30 deletions.
19 changes: 5 additions & 14 deletions packages/gatsby-plugin-feed/src/createFeed.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import url from 'url'
import deepmerge from 'deepmerge'
import RSS from 'rss'

const defaultOptions = {
Expand All @@ -8,17 +7,12 @@ const defaultOptions = {
}

export default function createFeed({
query: {
site: {
siteMetadata,
},
entries,
...rest
},
metadata,
entries,
output,
}) {
// Ensure that siteMetadata is not undefined
const metadata = siteMetadata || {}
// Provide a couple of defaults that will
// override the node-rss defaults
const options = { ...defaultOptions }

// Assign the default feed options. Note that we can skip existence
Expand All @@ -45,11 +39,8 @@ export default function createFeed({
}
}

// @TODO: is this useful? We should think of an example that would use this.
const context = deepmerge(rest, options)

// Return an instance of node-rss with our options
return new RSS(context)
return new RSS(options)
}

function hasMedia(nodes) {
Expand Down
43 changes: 31 additions & 12 deletions packages/gatsby-plugin-feed/src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
import path from 'path'
import deepmerge from 'deepmerge'

import { defaultOptions, runQuery, writeFile } from './internals'
import {
defaultOptions,
isFunction,
runQuery,
writeFile,
} from './internals'
import createFeed from './createFeed'
import serialize from './serialize'
import setup from './setup'

const publicPath = `./public`

exports.onPostBuild = async ({ graphql }, pluginOptions) => {
delete pluginOptions.plugins
exports.onPostBuild = async ({ graphql }, userOptions) => {
delete userOptions.plugins

// Merge default options with user options,
// overriding the defaults.
const options = {
const pluginOptions = {
...defaultOptions,
...pluginOptions,
...userOptions,
}

// If there is a top-level query to execute, do so,
// and replace the original query with the result.
// The resulting data will be used to generate the default
// no-configuration-required feed, or will be deep-merged
// with the result of any custom feed's query.
if (options.query) {
options.query = await runQuery(graphql, options.query)
if (pluginOptions.query) {
pluginOptions.query = await runQuery(graphql, pluginOptions.query)
}

return Promise.all(options.feeds.map(async (feed) => {
return Promise.all(pluginOptions.feeds.map(async (feed) => {
// Each feed's inherits the result of the global query
const feedOptions = {
let feedOptions = {
...feed,
query: options.query,
query: pluginOptions.query,
}

// Each feed may specify a query of its own. If this
Expand All @@ -41,11 +47,24 @@ exports.onPostBuild = async ({ graphql }, pluginOptions) => {
feedOptions.query = deepmerge(feedOptions.query, feedQuery)
}

// Each feed may provide a `setup` function for the purpose
// of remapping feed options before they are passed to node-rss
const metadata = isFunction(feed.setup) ?
feed.setup(feedOptions.query) :
setup(feedOptions.query)

// Create the feed instance
const outputFeed = createFeed(feedOptions)
const outputFeed = createFeed({
metadata,
entries: feedOptions.query.entries,
output: feedOptions.output,
})

// Then serialize each feed item and add it to the feed
const items = serialize(feedOptions)
const items = isFunction(feed.serialize) ?
feed.serialize(feedOptions.query) :
serialize(feedOptions.query)

items.forEach(i => outputFeed.item(i))

// Finally, write the feed file to disk
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-plugin-feed/src/internals.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "fs"
import pify from "pify"

export const writeFile = pify(fs.writeFile)
export const isFunction = f => (f && typeof f === `function`)

export const runQuery = (handler, query) =>
handler(query).then(r => {
Expand Down
12 changes: 8 additions & 4 deletions packages/gatsby-plugin-feed/src/serialize.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Returns the options that will be used to create each item
// in the feed that is generated by node-rss. Each feed may
// provide its own `serialize` function, but this provides
// smart defaults that should work for most folks.
//
// The function receives one argument: the result of the feed's query
export default function serialize({
query: {
site,
entries,
},
site,
entries,
}) {
return entries.edges.map(edge => {
return {
Expand Down
15 changes: 15 additions & 0 deletions packages/gatsby-plugin-feed/src/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Returns the options that will be used to initialize the
// node-rss instance. Each feed may provide its own
// `setup` function, but by default we just return the siteMetadata
// merged with the other top-level keys in the result the feed's query
// (but excluding the `entries` key).
export default function setup({
site: { siteMetadata },
entries,
...rest
}) {
return {
...siteMetadata,
...rest,
}
}

0 comments on commit 257dae4

Please sign in to comment.