-
Notifications
You must be signed in to change notification settings - Fork 14
/
gatsby-node.ts
179 lines (160 loc) · 4.21 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
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const spawn = require('cross-spawn')
import languages from './languages.js'
module.exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === 'MarkdownRemark') {
const path = createFilePath({ node, getNode, basePath: `content` })
const pathComponents = path.split('/').slice(1, -1)
node.collection = getNode(node.parent).sourceInstanceName
const category = node.frontmatter.category
const slug = pathComponents[0]
let locale = 'en'
// extract locale from file extension
if (pathComponents[pathComponents.length - 1]) {
locale = pathComponents[pathComponents.length - 1]
.split('.')
.slice(-1)
.pop()
}
if (pathComponents.length > 0 && parseInt(pathComponents[0])) {
createNodeField({
node,
name: `order`,
value: parseInt(pathComponents[0]),
})
}
createNodeField({
node,
name: `locale`,
value: locale,
})
createNodeField({
node,
name: `slug`,
value: slug,
})
createNodeField({
node,
name: `category`,
value: category,
})
}
}
module.exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const response = await graphql(`
query AllMarkDownRemark {
allMarkdownRemark {
edges {
node {
collection
fields {
slug
locale
}
}
}
}
}
`)
response.data.allMarkdownRemark.edges.forEach((edge) => {
const slug = edge.node.fields.slug
const template = edge.node.collection
if (['jobs'].includes(template)) {
createPage({
component: path.resolve(`./src/templates/${template}.js`),
path: `careers/${edge.node.fields.slug}`,
context: {
slug: edge.node.fields.slug,
},
})
}
})
const result = await graphql(`
query {
allMarkdownRemark(
filter: { collection: { in: ["articles", "talks"] } }
sort: { fields: [frontmatter___date], order: DESC }
) {
group(field: fields___locale) {
fieldValue
totalCount
}
}
}
`)
const postsPerPage = 10
languages.forEach((language) => {
const group = result.data.allMarkdownRemark.group.find(
(g) => g.fieldValue === language.code
)
const totalCount = group ? group.totalCount : 0
const numPages = Math.ceil(totalCount / postsPerPage)
Array.from({ length: numPages }).forEach((_, i) => {
if (language.code === 'en') {
createPage({
path: i === 0 ? `/articles` : `/articles/${i + 1}`,
component: path.resolve('./src/templates/articles.tsx'),
context: {
language: 'en',
limit: postsPerPage,
skip: i * postsPerPage,
numPages,
currentPage: i + 1,
},
})
} else {
createPage({
path:
i === 0
? `/${language.code}/articles`
: `/${language.code}/articles/${i + 1}`,
component: path.resolve('./src/templates/articles.tsx'),
context: {
language: language.code,
limit: postsPerPage,
skip: i * postsPerPage,
numPages,
currentPage: i + 1,
},
})
}
})
})
}
exports.onCreateWebpackConfig = ({ actions, stage }) => {
if (stage === 'build-javascript') {
actions.setWebpackConfig({
devtool: false,
})
}
actions.setWebpackConfig({
module: {
rules: [
{
test: /\.asc$/i,
use: 'raw-loader',
},
],
},
})
}
exports.onPreInit = () => {
const result = spawn.sync('yarn', ['i18next', 'src/**/*.{js,tsx}'], {
stdio: 'inherit',
})
}
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type MarkdownRemarkFrontmatter {
featuredImage: File @fileByRelativePath
start_date: Date @dateformat
contract_type: String
slug: String
}
`
createTypes(typeDefs)
}