forked from kbariotis/kostasbariotis.com
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgatsby-node.js
More file actions
214 lines (200 loc) · 5.29 KB
/
Copy pathgatsby-node.js
File metadata and controls
214 lines (200 loc) · 5.29 KB
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
214
const path = require('path');
// const { createFilePath } = require(`gatsby-source-filesystem`);
// 创建 node
exports.onCreateNode = ({ node, boundActionCreators }) => {
if (node.internal.type === 'MarkdownRemark') {
const { createNodeField } = boundActionCreators;
// const slug = createFilePath({ node, getNode, basePath: 'pages' });
createNodeField({
node,
name: 'slug',
value: node.frontmatter.path
});
}
};
/**
* This is where all starts
*/
exports.createPages = ({ boundActionCreators, graphql }) => {
const { createPage } = boundActionCreators;
/**
* Fetch our posts
*/
return graphql(`
{
publishedPosts: allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
filter: { frontmatter: { draft: { ne: true } } }
) {
edges {
next {
frontmatter {
path
}
}
previous {
frontmatter {
path
}
}
node {
excerpt(pruneLength: 250)
html
id
timeToRead
frontmatter {
date(formatString: "YYYY-MM-DD HH:mm:ss")
path
tags
title
draft
}
}
}
}
draftPosts: allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
filter: { frontmatter: { draft: { eq: true } } }
) {
edges {
next {
frontmatter {
path
}
}
previous {
frontmatter {
path
}
}
node {
excerpt(pruneLength: 250)
html
id
timeToRead
frontmatter {
date(formatString: "YYYY-MM-DD HH:mm:ss")
path
tags
title
draft
}
}
}
}
}
`).then((results) => {
if (results.errors) {
return Promise.reject(results.errors);
}
const published = results.data.publishedPosts && results.data.publishedPosts.edges;
const drafts = results.data.draftPosts && results.data.draftPosts.edges;
if (isArray(published)) {
generateContent(createPage, published);
createTagPages(createPage, published);
createPagination(createPage, published, `/page`);
}
if (isArray(drafts)) {
generateContent(createPage, drafts);
createPagination(createPage, drafts, `/drafts/page`);
}
});
};
function isArray(o) {
return Object.prototype.toString.call(o) == '[object Array]';
}
function generateContent(createPage, posts) {
const blogPostTemplate = path.resolve(`src/templates/blog-post.js`);
/**
* Create pages for each markdown file.
*/
posts.forEach(({ node, next, previous: pre }) => {
createPage({
path: node.frontmatter.path,
component: blogPostTemplate,
context: {
mainPostPath: node.frontmatter.path,
nextPostPath: next ? next.frontmatter.path : 'none',
prePostPath: pre ? pre.frontmatter.path : 'none'
}
});
});
}
/**
* Create pages for tags
*/
function createTagPages(createPage, edges) {
const tagTemplate = path.resolve(`src/templates/tags.js`);
const posts = {};
edges.forEach(({ node }) => {
if (node.frontmatter.tags) {
node.frontmatter.tags.split(', ').forEach((tag) => {
if (!posts[tag]) {
posts[tag] = [];
}
posts[tag].push(node);
});
}
});
Object.keys(posts).forEach((tagName) => {
const pageSize = 5;
const tagLength = posts[tagName].length;
const pagesSum = Math.ceil(tagLength / pageSize);
for (let page = 1; page <= pagesSum; page++) {
createPage({
path: page === 1 ? `/tag/${tagName.toLowerCase()}` : `/tag/${tagName.toLowerCase()}/page/${page}`,
component: tagTemplate,
context: {
posts: paginate(posts[tagName], pageSize, page),
length: tagLength,
tag: tagName,
pagesSum,
page
}
});
}
});
}
/**
* Create pagination for posts
*/
function createPagination(createPage, edges, pathPrefix) {
const pageTemplate = path.resolve(`src/templates/page.js`);
const pageSize = 5;
const length = edges.length;
const pagesSum = Math.ceil(length / pageSize);
for (let page = 1; page <= pagesSum; page++) {
createPage({
path: `${pathPrefix}/${page}`,
component: pageTemplate,
context: {
posts: paginate(edges, pageSize, page).map(({ node }) => node),
page,
pagesSum,
length,
prevPath: page - 1 > 0 ? `${pathPrefix}/${page - 1}` : null,
nextPath: page + 1 <= pagesSum ? `${pathPrefix}/${page + 1}` : null
}
});
}
}
function paginate(array, page_size, page_number) {
return array.slice(0).slice((page_number - 1) * page_size, page_number * page_size);
}
exports.modifyWebpackConfig = ({ config, stage }) => {
if (stage === 'build-html') {
config.loader('null', {
test: /gitalk/,
loader: 'null-loader'
});
}
// If production JavaScript and CSS build
if (stage === 'build-javascript') {
// Turn off source maps
config.merge({
devtool: false
});
}
};