text: /* eslint "no-console": "off" */
  
  const path = require('path');
  const _ = require('lodash');
  const moment = require('moment');
  const siteConfig = require('./data/SiteConfig');
  
  exports.onCreateNode = ({ node, actions, getNode }) => {
    const { createNodeField } = actions;
    let slug;
    if (node.internal.type === 'MarkdownRemark') {
      const fileNode = getNode(node.parent);
      const parsedFilePath = path.parse(fileNode.relativePath);
      if (
        Object.prototype.hasOwnProperty.call(node, 'frontmatter') &&
        Object.prototype.hasOwnProperty.call(node.frontmatter, 'title')
      ) {
        slug = `/${_.kebabCase(node.frontmatter.title)}`;
      } else if (parsedFilePath.name !== 'index' && parsedFilePath.dir !== '') {
        slug = `/${parsedFilePath.dir}/${parsedFilePath.name}/`;
      } else if (parsedFilePath.dir === '') {
        slug = `/${parsedFilePath.name}/`;
      } else {
        slug = `/${parsedFilePath.dir}/`;
      }
  
      if (Object.prototype.hasOwnProperty.call(node, 'frontmatter')) {
        if (Object.prototype.hasOwnProperty.call(node.frontmatter, 'slug'))
          slug = `/${_.kebabCase(node.frontmatter.slug)}`;
        if (Object.prototype.hasOwnProperty.call(node.frontmatter, 'date')) {
          const date = moment(node.frontmatter.date, siteConfig.dateFromFormat);
          if (!date.isValid)
            console.warn(`WARNING: Invalid date.`, node.frontmatter);
  
          createNodeField({ node, name: 'date', value: date.toISOString() });
        }
      }
      createNodeField({ node, name: 'slug', value: slug });
    }
  };
  
  exports.createPages = async ({ graphql, actions }) => {
    const { createPage } = actions;
    const postPage = path.resolve('src/templates/post.js');
    // const categoryPage = path.resolve('src/templates/category.js');
  
    // Get a full list of markdown posts
    const markdownQueryResult = await graphql(`
      {
        allMarkdownRemark {
          edges {
            node {
              fields {
                slug
              }
              frontmatter {
                title
                tags
                category
                date
              }
            }
          }
        }
      }
    `);
  
    if (markdownQueryResult.errors) {
      console.error(markdownQueryResult.errors);
      throw markdownQueryResult.errors;
    }
  
    const tagSet = new Set();
    const categorySet = new Set();
  
    const postsEdges = markdownQueryResult.data.allMarkdownRemark.edges;
  
    // Sort posts from most recent to oldest
    postsEdges.sort((postA, postB) => {
      const dateA = moment(
        postA.node.frontmatter.date,
        siteConfig.dateFromFormat,
      );
  
      const dateB = moment(
        postB.node.frontmatter.date,
        siteConfig.dateFromFormat,
      );
  
      if (dateA.isBefore(dateB)) return 1;
      if (dateB.isBefore(dateA)) return -1;
  
      return 0;
    });
  
    // Post page creating
    postsEdges.forEach((edge, index) => {
      // Generate a list of tags
      if (edge.node.frontmatter.tags) {
        edge.node.frontmatter.tags.forEach((tag) => {
          tagSet.add(tag);
        });
      }
  
      // Generate a list of categories
      if (edge.node.frontmatter.category) {
        categorySet.add(edge.node.frontmatter.category);
      }
  
      // Create post pages
      const prevID = index + 1 < postsEdges.length ? index + 1 : null;
      const nextID = index - 1 >= 0 ? index - 1 : null;
      const nextEdge = postsEdges[nextID];
      const prevEdge = postsEdges[prevID];
  
      createPage({
        path: `/blog${edge.node.fields.slug}`,
        component: postPage,
        context: {
          slug: edge.node.fields.slug,
          nextTitle:
            nextEdge !== undefined ? nextEdge.node.frontmatter.title : null,
          nextSlug: nextEdge !== undefined ? nextEdge.node.fields.slug : null,
          prevTitle:
            prevEdge !== undefined ? prevEdge.node.frontmatter.title : null,
          prevSlug: prevEdge !== undefined ? prevEdge.node.fields.slug : null,
        },
      });
    });
  
    // Create category pages - maybe in the future
    // categorySet.forEach((category) => {
    //   createPage({
    //     path: `/categories/${_.kebabCase(category)}/`,
    //     component: categoryPage,
    //     context: { category },
    //   });
    // });
  };
  