Skip to content

Commit

Permalink
it is working
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Feb 5, 2020
1 parent a4e0d49 commit 616a663
Show file tree
Hide file tree
Showing 15 changed files with 1,135 additions and 1,525 deletions.
67 changes: 12 additions & 55 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable max-statements */
const path = require('path')
const {URL} = require('url')
const slugify = require('@sindresorhus/slugify')
const {createFilePath} = require('gatsby-source-filesystem')
const remark = require('remark')
const stripMarkdownPlugin = require('strip-markdown')
const _ = require('lodash')

const PAGINATION_OFFSET = 7
const config = require('./config/website')

const createWorkshops = (createPage, edges) => {
edges.forEach(({node}, i) => {
Expand Down Expand Up @@ -74,23 +74,14 @@ const createPosts = (createPage, createRedirect, edges) => {
})
}

function createBlogPages({blogPath, data, paginationTemplate, actions}) {
function createBlogPages({data, actions}) {
if (_.isEmpty(data.edges)) {
throw new Error('There are no posts!')
}

const {edges} = data
const {createRedirect, createPage} = actions
createPosts(createPage, createRedirect, edges)
createPaginatedPages(
actions.createPage,
edges,
blogPath,
paginationTemplate,
{
categories: [],
},
)
return null
}

Expand Down Expand Up @@ -220,13 +211,11 @@ exports.createPages = async ({actions, graphql}) => {
createBlogPages({
blogPath: '/blog',
data: blog,
paginationTemplate: path.resolve(`src/templates/blog.js`),
actions,
})
createBlogPages({
blogPath: '/writing/blog',
data: writing,
paginationTemplate: path.resolve(`src/templates/writing-blog.js`),
actions,
})
createWorkshopPages({
Expand All @@ -243,47 +232,6 @@ exports.onCreateWebpackConfig = ({actions}) => {
})
}

function createPaginatedPages(
createPage,
edges,
pathPrefix,
paginationTemplate,
context,
) {
const pages = edges.reduce((acc, value, index) => {
const pageIndex = Math.floor(index / PAGINATION_OFFSET)

if (!acc[pageIndex]) {
acc[pageIndex] = []
}

acc[pageIndex].push(value.node.id)

return acc
}, [])

pages.forEach((page, index) => {
const previousPagePath =
index === 1 ? pathPrefix : `${pathPrefix}/${index - 1}`
const nextPagePath = `${pathPrefix}/${index + 1}`

createPage({
path: index > 0 ? `${pathPrefix}/${index}` : `${pathPrefix}`,
component: paginationTemplate,
context: {
pagination: {
page,
nextPagePath: index === pages.length - 1 ? null : nextPagePath,
previousPagePath: index === 0 ? null : previousPagePath,
pageCount: pages.length,
pathPrefix,
},
...context,
},
})
})
}

// eslint-disable-next-line complexity
exports.onCreateNode = ({node, getNode, actions}) => {
const {createNodeField} = actions
Expand Down Expand Up @@ -366,6 +314,15 @@ exports.onCreateNode = ({node, getNode, actions}) => {
value: slug,
})

const productionUrl = new URL(config.siteUrl)
productionUrl.pathname = slug

createNodeField({
name: 'productionUrl',
node,
value: productionUrl.toString(),
})

createNodeField({
name: 'date',
node,
Expand Down
Loading

0 comments on commit 616a663

Please sign in to comment.