GithubHelp home page GithubHelp logo

elboman / gatsby-remark-source-name Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 2.0 26 KB

Add a custom sourceName field to remark nodes

License: MIT License

JavaScript 100.00%
gatsby gatsbyjs gatsby-plugin gatsby-remark gatsby-source-filesystem source sourcename

gatsby-remark-source-name's Introduction

gatsby-remark-source-name

Add a custom sourceName field to Remark nodes to filter them easily.

Install

yarn add gatsby-remark-source-name

How to use

First add the plugin to your gatsby-config.js.

plugins: [`gatsby-remark-source-name`];

Next you define a name for the group of markdown files in the filesystem source plugin:

{
  resolve: `gatsby-source-filesystem`,
  options: {
    path: `${__dirname}/src/_posts`,
    name: 'blog', // -> name of the group
  }
}

You can then query the source name in GraphQL:

// example of gatsby-node.js

exports.createPages = ({ boundActionCreators, graphql }) => {
  const { createPage } = boundActionCreators;

   // create pages
  const PostTemplate = path.resolve('src/templates/post.js');


  const query = graphql(`
    query {
      allMarkdownRemark() {
        edges {
          node {
            excerpt(pruneLength: 250)
            html
            fields {
              sourceName
            }
          }
        }
      }
    }
  `);

  return query.then(result => {
    if (result.errors) {
      return Promise.reject(result.errors);
    }

    // filter by source instance name
    const posts = result.data.allMarkdownRemark.edges.filter(
      single => single.node.fields.sourceName === 'blog' // we use the source name to filter nodes
    );

    posts.forEach(({ node }) => {
      createPage({
        path: `/blog/${node.frontmatter.slug}`,
        component: PostTemplate,
        context: {
          slug: node.frontmatter.slug,
        },
      });
    });
  });
});

gatsby-remark-source-name's People

Contributors

elboman avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

boelensman1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.