const moment = require('moment');


module.exports = {
  siteMetadata: {
    title: 'Will Webberley',
    siteUrl: 'https://wilw.dev',
  },
  plugins: [
    {
      resolve: 'gatsby-source-filesystem',
      options: {
        name: 'src',
        path: `${__dirname}/src/pages`,
      },
    },
    {
      resolve: `gatsby-plugin-feed`,
      options: {
        query: `
          {
            site {
              siteMetadata {
                title
                description
                siteUrl
                site_url: siteUrl
              }
            }
          }
        `,
        feeds: [
          {
            serialize: ({ query: { site, allMarkdownRemark } }) => {
              return allMarkdownRemark.edges.map(edge => {
                const { slug } = edge.node.fields;
                const dateSection = slug.match(/[0-9]{4}\/[0-9]{2}\/[0-9]{2}\//g)[0];
                const newDateSection = dateSection.replace(/-/g, '/');
                const date = moment.utc(newDateSection, 'YYYY/MM/DD/').format()
                return Object.assign({}, edge.node.frontmatter, {
                  description: edge.node.description,
                  date: date,
                  url: site.siteMetadata.siteUrl + slug,
                  guid: site.siteMetadata.siteUrl + slug,
                  custom_elements: [{ "content:encoded": edge.node.html }],
                })
              })
            },
            query: `
              {
                allMarkdownRemark(
                  sort: { order: DESC, fields: [fileAbsolutePath] },
                ) {
                  edges {
                    node {
                      html
                      fields { slug }
                      frontmatter {
                        title
                        description
                      }
                    }
                  }
                }
              }
            `,
            output: "/rss.xml",
            title: "Will Webberley's Blog",
            description: 'A collection of notes and thoughts.'
          },
        ],
      },
    },
    'gatsby-transformer-remark',
    'gatsby-plugin-react-helmet',
  ],
};
