-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
gatsby-node.js
213 lines (198 loc) · 4.18 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const path = require('path');
const createPaginatedPages = require('gatsby-paginate');
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`
});
exports.createPages = async ({
graphql,
actions,
reporter
}) => {
const {
createPage
} = actions;
const BlogPostTemplate = path.resolve("./src/templates/BlogPost.tsx");
const BlogTagPostsTemplate = path.resolve("./src/templates/BlogTagPosts.tsx");
const BlogCategoryPostsTemplate = path.resolve("./src/templates/BlogCategoryPosts.tsx");
const BlogPostsResult = await graphql(`
{
allWordpressPost {
edges {
node {
id
slug
status
template
format
wordpress_id
title
excerpt
date(formatString: "MMMM DD, YYYY")
modified(formatString: "MMMM DD, YYYY")
author {
id
name
url
description
link
slug
path
wordpress_id
}
featured_media {
localFile {
childImageSharp {
fluid(maxWidth: 960, maxHeight: 600, quality: 85) {
aspectRatio
src
srcSet
sizes
base64
tracedSVG
srcWebp
srcSetWebp
}
}
}
}
categories {
id
link
wordpress_id
count
description
name
slug
path
}
tags {
id
link
wordpress_id
count
description
name
slug
path
}
}
}
}
allInstaNode(limit: 8) {
edges {
node {
id
likes
comments
mediaType
preview
original
timestamp
caption
localFile {
childImageSharp {
fluid(maxWidth: 960, maxHeight: 600, quality: 85) {
aspectRatio
src
srcSet
sizes
base64
tracedSVG
srcWebp
srcSetWebp
}
}
}
thumbnails {
src
config_width
config_height
}
dimensions {
height
width
}
}
}
}
}`);
if (BlogPostsResult.errors) {
reporter.panicOnBuild('Error while running GraphQL query.');
return;
}
const BlogPosts = BlogPostsResult.data.allWordpressPost.edges;
BlogPosts.forEach((post, index) => {
createPage({
path: `/post/${post.node.slug}`,
component: BlogPostTemplate,
context: {
id: post.node.wordpress_id,
slug: post.node.slug,
previous: index === 0 ? null : BlogPosts[index - 1].node,
next: index === (BlogPosts.length - 1) ? null : BlogPosts[index + 1].node
}
});
});
const BlogTagPosts = new Map();
const BlogCategoryPosts = new Map();
BlogPosts.forEach((post) => {
const tags = post.node.tags;
if (tags && tags.length > 0) {
tags.forEach((tag) => {
if (BlogTagPosts.has(tag.slug)) {
BlogTagPosts.set(tag.slug, [...BlogTagPosts.get(tag.slug), post]);
} else {
BlogTagPosts.set(tag.slug, [post]);
}
});
}
const categories = post.node.categories;
if (categories && categories.length > 0) {
categories.forEach((category) => {
if (BlogCategoryPosts.has(category.slug)) {
BlogCategoryPosts.set(category.slug, [...BlogCategoryPosts.get(category.slug), post]);
} else {
BlogCategoryPosts.set(category.slug, [post]);
}
});
}
});
const BlogTagSlugs = [...BlogTagPosts.keys()];
const BlogCategorySlugs = [...BlogCategoryPosts.keys()];
if (BlogTagSlugs.length > 0) {
BlogTagSlugs.forEach((BlogTagSlug) => {
createPage({
path: `/tag/${BlogTagSlug}`,
component: BlogTagPostsTemplate,
context: {
group: BlogTagPosts.get(BlogTagSlug),
slug: BlogTagSlug,
allInstaNode: BlogPostsResult.data.allInstaNode
}
});
});
}
if (BlogCategorySlugs.length > 0) {
BlogCategorySlugs.forEach((BlogCategorySlug) => {
createPage({
path: `/category/${BlogCategorySlug}`,
component: BlogCategoryPostsTemplate,
context: {
group: BlogCategoryPosts.get(BlogCategorySlug),
slug: BlogCategorySlug,
allInstaNode: BlogPostsResult.data.allInstaNode
}
});
});
}
createPaginatedPages({
edges: BlogPosts,
createPage: createPage,
pageTemplate: 'src/templates/BlogPosts.tsx',
pageLength: 2,
pathPrefix: 'posts',
context: {
allInstaNode: BlogPostsResult.data.allInstaNode
}
});
}