-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
220 lines (205 loc) · 6.27 KB
/
gatsby-node.js
File metadata and controls
220 lines (205 loc) · 6.27 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
215
216
217
218
219
220
// Dynamic content generation for blogs
const path = require('path')
const crypto = require('crypto')
const dir = require('node-dir')
const matter = require('gray-matter')
const unified = require('unified')
const markdown = require('remark-parse')
const slug = require('remark-slug')
const autoLinkHeadings = require("remark-autolink-headings")
const highlight = require('remark-highlight.js')
const html = require('remark-html')
const algoliasearch = require('algoliasearch')
const authors = require('./src/constants/generated-authors.json')
const categories = require('./src/constants/categories.json')
const highlightedBlogs = require('./src/constants/featured-blogs.json')
const workshops = require('./src/constants/workshops.json')
const blogsPerPage = 5
const highlightedBlogsRegEx = new RegExp(highlightedBlogs.join("|"), 'gi')
var client = algoliasearch(process.env.GATSBY_ALGOLIA_APP_ID, '9a3c539ada1e49ee00b670534c9c605f');
var index = client.initIndex(process.env.GATSBY_ALGOLIA_BLOG_INDEX);
exports.sourceNodes = async ({ boundActionCreators }) => {
const { createNode } = boundActionCreators;
return Promise.all([
new Promise((resolve, reject) => {
dir.readFiles('./content/blog', {
match: /.md$/,
//exclude: /^\./
}, (err, content, filename, next) => {
if (err) throw err
const { data, content: markdownContent } = matter(content)
const frontmatter = data.category ? data : { ...data, category: [] }
const blogId = path.basename(filename, path.extname(filename))
index.saveObject({
title: frontmatter.title,
description: frontmatter.description,
objectID: blogId,
});
unified()
.use(markdown)
.use(highlight)
.use(html)
.process(markdownContent, function(err, file) {
const blog = {
id: blogId,
parent: null,
children: [],
internal: {
type: `Blog`,
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(content))
.digest(`hex`),
},
frontmatter,
content: String(file),
}
createNode(blog)
next()
});
}, resolve);
}),
new Promise((resolve, reject) => {
dir.readFiles('./content/framework', {
match: /.md$/,
//exclude: /^\./
}, (err, content, filename, next) => {
if (err) throw err
const { data: frontmatter, content: markdownContent } = matter(content)
const url = frontmatter.gitLink.replace(/\/README.md|.md/i, '/')
unified()
.use(markdown)
.use(slug)
.use(autoLinkHeadings, {
content: {
type: "text",
value: "#",
},
linkProperties: {
className: "phenomic-HeadingAnchor",
},
})
.use(html)
.use(highlight)
.process(markdownContent, function(err, file) {
const doc = {
id: url,
parent: null,
children: [],
internal: {
type: `Doc`,
contentDigest: crypto
.createHash(`md5`)
.update(JSON.stringify(content))
.digest(`hex`),
},
frontmatter,
content: String(file),
}
createNode(doc)
next()
});
}, resolve);
})
]);
};
exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators
return Promise.all([
new Promise((resolve, reject) => {
graphql(`
{
allBlog {
edges {
node {
id
}
}
}
}
`).then(result => {
const blogs = result.data.allBlog.edges;
for(let i = 0 ; i < blogs.length ; i += blogsPerPage ) {
const page = i / blogsPerPage;
createPage({
path: `blog${ page === 0 ? '' : ('/page/' + (page + 1) ) }`,
component: path.resolve(`./src/templates/blogList.js`),
context: {
limit: 5,
start: i,
highlightedBlogsRegEx,
}
})
}
blogs.forEach(({ node }, index, records) => {
createPage({
path: `blog/${node.id}`,
component: path.resolve(`./src/templates/blog.js`),
context: {
blogId: node.id,
previousBlogId: (records[index - 1] || records[index + 2]).node.id,
nextBlogId: (records[index + 1] || records[index - 2]).node.id,
},
})
})
Object.keys(authors).forEach((authorKey) => {
createPage({
path: `author/${authorKey}`,
component: path.resolve(`./src/templates/author.js`),
context: {
authorId: [authorKey]
},
})
})
Object.keys(categories).forEach((categoryKey) => {
createPage({
path: `category/${categoryKey}`,
component: path.resolve(`./src/templates/category.js`),
context: {
categoryId: [categoryKey]
},
})
})
resolve()
})
}).catch(error => {
console.log(error)
reject()
}),
new Promise((resolve, reject) => {
graphql(`
{
allDoc {
edges {
node {
id
}
}
}
}
`).then(result => {
const docs = result.data.allDoc.edges
docs.forEach(({ node }) => {
createPage({
path: `framework${node.id}`,
component: path.resolve(`./src/templates/doc.js`),
context: {
docId: node.id
},
})
})
resolve()
})
}),
new Promise((resolve, reject) => {
Object.keys(workshops).forEach((key) => {
createPage({
path: `workshops/${key}`,
component: path.resolve(`./src/templates/workshop-form.js`),
context: { key },
})
})
resolve()
})
])
};