-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
161 lines (153 loc) · 4.28 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
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { paginate } = require('gatsby-awesome-pagination');
// eslint-disable-next-line consistent-return
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const queryResult = await graphql(`
query {
wp {
allSettings {
readingSettingsPostsPerPage
}
}
allWpPost {
tags: group(field: tags___nodes___name) {
fieldValue
edges {
node {
slug
}
}
}
categories: group(field: categories___nodes___name) {
fieldValue
edges {
node {
slug
}
}
}
nodes {
slug
id
}
edges {
node {
slug
id
}
next {
slug
}
previous {
slug
}
}
}
allWpPage {
nodes {
slug
id
}
}
allWpUser {
nodes {
slug
}
}
}
`);
if (queryResult.errors) {
queryResult.errors.forEach(e => console.error(e.toString()));
return Promise.reject(queryResult.errors);
}
// Create Author pages
queryResult.data.allWpUser.nodes.forEach(user => {
createPage({
path: `author/${user?.slug}`,
component: require.resolve('./src/templates/userTemplate.jsx'),
context: {
// This is the $slug variable
// passed to blog-post.js
slug: user?.slug,
},
});
});
// * POST
// Create a page view for each blog post
queryResult.data.allWpPost.edges.forEach(edge => {
createPage({
path: `post/${edge.node?.slug}`,
component: require.resolve('./src/templates/postTemplate.jsx'),
context: {
// This is the $slug variable
// passed to blog-post.js
slug: edge.node?.slug,
nextPostSlug: edge.next?.slug,
previousPostSlug: edge.previous?.slug,
// added for testing post previews with wp gatsby (doesn't work)
id: edge.node.id,
},
});
});
// * PAGE
// Create a page view for each wordpres page
queryResult.data.allWpPage.nodes.forEach(page => {
createPage({
path: `page/${page?.slug}`,
component: require.resolve('./src/templates/pageTemplate.jsx'),
context: {
// This is the $slug variable
// passed to blog-post.js
slug: page?.slug,
// added for testing post previews with wp gatsby (doesn't work)
id: page.id,
},
});
});
// * BLOG ARCHIVE PAGE
// Create a paginated blog, e.g., /, /page/2, /page/3
const blogPosts = queryResult.data.allWpPost.nodes;
const blogTemplate = require.resolve('./src/templates/blogTemplate.jsx');
// Set to fixed because of responsive layout fine-tuning
// const postsPerPage = queryResult.data.wp.allSettings.readingSettingsPostsPerPage;
paginate({
createPage,
items: blogPosts,
itemsPerPage: 6,
path: 'blog/',
pathPrefix: ({ pageNumber }) => (pageNumber === 0 ? 'blog' : 'blog'),
component: blogTemplate,
context: { type: 'blog' },
});
// * CATEGORY
// Create pages and list posts for each category
const categoryTemplate = require.resolve('./src/templates/categoryTemplate.jsx');
queryResult.data.allWpPost.categories.forEach(category => {
const str = category.fieldValue;
const slug = str.replace(/\s+/g, '-').toLowerCase();
paginate({
createPage,
items: category.edges,
itemsPerPage: 6,
pathPrefix: ({ pageNumber }) => (pageNumber === 0 ? `category/${slug}` : `${slug}`),
component: categoryTemplate,
context: { slug, category: category.fieldValue },
});
});
// * TAG
// Create pages and list posts for each tag
const tagTemplate = require.resolve('./src/templates/tagTemplate.jsx');
queryResult.data.allWpPost.tags.forEach(tag => {
const str = tag.fieldValue;
const slug = str.replace(/\s+/g, '-').toLowerCase();
paginate({
createPage,
items: tag.edges,
itemsPerPage: 6,
pathPrefix: ({ pageNumber }) => (pageNumber === 0 ? `tag/${slug}` : `${slug}`),
component: tagTemplate,
context: { slug, tag: tag.fieldValue },
});
});
};