text: const path = require("path")
  
  exports.createPages = async ({ actions, graphql, reporter }) => {
    const { createPage } = actions
  
    const result = await graphql(`
      {
        allMarkdownRemark(sort: { order: DESC, fields: [frontmatter___date] }) {
          edges {
            node {
              frontmatter {
                path
              }
            }
          }
        }
      }
    `)
  
    if (result.errors) {
      reporter.panicOnBuild(`Error while running GraphQL query.`)
      return
    }
  
    /**
     * Make each post to page.
     * The path in the frontmatter will be used to load the post.
     * But posts, its path starts with "/_/", will be ignored.
     */
    const posts = result.data.allMarkdownRemark.edges.filter(
      ({ node }) => !node.frontmatter.path.startsWith("/_/")
    )
    posts.forEach(({ node }) => {
      createPage({
        path: node.frontmatter.path,
        component: path.resolve(`src/layouts/post-layout.js`),
      })
    })
  
    /**
     * Make post list pages
     * Each page will have "postsPerPage" posts.
     */
    const postsPerPage = 15
    const numPages = Math.ceil(posts.length / postsPerPage)
    const postPaths = posts.map(({ node }) => node.frontmatter.path)
  
    console.log("These paths will be created as post.", postPaths)
  
    Array.from({ length: numPages }).forEach((_, i) => {
      createPage({
        path: `/all/${i + 1}`,
        component: path.resolve("./src/layouts/list-all-layout.js"),
        context: {
          limit: postsPerPage,
          skip: i * postsPerPage,
          numPages,
          currentPage: i + 1,
        },
      })
    })
  
    /**
     * Make category pages.
     * The category page lists all posts in each category.
     * Reads "category.json" to generate pages.
     * Since the page of the post is created before the category page, the post is loaded
     * if the path of "category.json" and the path of the post are the same.
     */
    const categoryInfo = require("./src/posts/category.json")
    const categoryPaths = categoryInfo.map(info => info.path)
  
    categoryPaths
      .filter(
        catePath => !catePath.startsWith("/_/") && !catePath.startsWith("http")
      )
      .forEach(catePath => {
        // if the path of "category.json" and the path of the post
        // are the same, will not generate category page.
        if (postPaths.includes(catePath)) return
  
        let pathRegex =
          "/" +
          catePath.split("").reduce((prev, cur, curIdx, arr) => {
            prev = prev === "/" ? "\\/" : prev
            return prev + (cur === "/" ? "\\/" : cur)
          }) +
          "\\/.*/"
        console.log("[INFO] Category page will use this regex >> " + pathRegex)
  
        createPage({
          path: catePath,
          component: path.resolve("./src/layouts/category-layout.js"),
          context: {
            categoryRegex: pathRegex,
            catePath,
          },
        })
      })
  }
  