-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
195 lines (167 loc) · 5.26 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
// You can delete this file if you're not using it
const path = require('path')
const _ = require('lodash')
exports.onCreateNode = ({ node }) => {
if (node.internal.type === `MarkdownRemark`) {
console.log(node.internal.type)
}
};
exports.string_to_slug = (str) => {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
var to = "aaaaeeeeiiiioooouuuunc------";
for (var i=0, l=from.length ; i<l ; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
return str;
}
exports.createPages = ({ boundActionCreators, graphql }) => {
const { createPage } = boundActionCreators
const blogPostTemplate = path.resolve(`src/templates/blog-post.js`)
const bitsTemplate = path.resolve(`src/templates/bits-post.js`)
const categoryTemplate = path.resolve(`src/templates/category.js`)
const tagTemplate = path.resolve(`src/templates/tag.js`)
const resourcePageTemplate = path.resolve(`src/templates/resource-page.js`)
const { createRedirect } = boundActionCreators
createRedirect({ fromPath: "/home", toPath: "/", isPermanent: true, redirectInBrowser: true });
// If you are experiencing issues with the ordering of the posts on the homepage,
// replace the `allMarkdownRemark` line below with something like this:
// allMarkdownRemark(sort:{fields:[frontmatter___date], order: ASC}) {
return graphql(`{
allContentfulBlogPost {
edges {
node {
id
title
published
tags
category
excert
url
body {
id
body
}
}
}
}
allContentfulBits {
edges {
node {
id
title
childContentfulBitsBitTextNode {
id
bit
}
}
}
}
allMarkdownRemark(
filter: {fileAbsolutePath: {regex: "/(resources)/.*.md$/"}}
) {
edges {
node {
frontmatter {
path
}
}
}
}
}`)
.then(result => {
if (result.errors) {
console.log(result.errors)
return Promise.reject(result.errors)
}
const posts = result.data.allContentfulBlogPost.edges
const bits = result.data.allContentfulBits.edges
createPage({
path: '/bits',
component: path.resolve('src/templates/bits-list.js'),
context: {}, // additional data can be passed via context
});
bits.forEach(({node}, index) => {
createPage({
path: `/bits/${this.string_to_slug(node.title)}`,
component: bitsTemplate,
context: {
title: node.title,
}
})
});
const resources = result.data.allMarkdownRemark.edges
const categorySet = new Set();
let tagDict = {}
resources.forEach(({ node }) => {
createPage({
path: node.frontmatter.path,
component: resourcePageTemplate,
context: {}, // additional data can be passed via context
});
});
// Create blog post list pages
const postsPerPage = 1000; // No worries less amount of data than JS bundles...I will have a search function
const numPages = Math.ceil(posts.length / postsPerPage);
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/blog` : `/blog/${i + 1}`,
component: path.resolve('src/templates/blog-list.js'),
context: {
limit: postsPerPage,
skip: i * postsPerPage,
numPages,
currentPage: i + 1
}
});
});
posts.forEach(({node}, index) => {
categorySet.add(node.category);
node.tags.forEach(tag => {
tagDict[tag] = (tagDict[tag] || 0) + 1;
})
createPage({
path: node.url,
component: blogPostTemplate,
context: {
prev: index === 0 ? null : posts[index - 1].node,
next: index === (posts.length - 1) ? null : posts[index + 1].node,
url: node.url
}
})
})
const categoryList = Array.from(categorySet);
categoryList.forEach(category => {
console.log(`Category: ${_.kebabCase(category)}`);
createPage({
path: `/categories/${_.kebabCase(category)}/`,
component: categoryTemplate,
context: {
category
}
});
});
tagDict = _(tagDict).toPairs().sortBy(0).fromPairs().value()
const tagList = _.chain(tagDict).toPairs().orderBy([1], ['desc']).fromPairs().keys().value()
tagList.forEach(tag => {
console.log(`Tag: ${_.kebabCase(tag)}`);
createPage({
path: `/tags/${_.kebabCase(tag)}/`,
component: tagTemplate,
context: {
tag
}
});
});
})
}