Add type field for markdown sources

1 min read
const _ = require("lodash")

exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
     const { createNodeField } = boundActionCreators;
   
     if (_.get(node, "internal.type") === `MarkdownRemark`) {
       // Get the parent node
       const parent = getNode(_.get(node, "parent"));
   
       // Create a field on this node for the "collection" of the parent
       // NOTE: This is necessary so we can filter `allMarkdownRemark` by
       // `collection` otherwise there is no way to filter for only markdown
       // documents of type `post`.
       createNodeField({
         node,
         name: "collection",
         value: _.get(parent, "sourceInstanceName")
       });
     }
   };
Is this post useful to you? Please give us a rating!

Subscribe

Want regular updates on Gatsby and its awesomeness? Subscribe here. No spam, promise. Full details on privacy.

We use MailChimp as our marketing automation platform. By clicking "Submit" to submit this form, you acknowledge that the information you provide will be transferred to MailChimp for processing in accordance with their Privacy Policy and Terms.

Share

If you'd like to share this post, here are some handy links to make that easier.

Post last updated: May 29, 2018

Comments

Feedback? A point to make? Express yourself via the comment form below.

Don't forget to check the captcha. Otherwise we won't receive your comment.

Be the first to post a comment on this post