-
Notifications
You must be signed in to change notification settings - Fork 6
/
gatsby-node.js
179 lines (166 loc) · 4.13 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
const path = require(`path`)
const slugify = require("./src/utils/slugify")
const tartanTemplate = path.resolve(`./src/templates/tartan.js`)
const tartansTemplate = path.resolve(`./src/templates/tartans.js`)
const letters = "abcdefghijklmnopqrstuvwxyz".split("")
const pageLength = 60
const { GraphQLJSONObject } = require("graphql-type-json")
const lunr = require(`lunr`)
const paginateNodes = (array, pageLength) => {
const result = Array()
for (let i = 0; i < Math.ceil(array.length / pageLength); i++) {
result.push(array.slice(i * pageLength, (i + 1) * pageLength))
}
return result
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const allTartans = await graphql(`
query {
allTartansCsv {
edges {
node {
id
fields {
slug
}
}
previous {
fields {
slug
Unique_Name
}
}
next {
fields {
slug
Unique_Name
}
}
}
}
}
`)
if (allTartans.errors) {
throw allTartans.errors
}
allTartans.data.allTartansCsv.edges.forEach(({ node, next, previous }) => {
createPage({
path: `/tartan/${node.fields.slug}`,
component: tartanTemplate,
context: {
id: node.id,
previous,
next,
},
})
})
let previousLetterLastIndex = 1
for (const letter of letters) {
const allTartansByLetter = await graphql(`
query {
allTartansCsv(filter: {Name: {regex: "/^${letter}/i"}}) {
nodes {
Palette
fields {
slug
Unique_Name
}
}
totalCount
}
}
`)
if (allTartansByLetter.errors) {
throw allTartansByLetter.errors
}
const nodes = allTartansByLetter.data.allTartansCsv.nodes
const totalCountByLetter = allTartansByLetter.data.allTartansCsv.totalCount
const paginatedNodes = paginateNodes(nodes, pageLength)
paginatedNodes.forEach((group, index, groups) => {
createPage({
path:
index > 0 ? `/tartans/${letter}/${index + 1}` : `/tartans/${letter}`,
component: tartansTemplate,
context: {
group,
index,
last: index === groups.length - 1,
pageCount: groups.length,
letter,
previousLetterLastIndex,
},
})
})
previousLetterLastIndex = Math.ceil(totalCountByLetter / pageLength)
}
}
// we will store slugs here and use this array to check if a new one already exists
let slugs = []
// we add numbers create unique slugs and names
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `TartansCsv`) {
let slug = slugify(node.Name)
let uniqueName = node.Name
if (slugs.indexOf(slug) !== -1) {
slug += `-${i}`
uniqueName += ` ${i}`
i++
} else {
i = 1
}
slugs.push(slug)
createNodeField({
name: `slug`,
node,
value: slug,
})
createNodeField({
name: `Unique_Name`,
node,
value: uniqueName,
})
}
}
exports.createResolvers = ({ cache, createResolvers }) => {
createResolvers({
Query: {
LunrIndex: {
type: GraphQLJSONObject,
resolve(source, args, context) {
const siteNodes = context.nodeModel.getAllNodes({
type: `TartansCsv`,
})
return createIndex(siteNodes, cache)
},
},
},
})
}
const createIndex = async (nodes, cache) => {
const cacheKey = `LunrIndex`
const cached = await cache.get(cacheKey)
if (cached) {
return cached
}
const store = {}
const index = lunr(function() {
this.ref(`slug`)
this.field(`title`)
for (node of nodes) {
const { slug } = node.fields
const doc = {
slug,
title: node.fields.Unique_Name,
}
store[slug] = {
title: doc.title,
}
this.add(doc)
}
})
const json = { index: index.toJSON(), store }
cache.set(cacheKey, json)
return json
}