forked from pehaa/tartanify.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
133 lines (123 loc) · 3.12 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
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 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) => {
return 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,
})
}
}