const moment = require('moment');

// Query for all blog posts ordered by filename (i.e. date) descending
const rssPostQuery = `
{
  allMarkdownRemark(
    sort: { order: DESC, fields: [fileAbsolutePath] },
    filter: { fields: { slug: { regex: "/blog/" } } }
  ) {
    edges {
      node {
        html
        fields { slug }
        frontmatter {
          title
          description
          date
          tags
        }
      }
    }
  }
}
`;
const createRssPost = (edge, site) => {
  const { node } = edge;
  const { slug } = node.fields;
  return Object.assign({}, edge.node.frontmatter, {
    description: edge.node.description,
    date: moment.utc(`${node.frontmatter.date}`, 'YYYY/MM/DDTHH:mmZ').format(),
    url: site.siteMetadata.siteUrl + slug,
    guid: site.siteMetadata.siteUrl + slug,
    custom_elements: [{ "content:encoded": edge.node.html }],
  })
};


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: {
        feeds: [
          {
            serialize: ({ query: { site, allMarkdownRemark } }) => allMarkdownRemark.edges.map(e => createRssPost(e, site)),
            query: rssPostQuery,
            output: "/rss.xml",
            title: "Will Webberley's Blog",
            description: 'A collection of all of my notes and posts.'
          },
          {
            serialize: ({ query: { site, allMarkdownRemark } }) => allMarkdownRemark.edges.filter(edge => {
              const tags = edge.node.frontmatter.tags;
              return tags && tags.length > 0 && tags.indexOf('book') > -1;
            }).map(e => createRssPost(e, site)),
            query: rssPostQuery,
            output: "/books.xml",
            title: "Will Webberley's Blog: Books",
            description: 'A collection of notes and posts about books.'
          },
          {
            serialize: ({ query: { site, allMarkdownRemark } }) => allMarkdownRemark.edges.filter(edge => {
              const tags = edge.node.frontmatter.tags;
              return tags && tags.length > 0 && tags.indexOf('technology') > -1;
            }).map(e => createRssPost(e, site)),
            query: rssPostQuery,
            output: "/technology.xml",
            title: "Will Webberley's Blog: Technology",
            description: 'A collection of notes and posts about technology.'
          },
          {
            serialize: ({ query: { site, allMarkdownRemark } }) => allMarkdownRemark.edges.filter(edge => {
              const tags = edge.node.frontmatter.tags;
              return tags && tags.length > 0 && tags.indexOf('life') > -1;
            }).map(e => createRssPost(e, site)),
            query: rssPostQuery,
            output: "/life.xml",
            title: "Will Webberley's Blog: Life",
            description: 'A collection of notes and posts about everyday life.'
          },
          {
            serialize: ({ query: { site, allMarkdownRemark } }) => allMarkdownRemark.edges.filter(edge => {
              const tags = edge.node.frontmatter.tags;
              return tags && tags.length > 0 && tags.indexOf('technology') === -1;
            }).map(e => createRssPost(e, site)),
            query: rssPostQuery,
            output: "/nontech.xml",
            title: "Will Webberley's Blog: Non-Tech",
            description: 'A collection of notes and posts not about technology.'
          },
        ],
      },
    },
    {
      resolve: `gatsby-transformer-remark`,
      options: {
        plugins: [
          {
            resolve: `gatsby-remark-prismjs`,
            options: {
              classPrefix: "language-",
              showLineNumbers: true,
              noInlineHighlight: false,
            }
          }
        ]
      },
    },
    'gatsby-plugin-react-helmet',
    'gatsby-plugin-styled-components',
  ],
};
