-
Notifications
You must be signed in to change notification settings - Fork 78
/
gatsby-node.js
172 lines (146 loc) · 4.48 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
const fs = require(`fs`)
const path = require(`path`)
const mkdirp = require(`mkdirp`)
const { withDefaults } = require(`./utils/default-options`)
const { localizedPath, getLanguages } = require(`./src/helpers`)
function writeFile(filePath, data, reporter) {
// Check if config file already exists
if (!fs.existsSync(filePath)) {
// Create the necessary folders for the default config file
mkdirp(path.dirname(filePath), (err) => {
if (err) {
reporter.panicOnBuild(
`[gatsby-theme-i18n]: Failing to create the directory for the default config file\n\n${err}`
)
return
}
// Create the default config file
fs.writeFile(filePath, data, (err) => {
if (err) {
reporter.panicOnBuild(
`[gatsby-theme-i18n]: Failing to create a default config file\n\n${err}`
)
return
}
reporter.info(
`[gatsby-theme-i18n]: Created a default config file at ${filePath}`
)
})
})
}
}
exports.onPreBootstrap = ({ reporter, store }, themeOptions) => {
const defaultContent = [
{
code: `en`,
hrefLang: `en-US`,
name: `English`,
localName: `English`,
langDir: `ltr`,
dateFormat: `MM/DD/YYYY`,
},
]
const data = JSON.stringify(defaultContent)
const { program } = store.getState()
const defaultConfigPath = path.join(program.directory, `src/i18n/config.json`)
if (themeOptions.configPath) {
if (!fs.existsSync(themeOptions.configPath)) {
reporter.panicOnBuild(
`[gatsby-theme-i18n]: Couldn't find the file at ${themeOptions.configPath}`
)
}
reporter.info(
`[gatsby-theme-i18n]: Config file found at ${themeOptions.configPath}`
)
} else {
writeFile(defaultConfigPath, data, reporter)
}
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
createTypes(`
type ThemeI18n implements Node {
defaultLang: String
configPath: String
config: [Locale]
}
type Locale {
code: String
hrefLang: String
dateFormat: String
langDir: String
localName: String
name: String
}
`)
}
exports.sourceNodes = (
{ actions, createContentDigest, createNodeId },
themeOptions
) => {
const { createNode } = actions
const options = withDefaults(themeOptions)
const config = require(options.configPath)
const configNode = {
...options,
config,
}
createNode({
...configNode,
id: createNodeId(`gatsby-theme-i18n-config`),
parent: null,
children: [],
internal: {
type: `ThemeI18n`,
contentDigest: createContentDigest(configNode),
content: JSON.stringify(configNode),
description: `Options for gatsby-theme-i18n`,
},
})
}
exports.onCreateNode = ({ node, actions }, themeOptions) => {
const { createNodeField } = actions
const { defaultLang } = withDefaults(themeOptions)
if (node.internal.type === `Mdx`) {
const name = path.basename(node.fileAbsolutePath, `.mdx`)
const isDefault = name === `index`
const lang = isDefault ? defaultLang : name.split(`.`)[1]
createNodeField({ node, name: `locale`, value: lang })
createNodeField({ node, name: `isDefault`, value: isDefault })
}
}
exports.onCreatePage = ({ page, actions }, themeOptions) => {
const { createPage, deletePage } = actions
const { configPath, defaultLang, locales } = withDefaults(themeOptions)
// Check if originalPath was already set and bail early as otherwise an infinite loop could occur
// as other plugins like gatsby-plugin-mdx could modify this
if (page.context.originalPath) {
return
}
const originalPath = page.path
deletePage(page)
const configLocales = require(configPath)
const languages = getLanguages(defaultLang, configLocales, locales)
languages.forEach((locale) => {
const newPage = {
...page,
path: localizedPath(defaultLang, locale.code, page.path),
matchPath: page.matchPath
? localizedPath(defaultLang, locale.code, page.matchPath)
: page.matchPath,
context: {
...page.context,
locale: locale.code,
hrefLang: locale.hrefLang,
originalPath,
dateFormat: locale.dateFormat,
},
}
// Check if the page is a localized 404
if (newPage.path.match(/^\/[a-z]{2}\/404\/$/)) {
// Match all paths starting with this code (apart from other valid paths)
newPage.matchPath = `/${locale.code}/*`
}
createPage(newPage)
})
}