-
Notifications
You must be signed in to change notification settings - Fork 598
/
gatsby-node.js
228 lines (213 loc) · 5.59 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
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
221
222
223
224
225
226
227
228
const path = require('path')
const _ = require("lodash")
const { createFilePath } = require(`gatsby-source-filesystem`)
const { fmImagesToRelative } = require('gatsby-remark-relative-images');
exports.createPages = ({actions, graphql}) => {
const { createPage } = actions;
const pluginTemplate = path.resolve('src/templates/plugin-page.js');
const pluginTagTemplate = path.resolve("src/templates/plugin-tags.js");
const pluginsSoftwareTemplate = path.resolve("src/templates/plugin-software.js");
const themeTagTemplate = path.resolve("src/templates/theme-tags.js");
const themeTemplate = path.resolve('src/templates/theme-page.js');
return graphql(`{
plugins:allMarkdownRemark(filter: { collection: { eq: "plugins" } }) {
group(field: collection) {
fieldValue
totalCount
}
totalCount
edges {
node {
excerpt
html
id
fields {
slug
}
frontmatter {
title
sub
description
author
github
download
status
support
layout
ghcommentid
date
tags
software
}
}
}
},
themes:allMarkdownRemark(filter: { collection: { eq: "themes" } }) {
group(field: collection) {
fieldValue
totalCount
}
totalCount
edges {
node {
excerpt
html
id
fields {
slug
}
frontmatter {
title
sub
description
author
thumbnail
github
status
download
support
layout
ghcommentid
date
tags
software
}
}
}
},
allMarkdownRemark {
edges {
node {
excerpt
html
id
fields {
slug
}
frontmatter {
title
sub
description
author
thumbnail
github
download
support
layout
ghcommentid
date
tags
software
}
}
}
}
}`)
.then(res => {
if(res.errors) {
return Promise.reject(res.errors);
}
const themes = res.data.themes.edges
const plugins = res.data.plugins.edges
// plugins/tags/software pages:
let software = []
// Iterate through each post, putting all found softwares into `tags`
_.each(plugins, edge => {
if (_.get(edge, "node.frontmatter.software")) {
software = software.concat(edge.node.frontmatter.software)
}
})
// Eliminate duplicate softwares
software = _.uniq(software)
// Make software tag pages
software.forEach(softwares => {
createPage({
path: `/plugins/softwares/${_.kebabCase(softwares)}/`,
component: pluginsSoftwareTemplate,
context: {
softwares,
},
})
})
//Next set
// Theme Tag pages:
let tags = []
// Iterate through each post, putting all found tags into `tags`
_.each(themes, edge => {
if (_.get(edge, "node.frontmatter.tags")) {
tags = tags.concat(edge.node.frontmatter.tags)
}
})
// Eliminate duplicate tags
tags = _.uniq(tags)
// Make tag pages
tags.forEach(tag => {
createPage({
path: `/themes/tags/${_.kebabCase(tag)}/`,
component: themeTagTemplate,
context: {
tag,
},
})
})
//Next set
// Plugin Tag pages:
let plugintags = []
// Iterate through each post, putting all found tags into `tags`
_.each(plugins, edge => {
if (_.get(edge, "node.frontmatter.tags")) {
plugintags = plugintags.concat(edge.node.frontmatter.tags)
}
})
// Eliminate duplicate tags
plugintags = _.uniq(plugintags)
// Make tag pages
plugintags.forEach(tag => {
createPage({
path: `/plugins/tags/${_.kebabCase(tag)}/`,
component: pluginTagTemplate,
context: {
tag,
},
})
})
res.data.plugins.edges.forEach(({ node }) => {
createPage({
path: '/plugins' + node.fields.slug,
component: pluginTemplate,
context: {
slug: node.fields.slug,
}, // additional data can be passed via context
})
})
res.data.themes.edges.forEach(({ node }) => {
createPage({
path: '/themes' + node.fields.slug,
component: themeTemplate,
context: {
slug: node.fields.slug,
}, // additional data can be passed via context
})
})
})
}
// Adds 'collection' to node
exports.onCreateNode =({ node, getNode, actions }) => {
const { createNodeField } = actions;
fmImagesToRelative(node);
if (node.internal.type === `MarkdownRemark`) {
node.collection = getNode(node.parent).sourceInstanceName;
const relativeFilePath = createFilePath({
node,
getNode,
basePath: "pages",
})
const slug = createFilePath({ node, getNode, basePath: `pages` })
// Creates new query'able field with name of 'slug'
createNodeField({
node,
name: "slug",
value: slug,
})
}
}