-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
188 lines (165 loc) · 5.33 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
const _ = require("lodash");
const path = require("path");
const Promise = require("bluebird");
const fs = require('fs');
const { createFilePath } = require(`gatsby-source-filesystem`);
const { blogPostTeaserFields, blogPostSort } = require(`./src/fragments.js`);
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode });
const fileNode = getNode(node.parent);
const source = fileNode.sourceInstanceName;
const separtorIndex = ~slug.indexOf("--") ? slug.indexOf("--") : 0;
const shortSlugStart = separtorIndex ? separtorIndex + 2 : 0;
if (source !== "parts") {
createNodeField({
node,
name: `slug`,
value: `${separtorIndex ? "/" : ""}${slug.substring(shortSlugStart)}`
});
}
createNodeField({
node,
name: `prefix`,
value: separtorIndex ? slug.substring(1, separtorIndex) : ""
});
createNodeField({
node,
name: `source`,
value: source
});
}
};
function createPaginationJSON(pathSuffix, pagePosts) {
const dir = "public/paginationJson/"
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
const filePath = dir+"index"+pathSuffix+".json";
const dataToSave = JSON.stringify(pagePosts);
fs.writeFile(filePath, dataToSave, function(err) {
if(err) {
return console.log(err);
}
});
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return new Promise((resolve, reject) => {
const postTemplate = path.resolve("./src/templates/PostTemplate.js");
const pageTemplate = path.resolve("./src/templates/PageTemplate.js");
const tagTemplate = path.resolve("./src/templates/TagTemplate.js");
const activeEnv = process.env.ACTIVE_ENV || process.env.NODE_ENV || "development"
console.log(`Using environment config: '${activeEnv}'`)
let filters = `filter: { fields: { slug: { ne: null } } }`;
resolve(
graphql(
`
{
allMarkdownRemark(
` + filters + `
` + blogPostSort + `
) {
` + blogPostTeaserFields + `
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors);
reject(result.errors);
}
var items = result.data.allMarkdownRemark.edges;
// Don't leak drafts into production.
if (activeEnv == "production") {
items = items.filter(item =>
item.node.fields.prefix &&
!(item.node.fields.prefix+"").startsWith("draft")
)
}
// Create tags list
const tagSet = new Set();
items.forEach(edge => {
const {
node: {
frontmatter: { tags }
}
} = edge;
if (tags && tags != null) {
tags.forEach(tag => {
if (tag && tag !== null) {
tagSet.add(tag);
}
})
}
});
// Create tag pages
const tagList = Array.from(tagSet);
tagList.forEach(tag => {
createPage({
path: `/tag/${_.kebabCase(tag)}/`,
component: tagTemplate,
context: {
tag
}
});
});
// Create posts
const posts = items.filter(item => item.node.fields.source === "posts");
posts.forEach(({ node }, index) => {
const slug = node.fields.slug;
const prev = index === 0 ? undefined : posts[index - 1].node;
const next = index === posts.length - 1 ? undefined : posts[index + 1].node;
const source = node.fields.source;
createPage({
path: slug,
component: postTemplate,
context: {
slug,
next,
prev,
source
}
});
});
// and pages.
const pages = items.filter(item => item.node.fields.source === "pages");
pages.forEach(({ node }) => {
const slug = node.fields.slug;
const source = node.fields.source;
createPage({
path: slug,
component: pageTemplate,
context: {
slug,
source
}
});
});
// Create "paginated homepage" == pages which list blog posts.
// And at the same time, create corresponding JSON for infinite scroll.
// Users who have JS enabled will see infinite scroll instead of pagination.
const postsPerPage = 3;
const numPages = Math.ceil(posts.length / postsPerPage);
_.times(numPages, i => {
const pathSuffix = (i>0 ? i+1 : "");
// Get posts for this page
const startInclusive = i * postsPerPage;
const endExclusive = startInclusive + postsPerPage;
const pagePosts = posts.slice(startInclusive, endExclusive)
createPaginationJSON(pathSuffix, pagePosts);
createPage({
path: `/`+pathSuffix,
component: path.resolve("./src/templates/index.js"),
context: {
numPages,
currentPage: i + 1,
initialPosts: pagePosts
}
});
});
})
);
});
};