-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
executable file
·81 lines (72 loc) · 2.63 KB
/
gatsby-node.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
81
const _ = require(`lodash`)
const Promise = require(`bluebird`)
const path = require(`path`)
const config = require(`./src/utils/siteConfig`)
const { paginate } = require(`gatsby-awesome-pagination`)
/**
* Here is the place where Gatsby creates the URLs for all the
* posts that we fetched from the Ghost site.
*/
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
const createPosts = new Promise((resolve, reject) => {
const postTemplate = path.resolve(`./src/templates/post.js`)
const indexTemplate = path.resolve(`./src/templates/index.js`)
resolve(
graphql(`
{
allGhostPost(
sort: {order: ASC, fields: published_at},
filter: {
slug: {ne: "data-schema"}
}
) {
edges {
node {
slug
}
}
}
}`
).then((result) => {
if (result.errors) {
return reject(result.errors)
}
if (!result.data.allGhostPost) {
return resolve()
}
const items = result.data.allGhostPost.edges
_.forEach(items, ({ node }) => {
// This part here defines, that our posts will use
// a `/:slug/` permalink.
node.url = `/${node.slug}/`
createPage({
path: node.url,
component: path.resolve(postTemplate),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
slug: node.slug,
},
})
})
// Pagination for posts, e.g., /, /page/2, /page/3
paginate({
createPage,
items: items,
itemsPerPage: config.postsPerPage,
component: indexTemplate,
pathPrefix: ({ pageNumber }) => {
if (pageNumber === 0) {
return `/`
} else {
return `/page`
}
},
})
return resolve()
})
)
})
return Promise.all([createPosts])
}