text: const path = require(`path`)
  const  _ = require('lodash')
  const { createFilePath } = require(`gatsby-source-filesystem`)
  
  exports.createPages = async ({ graphql, actions }) => {
    const { createPage } = actions
  
    const blogPost = path.resolve(`./src/templates/blog-post.js`);
    const tagTemplate = path.resolve("src/templates/tags.js");
    const result = await graphql(`
      {
        postsRemark: allMarkdownRemark(
          sort: { order: DESC, fields: [frontmatter___date] }
          limit: 2000
        ) {
          edges {
            node {
              fields {
                slug
              }
              frontmatter {
                title
                tags
              }
            }
          }
        }
        tagsGroup: allMarkdownRemark(limit: 2000) {
          group(field: frontmatter___tags) {
            fieldValue
          }
        }
      }
    `)
  
    if (result.errors) {
      throw result.errors
    }
  // Create blog posts pages.
  const posts = result.data.postsRemark.edges;
  posts.forEach((post, index) => {
    const previous = index === posts.length - 1 ? null : posts[index + 1].node;
    const next = index === 0 ? null : posts[index - 1].node;
    
      createPage({
      path: post.node.fields.slug,
      component: blogPost,
      context: {
      slug: post.node.fields.slug,
      previous,
      next,
      },
      });
    });
  
      // Create blog post list pages
      const postsPerPage = 5;
      const numPages = Math.ceil(posts.length / postsPerPage);
  
      Array.from({ length: numPages }).forEach((_, i) => {
        createPage({
          path: i === 0 ? `/` : `/${i + 1}`,
          component: path.resolve('./src/templates/blog-list.js'),
          context: {
            limit: postsPerPage,
            skip: i * postsPerPage,
            numPages,
            currentPage: i + 1
          },
        });
      });
  
      // Extract tag data from query
    const tags = result.data.tagsGroup.group
    // Make tag pages
    tags.forEach(tag => {
      createPage({
        path: `/tags/${_.kebabCase(tag.fieldValue)}/`,
        component: tagTemplate,
        context: {
          tag: tag.fieldValue,
        },
      })
    });
  
  }
  
  exports.onCreateNode = ({ node, actions, getNode }) => {
    const { createNodeField } = actions
  
    if (node.internal.type === `MarkdownRemark`) {
      const value = createFilePath({ node, getNode })
      createNodeField({
        name: `slug`,
        node,
        value,
      })
    }
  }
  
  