forked from ethereum/esp-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
139 lines (119 loc) · 3.69 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
// https://www.gatsbyjs.org/docs/node-apis/
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const gatsbyConfig = require(`./gatsby-config.js`)
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 moveArrItem = (arr, fromIndex, toIndex) => {
const element = arr[fromIndex]
arr.splice(fromIndex, 1)
arr.splice(toIndex, 0, element)
}
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
// only edit markdown nodes
if (node.internal.type === `Mdx`) {
let slug = createFilePath({ node, getNode, basePath: `pages` })
// generate slug for internationalization support
// e.g. /projects/circom/en.md --> /en/projects/circom/
// e.g. /projects/circom/ko.md --> /ko/projects/circom/
const split = slug.split("/")
const languageIndex = split.length - 2
const language = split[languageIndex]
if (supportedLanguages.includes(language)) {
moveArrItem(split, languageIndex, 1)
slug = split.join("/")
}
createNodeField({
node,
name: `slug`,
value: slug,
})
}
}
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const result = await graphql(`
query {
allMdx {
edges {
node {
fields {
slug
}
frontmatter {
lang
}
}
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild('🚨 ERROR: Loading "createPages" query')
}
result.data.allMdx.edges.forEach(({ node }) => {
if (!node.frontmatter.lang) {
console.error(
"Page is missing a `lang` frontmatter property: ",
node.fields.slug
)
}
const component = node.fields.slug.includes("/projects/")
? path.resolve(`./src/templates/project-detail.js`)
: path.resolve(`./src/templates/static.js`)
createPage({
path: node.fields.slug,
component,
context: {
slug: node.fields.slug,
// 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: node.fields.slug.substr(3),
redirect: true,
},
},
})
})
}
// Delete page if not supported in language version
exports.onCreatePage = ({ page, actions: { deletePage } }) => {
const lang = page.context.language
// Delete all page components that aren't english
if (lang !== defaultLanguage && page.component.includes(`/src/pages/`)) {
deletePage(page)
}
}