-
Notifications
You must be signed in to change notification settings - Fork 34
/
gatsby-node.js
171 lines (149 loc) Β· 4.63 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
// 1. Generate string versions of tutorial array fields for faster search
///////////////////////////////////////////////////////////////////////////////////
// TODO: unit test this utility
function convertArrayToString(array) {
return array ? array.join(` `).toLowerCase() : ``
}
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
if (node.internal.type === 'TutorialsYaml') {
createNodeField({
node,
name: `formatsAsString`,
value: convertArrayToString(node.formats),
})
createNodeField({
node,
name: `topicsAsString`,
value: convertArrayToString(node.topics),
})
createNodeField({
node,
name: `authorsAsString`,
value: convertArrayToString(node.authors),
})
}
}
// 2. Reorder the tutorials and generate the lists of unique filters
///////////////////////////////////////////////////////////////////////////////////
let indexPageContext = {}
exports.createPages = async ({ graphql, reporter }) => {
const result = await graphql(
`
{
tutorialsWithDates: allTutorialsYaml(
filter: { date: { ne: null } }
sort: { fields: [date], order: DESC }
) {
nodes {
title
link
formats
date(formatString: "MMM DD, YYYY")
length
authors
source
topics
fields {
authorsAsString
formatsAsString
topicsAsString
}
}
}
tutorialsWithoutDates: allTutorialsYaml(
filter: { date: { eq: null } }
sort: { fields: [title], order: ASC }
) {
nodes {
title
link
formats
language
date(formatString: "MMM DD, YYYY")
length
authors
source
topics
fields {
authorsAsString
formatsAsString
topicsAsString
}
}
}
}
`
)
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
// Move tutorials with no date to the end of the list
const { tutorialsWithDates, tutorialsWithoutDates } = result.data
const tutorials = [...tutorialsWithDates.nodes, ...tutorialsWithoutDates.nodes]
// TODO: extract repeated patterns into utilities and test them...
// Create a sorted list of all unique formats
const formatArrays = tutorials.map(tutorial => tutorial.formats)
const formats = [
...new Set(
formatArrays
.reduce((acc, curr) => [...acc, ...curr]) // merge arrays into one
.map(format => format.toLowerCase()) // convert all formats to lowercase
),
].sort()
// Create a sorted list of all unique topics
const topicArrays = tutorials.map(tutorial => tutorial.topics)
const topics = [
...new Set(
topicArrays
.reduce((acc, curr) => [...acc, ...curr]) // merge arrays into one
.map(topic => topic.toLowerCase()) // convert all topics to lowercase
),
].sort()
// Create a sorted list of all unique authors
const authorArrays = tutorials.map(tutorial => tutorial.authors)
const authors = [
...new Set(
authorArrays.reduce((acc, curr) => [...acc, ...curr]) // merge arrays
),
].sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())) // ignore case
// Create a sorted list of all unique sources
const sources = [
...new Set(tutorials.map(tutorial => (tutorial.source ? tutorial.source : ''))),
].sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())) // ignore case
// Will add this object to the index page context in .onCreatePage() below:
indexPageContext = {
tutorials: tutorials,
formats: formats,
topics: topics,
authors: authors,
sources: sources,
}
}
// 3. Add processed tutorials and filter lists to the index page context
///////////////////////////////////////////////////////////////////////////////////
exports.onCreatePage = ({ page, actions }) => {
const { createPage, deletePage } = actions
if (page.path === `/`) {
deletePage(page)
createPage({
...page,
context: indexPageContext,
})
}
}
// Temporary: Alias react-dom to @hot-loader/react-dom to remove console warning
///////////////////////////////////////////////////////////////////////////////////
exports.onCreateWebpackConfig = ({ stage, actions }) => {
if (stage.startsWith('develop')) {
actions.setWebpackConfig({
resolve: {
alias: {
'react-dom': '@hot-loader/react-dom',
},
},
})
}
}