forked from Uvacoder/gatsby-advanced-blog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
209 lines (184 loc) · 5.77 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
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`,
});
const path = require('path');
const { createFilePath } = require('gatsby-source-filesystem');
const {
CONTENT_PER_PAGE,
POST,
PORTFOLIO,
RESUME,
} = require('./src/constants');
exports.onCreateWebpackConfig = ({
stage,
plugins,
actions,
}) => {
actions.setWebpackConfig({
externals: {
document: true,
discus_config: true,
},
resolve: {
alias: {
'~': path.resolve(__dirname, 'src'),
},
},
plugins: [
plugins.define({
__DEVELOPMENT__: stage === 'develop' || stage === 'develop-html',
}),
],
});
};
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return new Promise((resolve, reject) => {
const post = path.resolve('./src/templates/Post.jsx');
const list = path.resolve('./src/templates/List.jsx');
const taggedList = path.resolve('./src/templates/TaggedList.jsx');
const categorizedList = path.resolve('./src/templates/CategorizedList.jsx');
const resume = path.resolve('./src/templates/Resume.jsx');
const portfolios = path.resolve('./src/templates/Portfolios.jsx');
const portfolio = path.resolve('./src/templates/Portfolio.jsx');
resolve(
graphql(`
{
allMarkdownRemark(limit: 10000) {
edges {
node {
frontmatter {
path
category
tags
type
hide
}
}
}
}
}
`).then(({ errors, data: { allMarkdownRemark: { edges } } }) => {
if (errors) {
console.log(errors); // eslint-disable-line no-console
reject(errors);
}
const tagMatrix = [];
const categoryMatrix = [];
// Create blog posts pages.
edges.forEach(({ node: { frontmatter: { path, tags, category, type, hide } } }) => {
if (hide !== true) {
if (Array.isArray(tags)) {
tagMatrix.push(tags);
}
if (typeof category === 'string') {
categoryMatrix.push(category);
}
let component = null;
switch (type) {
case PORTFOLIO:
component = portfolio;
break;
case RESUME:
component = resume;
break;
case POST:
default:
component = post;
break;
}
if (component !== null) {
createPage({
path,
component,
context: {},
});
}
}
});
const portfoliosCount = edges
.filter(({ node: { frontmatter: { type } } }) => (type === PORTFOLIO))
.length;
if (portfoliosCount) {
createPage({
path: '/portfolios',
component: portfolios,
context: {
},
});
}
const postsCount = edges
.filter(({ node: { frontmatter: { hide, type } } }) => (!hide && (type || POST) === POST))
.length;
const pagesCount = postsCount ? (Math.ceil(postsCount / CONTENT_PER_PAGE) + 1) : 1;
const pages = Array.from(new Array(pagesCount), (el, i) => i + 1);
if (pages.length > 0) {
pages.forEach((page) => {
createPage({
path: `/pages/${page}`,
component: list,
context: {
},
});
});
} else {
createPage({
path: '/pages/1',
component: list,
context: {
},
});
}
const tags = [...new Set(tagMatrix.reduce((prev, curr) => (curr !== null ? [...prev, ...curr] : prev), []))];
tags.forEach((tag) => {
const taggedPostCount = edges.reduce((count, { node: { frontmatter: { tags: postTags } } }) => {
if (postTags !== null && postTags.includes(tag)) {
return count + 1;
}
return count;
}, 0);
const taggedListCount = taggedPostCount ? (Math.ceil(taggedPostCount / CONTENT_PER_PAGE) + 1) : 1;
const taggedListPages = Array.from(new Array(taggedListCount), (el, i) => i + 1);
taggedListPages.forEach((taggedListPage) => {
createPage({
path: `/tags/${tag}/${taggedListPage}`,
component: taggedList,
context: {
},
});
});
});
const categories = [...new Set(categoryMatrix)];
categories.forEach((category) => {
const categorizedPostCount = edges.reduce((count, { node: { frontmatter: { category: postCategory } } }) => {
if (postCategory !== null && postCategory.includes(category)) {
return count + 1;
}
return count;
}, 0);
const categorizedListCount = categorizedPostCount ? (Math.ceil(categorizedPostCount / CONTENT_PER_PAGE) + 1) : 1;
const categorizedListPages = Array.from(new Array(categorizedListCount), (el, i) => i + 1);
categorizedListPages.forEach((categorizedListPage) => {
createPage({
path: `/categories/${category}/${categorizedListPage}`,
component: categorizedList,
context: {
},
});
});
});
})
);
});
};
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === 'MarkdownRemark') {
const value = createFilePath({ node, getNode });
createNodeField({
name: 'slug',
node,
value,
});
}
};