-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
89 lines (82 loc) · 2.3 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
82
83
84
85
86
87
88
89
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
// Define a template for blog post
const blogPage = path.resolve(`./src/templates/blog-page.js`)
const result = await graphql(
`
{
story: allStoryblokEntry(
filter: { field_component: { eq: "blogpost" } }
) {
nodes {
name
slug
uuid
created_at(formatString: "YYYY/MM/DD")
content
full_slug
}
}
settings: storyblokEntry(field_component: { eq: "settings" }) {
content
full_slug
}
bio: storyblokEntry(field_component: { eq: "bio" }) {
content
full_slug
}
}
`
)
if (result.errors) {
reporter.panicOnBuild(
`There was an error loading your blog posts`,
result.errors
)
return
}
const posts = result.data.story.nodes
const settings = {
content: JSON.parse(result.data.settings.content),
full_slug: result.data.settings.full_slug,
}
const bio = {
content: JSON.parse(result.data.bio.content),
full_slug: result.data.bio.full_slug,
}
// Create blog posts pages
// But only if there's at least one markdown file found at "content/blog" (defined in gatsby-config.js)
// `context` is available in the template as a prop and as a variable in GraphQL
if (posts.length > 0) {
posts.forEach((post, index) => {
const previous = {
slug: index === 0 ? null : posts[index - 1].slug,
title: index === 0 ? null : JSON.parse(posts[index - 1].content).title,
}
const next = {
slug: index === posts.length - 1 ? null : posts[index + 1].slug,
title:
index === posts.length - 1
? null
: JSON.parse(posts[index + 1].content).title,
}
const content = JSON.parse(post.content)
createPage({
path: `${post.slug}`,
component: blogPage,
context: {
_uid: content._uid,
uuid: post.uuid,
previous,
next,
full_slug: post.full_slug,
content: content,
settings,
bio,
},
})
})
}
}