-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
120 lines (115 loc) · 2.8 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const path = require(`path`)
// Create blog pages dynamically
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const blogPostTemplate = path.resolve(`src/templates/SinglePost.js`)
const categoryTemplate = path.resolve(`src/templates/category.js`)
const tagTemplate = path.resolve(`src/templates/tags.js`)
const result = await graphql(`
query MyPosts {
posts: allStrapiPosts {
edges {
node {
strapiId
title
body
createdAt(fromNow: true)
like
category {
name
}
tags {
name
}
thumbnail {
url
localFile {
childImageSharp {
gatsbyImageData
}
}
}
}
}
}
categories: allStrapiCategories {
edges {
node {
strapiId
name
description
thumbnail {
url
localFile {
childImageSharp {
gatsbyImageData
}
}
}
}
}
}
tags: allStrapiTags {
edges {
node {
strapiId
name
thumbnail {
url
localFile {
childImageSharp {
gatsbyImageData
}
}
}
}
}
}
}
`)
if (result.errors) {
throw result.errors
}
const posts = result.data.posts.edges
const categories = result.data.categories.edges
const tags = result.data.tags.edges
posts.forEach(post => {
createPage({
path: `/blog/${post.node.title.toLowerCase().replace(/ /g, "-")}`,
component: blogPostTemplate,
context: {
title: post.node.title,
body: post.node.body,
id: post.node.strapiId,
thumbnail: post.node.thumbnail,
tags: post.node.tags,
category: post.node.category.name,
createdAt: post.node.createdAt,
like: post.node.like,
},
})
})
categories.forEach(category => {
createPage({
path: `/blog/${category.node.name.toLowerCase().replace(/ /g, "-")}`,
component: categoryTemplate,
context: {
name: category.node.name,
description: category.node.description,
id: category.node.strapiId,
thumbnail: category.node.thumbnail,
},
})
})
tags.forEach(tag => {
createPage({
path: `/blog/${tag.node.name.toLowerCase().replace(/ /g, "-")}`,
component: tagTemplate,
context: {
name: tag.node.name,
id: tag.node.strapiId,
thumbnail: tag.node.thumbnail,
},
})
})
}