const _ = require('lodash')
const Promise = require('bluebird')
const path = require('path')
const slash = require('slash')

exports.createPages = ({ graphql, actions }) => {
  const { createPage } = actions

  return new Promise((resolve, reject) => {
    const postTemplate = path.resolve('./src/templates/post-template.jsx')
    const pageTemplate = path.resolve('./src/templates/page-template.jsx')
    const tagTemplate = path.resolve('./src/templates/tag-template.jsx')
    const categoryTemplate = path.resolve(
      './src/templates/category-template.jsx'
    )

    graphql(`
      {
        allMarkdownRemark(
          limit: 1000
          filter: { frontmatter: { draft: { ne: true } } }
        ) {
          edges {
            node {
              fields {
                slug
              }
              frontmatter {
                tags
                layout
                category
              }
            }
          }
        }
      }
    `).then(result => {
      if (result.errors) {
        console.log(result.errors)
        reject(result.errors)
      }

      _.each(result.data.allMarkdownRemark.edges, edge => {
        const slug = edge.node.fields.slug
        if (_.get(edge, 'node.frontmatter.layout') === 'page') {
          createPage({
            path: slug,
            component: slash(pageTemplate),
            context: { slug },
          })
        } else if (_.get(edge, 'node.frontmatter.layout') === 'post') {
          createPage({
            path: slug,
            component: slash(postTemplate),
            context: { slug },
          })

          const langPrefix = slug.startsWith('/fr/') ? '/fr' : ''

          let tags = []
          if (_.get(edge, 'node.frontmatter.tags')) {
            tags = tags.concat(edge.node.frontmatter.tags)
          }

          tags = _.uniq(tags)
          _.each(tags, tag => {
            const tagPath = `${langPrefix}/tags/${_.kebabCase(tag)}/`
            createPage({
              path: tagPath,
              component: tagTemplate,
              context: { tag },
            })
          })

          let categories = []
          if (_.get(edge, 'node.frontmatter.category')) {
            categories = categories.concat(edge.node.frontmatter.category)
          }

          categories = _.uniq(categories)
          _.each(categories, category => {
            const categoryPath = `${langPrefix}/categories/${_.kebabCase(
              category
            )}/`
            createPage({
              path: categoryPath,
              component: categoryTemplate,
              context: { category },
            })
          })
        }
      })

      resolve()
    })
  })
}

exports.onCreateNode = ({ node, actions, getNode }) => {
  const { createNodeField } = actions

  if (node.internal.type === 'File') {
    const parsedFilePath = path.parse(node.absolutePath)
    const slug = `/${parsedFilePath.dir.split('---')[1]}/`
    createNodeField({ node, name: 'slug', value: slug })
  } else if (
    node.internal.type === 'MarkdownRemark' &&
    typeof node.slug === 'undefined'
  ) {
    const fileNode = getNode(node.parent)
    let slug = fileNode.fields.slug
    if (typeof node.frontmatter.path !== 'undefined') {
      slug = node.frontmatter.path
    }
    createNodeField({
      node,
      name: 'slug',
      value: slug,
    })

    const lang = slug.startsWith('/fr/') ? 'fr' : 'en'
    const langPrefix = lang === 'fr' ? '/fr' : ''
    const nodeWithLang = { ...node, frontmatter: { ...node.frontmatter, lang } }

    if (node.frontmatter.tags) {
      const tagSlugs = node.frontmatter.tags.map(
        tag => `${langPrefix}/tags/${_.kebabCase(tag)}/`
      )
      createNodeField({ node: nodeWithLang, name: 'tagSlugs', value: tagSlugs })
    }

    if (typeof node.frontmatter.category !== 'undefined') {
      const categorySlug = `${langPrefix}/categories/${_.kebabCase(
        node.frontmatter.category
      )}/`
      createNodeField({
        node: nodeWithLang,
        name: 'categorySlug',
        value: categorySlug,
      })
    }
  }
}

@@ -118,16 +118,907 @@
 lash')%0A%0A
+// Not sure if this is the most appropriate hook to create redirections%0A// but, so far, it works.%0Aexports.onPostBootstrap = (%7B actions %7D) =%3E %7B%0A  const %7B createRedirect %7D = actions%0A%0A  // Redirect pages from old blog that get referenced in Google.%0A  // They contained %60.html%60 suffix, which doesn't exist today.%0A  const oldHtmlPages = %5B'/about', '/projects', '/worth-reading'%5D%0A  oldHtmlPages.forEach(page =%3E %7B%0A    createRedirect(%7B%0A      fromPath: %60$%7Bpage%7D.html%60,%0A      isPermanent: true,%0A      redirectInBrowser: true,%0A      toPath: page,%0A    %7D)%0A    createRedirect(%7B%0A      fromPath: %60/fr$%7Bpage%7D.html%60,%0A      isPermanent: true,%0A      redirectInBrowser: true,%0A      toPath: %60/fr$%7Bpage%7D%60,%0A    %7D)%0A  %7D)%0A%0A  // There is no archive page anymore. It used to contain all articles.%0A  createRedirect(%7B%0A    fromPath: '/archive/',%0A    isPermanent: true,%0A    redirectInBrowser: true,%0A    toPath: '/',%0A  %7D)%0A%7D%0A%0A
 exports.

Create redirects to handle old URLs (SEO)