forked from ethereum/ethereum-org-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
318 lines (292 loc) · 9.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
// https://www.gatsbyjs.org/docs/node-apis/
const fs = require("fs")
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const gatsbyConfig = require(`./gatsby-config.js`)
const { getLangContentVersion } = require(`./src/utils/translations`)
const supportedLanguages = gatsbyConfig.siteMetadata.supportedLanguages
const defaultLanguage = gatsbyConfig.siteMetadata.defaultLanguage
// same function from 'gatsby-plugin-intl'
const flattenMessages = (nestedMessages, prefix = "") => {
return Object.keys(nestedMessages).reduce((messages, key) => {
let value = nestedMessages[key]
let prefixedKey = prefix ? `${prefix}.${key}` : key
if (typeof value === "string") {
messages[prefixedKey] = value
} else {
Object.assign(messages, flattenMessages(value, prefixedKey))
}
return messages
}, {})
}
// same function from 'gatsby-plugin-intl'
const getMessages = (path, language) => {
try {
const messages = require(`${path}/${language}.json`)
return flattenMessages(messages)
} catch (error) {
if (error.code === "MODULE_NOT_FOUND") {
process.env.NODE_ENV !== "test" &&
console.error(
`[gatsby-plugin-intl] couldn't find file "${path}/${language}.json"`
)
}
throw error
}
}
const outdatedMarkdownPages = [
"/dapps/",
"/enterprise/",
"/eth/",
"/learn/",
"/wallets/",
"/what-is-ethereum/",
]
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
// Edit markdown nodes
if (node.internal.type === `Mdx`) {
let slug = createFilePath({ node, getNode, basePath: `content` })
let isOutdated = false
if (slug.includes("/translations/")) {
slug = slug.replace("/translations", "")
const split = slug.split("/")
split.splice(1, 1)
const originalPath = split.join("/")
if (outdatedMarkdownPages.includes(originalPath)) {
isOutdated = true
}
} else {
slug = `/${defaultLanguage}${slug}`
}
const absolutePath = node.fileAbsolutePath
const relativePathStart = absolutePath.indexOf("src/")
const relativePath = absolutePath.substring(relativePathStart)
// Boolean if page is outdated (most translated files are)
createNodeField({
node,
name: `isOutdated`,
value: isOutdated,
})
// Page URI
createNodeField({
node,
name: `slug`,
value: slug,
})
// Relative path of file (for GitHub API commit history)
createNodeField({
node,
name: `relativePath`,
value: relativePath,
})
}
}
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const result = await graphql(`
query {
allMdx {
edges {
node {
fields {
isOutdated
slug
relativePath
}
frontmatter {
lang
template
}
}
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild('🚨 ERROR: Loading "createPages" query')
}
result.data.allMdx.edges.forEach(({ node }) => {
const slug = node.fields.slug
// Set template of markdown files
const nodeTemplate = node.frontmatter.template
let template = nodeTemplate ? nodeTemplate : `static`
if (slug.includes(`/tutorials/`)) {
template = `tutorial`
} else if (slug.includes(`/docs/`)) {
template = `docs`
}
const isLegal =
slug.includes(`/cookie-policy/`) ||
slug.includes(`/privacy-policy/`) ||
slug.includes(`/terms-of-use/`) ||
slug.includes(`/contributing/`)
const language = node.frontmatter.lang
const relativePath = node.fields.relativePath
// If markdown file is English, check for corresponding file in each language.
// e.g. English file: "src/content/community/index.md"
// e.g. corresponding German file: "src/content/translations/de/community/index.md"
if (language === defaultLanguage && !isLegal) {
for (const lang of supportedLanguages) {
const splitPath = relativePath.split("/")
splitPath.splice(2, 0, `translations/${lang}`)
const langPath = splitPath.join("/")
// If corresponding file doesn't exist, create a page for it.
if (!fs.existsSync(langPath)) {
const splitSlug = slug.split("/")
splitSlug.splice(1, 1, lang)
const langSlug = splitSlug.join("/")
createPage({
path: langSlug,
component: path.resolve(`./src/templates/${template}.js`),
context: {
slug: langSlug,
isOutdated: false,
isContentEnglish: true,
relativePath: relativePath, // Use English path for template MDX query
// Create `intl` object so `gatsby-plugin-intl` will skip
// generating language variations for this page
intl: {
language: lang,
defaultLanguage,
languages: supportedLanguages,
messages: getMessages("./src/intl/", lang),
routed: true,
originalPath: slug.substr(3),
redirect: false,
},
},
})
}
}
}
createPage({
path: slug,
component: path.resolve(`./src/templates/${template}.js`),
context: {
slug,
isOutdated: node.fields.isOutdated,
relativePath: relativePath,
// Create `intl` object so `gatsby-plugin-intl` will skip
// generating language variations for this page
intl: {
language,
defaultLanguage,
languages: supportedLanguages,
messages: getMessages("./src/intl/", language),
routed: true,
originalPath: slug.substr(3),
redirect: false,
},
},
})
})
// Create contentVersion v2.0 pages
const contentV2Pages = [`eth`, `dapps`, `wallets/index`, `what-is-ethereum`]
const contentV2Languages = supportedLanguages.filter(
(lang) => getLangContentVersion(lang) >= 2.0
)
contentV2Pages.forEach((page) => {
const component = page
// Account for nested pages
if (page.includes("/index")) {
page = page.replace("/index", "")
}
contentV2Languages.forEach((lang) => {
createPage({
path: `/${lang}/${page}/`,
component: path.resolve(`./src/pages-conditional/${component}.js`),
context: {
slug: `/${lang}/${page}/`,
intl: {
language: lang,
languages: supportedLanguages,
defaultLanguage,
messages: getMessages("./src/intl/", lang),
routed: true,
originalPath: `/${page}/`,
redirect: false,
},
},
})
})
})
// Create contentVersion v1.0 pages
// v1.0 doesn't have existing markdown files for these pages
const contentV1Pages = [`eth`, `dapps`, `wallets/index`]
const contentV1Languages = supportedLanguages.filter(
(lang) => getLangContentVersion(lang) === 1.0
)
contentV1Pages.forEach((page) => {
const component = page
// Account for nested pages
if (page.includes("/index")) {
page = page.replace("/index", "")
}
contentV1Languages.forEach((lang) => {
createPage({
path: `/${lang}/${page}/`,
component: path.resolve(`./src/pages-conditional/${component}.js`),
context: {
slug: `/${lang}/${page}/`,
isContentEnglish: true,
intl: {
language: lang,
languages: supportedLanguages,
defaultLanguage,
messages: getMessages("./src/intl/", lang),
routed: true,
originalPath: `/${page}/`,
redirect: false,
},
},
})
})
})
}
// Add additional context to translated pages
// https://www.gatsbyjs.com/docs/creating-and-modifying-pages/#pass-context-to-pages
exports.onCreatePage = ({ page, actions }) => {
const { createPage, deletePage } = actions
const isTranslated = page.context.language !== defaultLanguage
const hasNoContext = page.context.isOutdated === undefined
const langVersion = getLangContentVersion(page.context.language)
if (isTranslated && hasNoContext) {
let isOutdated = false
if (page.component.includes("src/pages/index.js")) {
isOutdated = true
}
deletePage(page)
createPage({
...page,
context: {
...page.context,
isOutdated,
//display TranslationBanner for translation-component pages that are still in English
isContentEnglish:
langVersion < 2 && !page.component.includes("/developers/index.js"),
},
})
}
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type Mdx implements Node {
frontmatter: Frontmatter
}
type Frontmatter {
sidebar: Boolean
sidebarDepth: Int
incomplete: Boolean
template: String
summaryPoints: [String!]!
}
type Eth2BountyHuntersCsv implements Node {
username: String,
name: String,
score: Int
}
`
createTypes(typeDefs)
}