/
createPages.js
80 lines (71 loc) · 2.33 KB
/
createPages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const config = require('../../../gatsby-config')
const query = require('../data/query')
const path = require(`path`)
const { paginate } = require(`gatsby-awesome-pagination`)
module.exports = async ({ graphql, actions }) => {
const { createPage } = actions
const basePath = config.siteMetadata.basePath || '/'
// Create a page for each "post"
const postsQuery = await graphql(query.data.posts)
const posts = postsQuery.data.allContentfulPost.edges
posts.forEach((post, i) => {
const next = i === posts.length - 1 ? null : posts[i + 1].node
const prev = i === 0 ? null : posts[i - 1].node
createPage({
path: `${basePath === '/' ? '' : basePath}/${post.node.slug}/`,
component: path.resolve(`./src/templates/post.js`),
context: {
slug: post.node.slug,
basePath: basePath === '/' ? '' : basePath,
prev,
next,
},
})
})
// Create a page containing all "posts" and paginate.
paginate({
createPage,
component: path.resolve(`./src/templates/posts.js`),
items: posts,
itemsPerFirstPage: config.siteMetadata.postsPerFirstPage || 7,
itemsPerPage: config.siteMetadata.postsPerPage || 6,
pathPrefix: basePath,
context: {
basePath: basePath === '/' ? '' : basePath,
paginationPath: basePath === '/' ? '' : `/${basePath}`,
},
})
// Create "tag" page and paginate
const tagsQuery = await graphql(query.data.tags)
const tags = tagsQuery.data.allContentfulTag.edges
tags.forEach((tag, i) => {
const tagPagination =
basePath === '/'
? `/tag/${tag.node.slug}`
: `/${basePath}/tag/${tag.node.slug}`
paginate({
createPage,
component: path.resolve(`./src/templates/tag.js`),
items: tag.node.post || [],
itemsPerPage: config.siteMetadata.postsPerPage || 6,
pathPrefix: tagPagination,
context: {
slug: tag.node.slug,
basePath: basePath === '/' ? '' : basePath,
paginationPath: tagPagination,
},
})
})
// Create a page for each "page"
const pagesQuery = await graphql(query.data.pages)
const pages = pagesQuery.data.allContentfulPage.edges
pages.forEach((page, i) => {
createPage({
path: `/${page.node.slug}/`,
component: path.resolve(`./src/templates/page.js`),
context: {
slug: page.node.slug,
},
})
})
}