-
Notifications
You must be signed in to change notification settings - Fork 71
/
gatsby-node.ts
307 lines (264 loc) · 7.93 KB
/
gatsby-node.ts
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import type {GatsbyNode} from "gatsby"
import fs from "fs";
import path from "path";
import {createFilePath} from "gatsby-source-filesystem";
interface MDFile {
extension?: string;
dir: string;
name: string;
absolutePath: string;
}
export const createPages: GatsbyNode["createPages"] = async ({graphql, actions, getNode, reporter}) => {
const {createPage, createRedirect} = actions;
const redirectsFile = {
redirects: []
};
const files = await graphql(
`
{
allFile {
edges {
node {
absolutePath
dir
name
}
}
}
}
`
);
const slugs = await graphql(
`{
allMarkdownRemark {
edges {
node {
fields {
slug
title
redirect {
from
to
}
menu_order
menu_title
section_title
nodeId
}
fileAbsolutePath
}
}
}
}`
);
if (files.errors || slugs.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
const docsPageTemplate = path.resolve('src/templates/docs/index.tsx');
// @ts-ignore
files.data.allFile.edges.forEach(({node}: { node: MDFile }) => {
const {absolutePath, dir, name} = node;
const dirStart = dir.split('content/rsk-devportal')[1];
const pageSlug = `${dirStart.startsWith('/_') ? dirStart.substring(2) : dirStart }/${name === 'index' ? '' : `${name}/`}`;
let slug = null;
let title = null;
let redirect: { from: string, to: string } | null = null;
let menu_order = null;
let menu_title = null;
let section_title = null;
let external = null;
let nodeId = null;
// @ts-ignore
slugs.data.allMarkdownRemark.edges.forEach(({node}) => {
if (node.fields && node.fileAbsolutePath === absolutePath) {
slug = node.fields.slug;
title = node.fields.title;
redirect = node.fields.redirect;
menu_order = node.fields.menu_order;
menu_title = node.fields.menu_title;
section_title = node.fields.section_title;
external = node.fields.external;
nodeId = node.fields.nodeId;
if (redirect) {
// @ts-ignore
redirectsFile.redirects.push({
source: redirect.from || pageSlug,
destination: redirect.to,
permanent: true,
});
// @ts-ignore
redirectsFile.redirects.push({
source: (redirect.from || pageSlug).slice(0, -1),
destination: redirect.to,
permanent: true,
});
}
}
});
if (!slug) return;
if (redirect) {
// @ts-ignore
createRedirect({ fromPath: redirect.from || pageSlug, toPath: redirect.to, isPermanent: true });
}
if (pageSlug.startsWith('/webinars/')) {
createPage({
path: pageSlug,
component: path.resolve('src/templates/webinars/single.tsx'),
// In your blog post template's graphql query, you can use pagePath
// as a GraphQL variable to query for data from the markdown file.
context: {
slug,
title,
nodeId,
},
});
} else {
createPage({
path: pageSlug,
component: docsPageTemplate,
// In your blog post template's graphql query, you can use pagePath
// as a GraphQL variable to query for data from the markdown file.
context: {
slug,
title,
menu_order,
menu_title,
section_title,
external,
nodeId
},
});
}
});
// @ts-ignore
redirectsFile.redirects.push({
source: '/grants/',
destination: '/',
permanent: true,
});
// @ts-ignore
redirectsFile.redirects.push({
source: '/grants',
destination: '/',
permanent: true,
});
// @ts-ignore
redirectsFile.redirects.push({
source: '/discord',
destination: 'https://rootstock.io/discord',
permanent: true,
});
fs.writeFileSync(path.join(__dirname, '/vercel.json'), JSON.stringify(redirectsFile));
}
export const onCreateNode: GatsbyNode["onCreateNode"] = ({node, getNode, actions}) => {
const {createNodeField} = actions;
const basePath = "/content/rsk-devportal/";
if (node.internal.type === "MarkdownRemark") {
const relativeFilePath = createFilePath({
node,
getNode,
basePath: `.${basePath}`,
});
// @ts-ignore
//if (relativeFilePath === '/' && !node.fileAbsolutePath.includes(`${basePath}index.md`)) return;
// // @ts-ignore
// const dirStart = node.fileAbsolutePath.split('content/rsk-devportal')[1];
//
// // @ts-ignore
// const pageSlug = `${dirStart.startsWith('/_') ? dirStart.substring(2) : dirStart }`.split('/').slice(0, -1).join('/');
//
// // @ts-ignore
// const fileName = node.fileAbsolutePath.split(pageSlug)[1];
//
// // @ts-ignore
// const slug = node.frontmatter.redirect ? node.id : `${pageSlug}${fileName === '/index.md' ? '' : fileName.split('.md')[0]}/`;
// @ts-ignore
const slug = node.frontmatter.redirect ? node.id : relativeFilePath;
createNodeField({
node,
name: "title",
// @ts-ignore
value: node.frontmatter.title,
});
createNodeField({
node,
name: "slug",
// @ts-ignore
value: slug,
});
createNodeField({
node,
name: "menu_title",
// @ts-ignore
value: node.frontmatter.menu_title,
});
createNodeField({
node,
name: "section_title",
// @ts-ignore
value: node.frontmatter.section_title,
});
createNodeField({
node,
name: "menu_order",
// @ts-ignore
value: node.frontmatter.menu_order,
});
createNodeField({
node,
name: "external",
// @ts-ignore
value: node.frontmatter.external,
});
createNodeField({
node,
name: "nodeId",
// @ts-ignore
value: node.id,
});
// @ts-ignore
if (node.frontmatter.layout === 'redirect') {
createNodeField({
node,
name: "redirect",
value: {
// @ts-ignore
from: node.frontmatter.permalink,
// @ts-ignore
to: node.frontmatter.redirect
},
});
}
}
}
export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] = ({actions}) => {
const {createTypes} = actions;
const typeDefs = `
type MarkdownRemark implements Node {
frontmatter: Frontmatter
}
type Frontmatter {
title: String!
description: String
layout: String
redirect: String
permalink: String
menu_order: Int
menu_title: String
section_title: String
external: String
}
`;
createTypes(typeDefs);
}
export const onPostBuild: GatsbyNode['onPostBuild'] = () => {
fs.cpSync(path.join(__dirname, '/content/rsk-devportal/dist/images'), path.join(__dirname, '/public/dist/images'), {recursive: true})
fs.cpSync(path.join(__dirname, '/content/rsk-devportal/assets'), path.join(__dirname, '/public/assets'), {recursive: true})
fs.cpSync(path.join(__dirname, '/content/rsk-devportal/grants'), path.join(__dirname, '/public/grants'), {recursive: true})
fs.cpSync(path.join(__dirname, '/content/rsk-devportal/webinars/img'), path.join(__dirname, '/public/assets/webinars/img'), {recursive: true})
fs.cpSync(path.join(__dirname, '/content/rsk-devportal/libraries'), path.join(__dirname, '/public/libraries'), {recursive: true})
fs.cpSync(path.join(__dirname, '/content/rsk-devportal/rif'), path.join(__dirname, '/public/rif'), {recursive: true})
fs.cpSync(path.join(__dirname, '/content/rsk-devportal/tutorials'), path.join(__dirname, '/public/tutorials'), {recursive: true})
fs.cpSync(path.join(__dirname, '/content/rsk-devportal/webinars'), path.join(__dirname, '/public/webinars'), {recursive: true})
}