-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
357 lines (330 loc) · 9.43 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
const { type, version } = require('os')
const path = require(`path`)
// 为数据文件添加'locale'节点
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
// 默认语言设置为英语
const defaultLang = 'en'
if (
node.internal.type === 'logo' ||
node.internal.type === 'sourceInfo' ||
node.internal.type === 'logoPack' ||
node.internal.type === 'MarkdownRemark'
) {
// 通过父节点获取文件名
const name = getNode(node.parent).name
// 判断是否为默认语言
const isDefault = name === name.split(`.`)[0]
// 获取语言类型,如果是默认语言则为'en',否则从文件名中获取
const lang = isDefault ? defaultLang : name.split(`.`)[1]
// 创建节点字段并设置'locale'字段值
createNodeField({ node, name: `locale`, value: lang })
createNodeField({ node, name: `isDefault`, value: isDefault })
// 为 logo 添加 `logoID` 字段
if (node.internal.type === 'logo') {
// 获取 `logoID`
const logoID = node.logoID
// 创建唯一ID字段
const uniqueID = `${lang}-${logoID}`
// 创建节点字段并设置'uniqueID'字段值
createNodeField({ node, name: `uniqueID`, value: uniqueID })
}
}
}
// 为字段添加自定义解析器
exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
logo: {
// LOGO的其他样式类型
otherStyle: {
type: ['logo'],
resolve: async (source, args, context, info) => {
const { entries } = await context.nodeModel.findAll({
query: {
filter: {
id: { ne: source.id }, // 排除当前 LOGO
fields: {
locale: { eq: source.fields.locale } // 匹配页面语言
},
sourceID: { eq: source.sourceID }, // 匹配主体 ID
version: { eq: source.version } // LOGO 版本相同
}
},
type: 'logo'
})
return entries
}
},
// LOGO 时间线(历史)
logoTimeline: {
type: ['logo'],
resolve: async (source, args, context, info) => {
const { entries } = await context.nodeModel.findAll({
query: {
filter: {
fields: {
locale: { eq: source.fields.locale } // 匹配页面语言
},
sourceID: { eq: source.sourceID }, // 匹配主体ID
style: { eq: 'color' } // 仅匹配'color'样式
}
},
type: 'logo'
})
return entries
}
},
// LOGO 主体详细信息
detailInfo: {
type: ['sourceInfo'],
resolve: async (source, args, context, info) => {
const { entries } = await context.nodeModel.findAll({
query: {
filter: {
sourceID: { eq: source.sourceID }, // 匹配主体ID
fields: {
locale: { eq: source.fields.locale } // 匹配页面语言
}
}
},
type: 'sourceInfo'
})
return entries
}
},
// LOGO 贡献者
contributorInfo: {
type: ['contributor'],
resolve: async (source, args, context, info) => {
const { entries } = await context.nodeModel.findAll({
query: {
filter: {
ctrbID: { eq: source.ctrbID } // 匹配贡献都ID
}
},
type: 'contributor'
})
return entries
}
}
},
logoPack: {
// 徽标集合信息
packInfo: {
type: ['logo'],
resolve: async (source, args, context, info) => {
const { entries } = await context.nodeModel.findAll({
query: {
filter: {
fields: {
locale: { eq: source.fields.locale } // 匹配页面语言
},
sourceID: { eq: source.packSource }, // 匹配主体ID
style: { eq: 'color' }, // 仅匹配 color 样式
version: { eq: source.logoVersion } // 匹配 logo 版本
}
},
type: 'logo'
})
return entries
}
},
// 集合包含的项目信息
itemsInfo: {
type: ['logo'],
resolve: async (source, args, context, info) => {
const { entries } = await context.nodeModel.findAll({
query: {
filter: {
fields: {
locale: { eq: source.fields.locale } // 匹配页面语言
},
style: { eq: 'color' } // 仅匹配 color 样式
}
},
type: `logo`
})
const items = entries.filter(item => {
// 根据主体 ID 与徽标 version 匹配徽标
return source.items.some(i => i.id === item.sourceID && i.version === item.version)
})
return items
}
}
},
// 为徽标主体信息添加 徽标数量 与 最新版本 字段
sourceInfo: {
// 添加主体包含 徽标数量 字段
logoCount: {
type: `Int`,
resolve: async (source, args, context, info) => {
const { entries } = await context.nodeModel.findAll({
query: {
filter: {
sourceID: { eq: source.sourceID },
fields: {
locale: { eq: source.fields.locale }
}
}
},
type: `logo`
})
return Array.from(entries).length
}
},
// 添加主体最新徽标版本字段
latestVersion: {
type: `Float`,
resolve: async (source, args, context, info) => {
const { entries } = await context.nodeModel.findAll({
query: {
filter: {
sourceID: { eq: source.sourceID },
fields: {
locale: { eq: source.fields.locale }
}
}
},
type: `logo`
})
const versions = Array.from(entries).map(({ version }) => version)
return Math.max(...versions)
}
},
// 添加主体最新版本徽标
latestLogo: {
type: ['logo'],
resolve: async (source, args, context, info) => {
const { entries } = await context.nodeModel.findAll({
query: {
filter: {
sourceID: { eq: source.sourceID },
fields: {
locale: { eq: source.fields.locale }
},
style: { eq: 'color' },
isOutdated: { eq: false }
}
},
type: `logo`
})
return entries
}
},
// 添加主体国旗标志
nationalFlag: {
type: ['flag'],
resolve: async (source, args, context) => {
const { entries } = await context.nodeModel.findAll({
query: {
filter: {
nation: { eq: source.nation }
}
},
type: 'flag'
})
return entries
}
}
}
}
createResolvers(resolvers)
}
// 创建 LOGO && PACK 详情页面
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
// 创建 LOGO 详情页
const logoDetail = await graphql(`
query {
allLogo(filter: { fields: { locale: { eq: "en" } } }, sort: { logoID: ASC }) {
edges {
node {
slug
}
next {
slug
}
previous {
slug
}
}
}
}
`)
// 创建 LOGO PACKS 页面
const logoPacks = await graphql(`
query {
allLogoPack(sort: { packID: ASC }, filter: { fields: { locale: { eq: "en" } } }) {
edges {
node {
slug
}
next {
slug
}
previous {
slug
}
}
}
}
`)
// 创建 Documents 页面
const support = await graphql(`
query {
allMarkdownRemark {
nodes {
frontmatter {
slug
}
}
}
}
`)
if (logoDetail.errors) {
reporter.panicOnBuild(logoDetail.errors)
return
}
if (logoPacks.errors) {
reporter.panicOnBuild(logoPacks.errors)
return
}
if (support.errors) {
reporter.panicOnBuild(support.errors)
return
}
// 创建 LOGO 详情页
logoDetail.data.allLogo.edges.forEach(({ node, next, previous }) => {
createPage({
path: node.slug,
component: path.resolve(`./src/templates/logo-detail.js`),
context: {
slug: node.slug,
next,
previous
}
})
})
// 创建 LOGO PACKS 页面
logoPacks.data.allLogoPack.edges.forEach(({ node, next, previous }) => {
createPage({
path: node.slug,
component: path.resolve(`./src/templates/pack-detail.js`),
context: {
slug: node.slug,
next,
previous
}
})
})
// 创建 Documents 页面
support.data.allMarkdownRemark.nodes.forEach(node => {
createPage({
path: `/support/${node.frontmatter.slug}`,
component: path.resolve(`./src/templates/support.js`),
context: {
slug: node.frontmatter.slug
}
})
})
}