forked from ethereum/ethereum-org-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
217 lines (192 loc) · 5.79 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
// https://www.gatsbyjs.org/docs/node-apis/
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const gatsbyConfig = require(`./gatsby-config.js`)
const { getLangPages } = 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
}
}
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
// only edit markdown nodes
if (node.internal.type === `Mdx`) {
let slug = createFilePath({ node, getNode, basePath: `content` })
// configure language paths
if (slug.includes("/translations/")) {
slug = slug.replace("/translations", "")
} else {
slug = `/en${slug}`
}
// Get relative path for Github API queries (file commit history)
const absolutePath = node.fileAbsolutePath
const relativePathStart = absolutePath.indexOf("src/")
const relativePath = absolutePath.substring(relativePathStart)
createNodeField({
node,
name: `slug`,
value: slug,
})
createNodeField({
node,
name: `relativePath`,
value: relativePath,
})
}
}
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const result = await graphql(`
query {
allMdx {
edges {
node {
fields {
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
const relativePath = node.fields.relativePath
// 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`
}
createPage({
path: slug,
component: path.resolve(`./src/templates/${template}.js`),
context: {
slug,
relativePath,
// create `intl` object so `gatsby-plugin-intl` will skip
// generating language variations for this page
intl: {
language: node.frontmatter.lang,
languages: supportedLanguages,
messages: getMessages("./src/intl/", node.frontmatter.lang),
routed: true,
originalPath: slug.substr(3),
redirect: false,
},
},
})
})
// Create conditional pages
// Necessary because placing these components within src/pages/
// (e.g. src/pages/eth.js ) would overwrite pages generated from markdown,
// including all translations (e.g. src/content/translations/de/eth/index.md)
// TODO create flexibility as we add more pages,
// currently `versionTwoPages` are just English
const versionTwoPages = [
`assets`,
`eth`,
`dapps`,
`developers/index`,
`developers/learning-tools`,
`developers/local-environment`,
`developers/tutorials`,
`wallets/index`,
`wallets/find-wallet`,
`what-is-ethereum`,
]
versionTwoPages.forEach((page) => {
const component = page
// Account for nested pages
if (page.includes("/index")) {
page = page.replace("/index", "")
}
createPage({
path: `/en/${page}/`,
component: path.resolve(`./src/pages-conditional/${component}.js`),
context: {
slug: `/en/${page}/`,
intl: {
language: `en`,
languages: supportedLanguages,
messages: getMessages("./src/intl/", "en"),
routed: true,
originalPath: `/en/${page}/`,
redirect: false,
},
},
})
})
}
exports.onCreatePage = ({ page, actions: { deletePage } }) => {
const lang = page.context.language
// Delete `/build/` page from English (it's now `/developers/learning-tools/`)
if (lang === defaultLanguage && page.component.includes(`/pages/build.js`)) {
deletePage(page)
}
// Delete page if not supported in language version
if (lang !== defaultLanguage && page.component.includes(`/src/pages/`)) {
const langPageComponents = getLangPages(lang)
const component = page.component.split("/").pop() // e.g. 'build.js'
if (!langPageComponents.includes(component)) {
deletePage(page)
}
}
}
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)
}