forked from nishantwrp/gsoc-organizations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
135 lines (121 loc) · 3.47 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
const slugify = require("slugify")
const { compileData } = require("./api/compile-data")
const getAllNodesData = organizations => {
const filtersIndexes = {
years: {},
technologies: {},
topics: {},
categories: {},
}
const allYears = []
const allTechnologies = []
const allTopics = []
const allCategories = []
for (const organization of organizations) {
const years = Object.keys(organization.years)
const technologies = organization.technologies
const topics = organization.topics
const category = organization.category
if (!(category in filtersIndexes.categories)) {
allCategories.push({
name: category,
frequency: 1,
})
filtersIndexes.categories[category] = allCategories.length - 1
} else {
allCategories[filtersIndexes.categories[category]].frequency++
}
for (const topic of topics) {
if (!(topic in filtersIndexes.topics)) {
allTopics.push({
name: topic,
frequency: 1,
})
filtersIndexes.topics[topic] = allTopics.length - 1
} else {
allTopics[filtersIndexes.topics[topic]].frequency++
}
}
for (const technology of technologies) {
if (!(technology in filtersIndexes.technologies)) {
allTechnologies.push({
name: technology,
frequency: 1,
})
filtersIndexes.technologies[technology] = allTechnologies.length - 1
} else {
allTechnologies[filtersIndexes.technologies[technology]].frequency++
}
}
for (const year of years) {
if (!(year in filtersIndexes.years)) {
allYears.push({
name: year,
frequency: 1,
})
filtersIndexes.years[year] = allYears.length - 1
} else {
allYears[filtersIndexes.years[year]].frequency++
}
}
}
return {
Filter: {
years: allYears,
topics: allTopics,
categories: allCategories,
technologies: allTechnologies,
},
Organization: organizations,
}
}
exports.sourceNodes = async ({
actions,
createNodeId,
createContentDigest,
}) => {
const { createNode } = actions
const addNode = (nodeType, id, nodeObject) => {
const gatsbyNode = {
...nodeObject,
id: createNodeId(`${nodeType}-${id}`),
parent: null,
children: [],
internal: {
type: nodeType,
contentDigest: createContentDigest(nodeObject),
content: JSON.stringify(nodeObject),
},
}
createNode(gatsbyNode)
}
const organizations = compileData()
const nodesData = getAllNodesData(organizations)
for (const [nodeType, nodeObjects] of Object.entries(nodesData)) {
if (Array.isArray(nodeObjects)) {
for (const [index, nodeObject] of Object.entries(nodeObjects)) {
addNode(nodeType, index, nodeObject)
}
} else {
addNode(nodeType, "id", nodeObjects)
}
}
}
exports.createPages = ({ actions: { createPage } }) => {
const organizations = compileData()
for (const organization of organizations) {
for (const year of Object.keys(organization.years)) {
organization.years[year].projects = organization.years[year].projects.map(
project => {
delete project["description"]
return project
}
)
}
createPage({
path: `organization/${slugify(organization.name, { lower: true })}/`,
component: require.resolve("./src/templates/organization.jsx"),
context: { organization: organization },
})
}
}