-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
171 lines (162 loc) · 4.27 KB
/
gatsby-node.js
File metadata and controls
171 lines (162 loc) · 4.27 KB
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
const axios = require("axios")
const path = require("path")
const i18nConfig = require("./i18n/config.json")
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
const config = require("./config")
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
createTypes(`
type SubSite implements Node @dontInfer {
id: ID!
title: String!
description: String!
icon: File!
locale: String!
url: String!
index: Int!
}
`)
}
exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
SubSite: {
icon: {
resolve: (source, _, context, __) => {
if (source.icon___NODE) {
return context.nodeModel.getNodeById({
id: source.icon___NODE,
})
}
},
},
},
}
createResolvers(resolvers)
}
exports.sourceNodes = async ({
actions,
createContentDigest,
createNodeId,
store,
cache,
}) => {
const { createNode } = actions
const sites = config.sites
const apis = []
// const pinResults = i18nConfig.map(i18n => {
// let name = "How to spend every day"
// let description =
// "Community experiment based on value classification by Buzzing"
// if (i18n.code.startsWith("zh")) {
// name = "如何度过每一天"
// description = "主动思考我该如何度过每一天,由Buzzing运营"
// }
// let site = "https://d.buzzing.cc"
// return {
// name,
// lang: i18n.code,
// start_url: "/",
// description,
// site,
// icons: [
// {
// src: "pictrs/image/PuxdcKX8Pa.png",
// },
// ],
// }
// });
const pinResults = [];
sites.forEach(site => {
i18nConfig.forEach(i18n => {
let apiPath = "/manifest.webmanifest"
if (i18n.code !== "zh") {
apiPath = `/manifest_${i18n.code}.webmanifest`
}
apis.push({
site: site,
api: `${site}${apiPath}`,
})
})
})
const promises = apis.map(api => {
console.log("api.api", api.api)
return axios(api.api)
.then(result => {
let lang = result.data.lang
if (lang === "zh-Hans") {
lang = "zh"
}
return {
...result.data,
lang,
ok: true,
site: api.site,
}
})
.catch(e => {
console.error(`api: ${api.api} error`)
// console.error(e)
// throw e
return {
ok: false,
site: api.site,
}
})
})
let apiResults = await Promise.all(promises)
let results = pinResults.concat(apiResults)
for (let i = 0; i < results.length; i++) {
const data = results[i]
if (!data || data.ok == false || !data.name) {
// console.log("data", data)
// console.log("data.ok", data.ok)
continue
}
const site = data.site
if (site === "https://news.buzzing.cc" && data.lang === "zh") {
data.name = "国外新闻头条"
}
if (site === "https://hackernews.buzzing.cc" && data.lang === "zh") {
data.description =
"更多的HackerNews帖子,所有出现在HackerNews首页的帖子都会被收录,适合HN热门看不够的同学,比如我"
}
// console.log("data.description", data.site, data.lang, data.description)
const node = {
index: i,
id: `${data.site}/${data.lang}`,
title: data.name,
description: data.description,
locale: data.lang,
url: `${site}${data.start_url}`,
}
const iconUrl = `${site}/${data.icons[data.icons.length - 1].src}`
// create a file node for image URLs
const remoteFileNode = await createRemoteFileNode({
url: iconUrl,
parentNodeId: node.id,
createNode,
createNodeId,
cache,
store,
}).catch(e => {
console.error(`iconUrl: ${site} error`)
console.error(e)
throw e
})
// if the file was created, attach the new node to the parent node
if (remoteFileNode) {
node.icon___NODE = remoteFileNode.id
}
createNode({
...node,
parent: null,
children: [],
internal: {
type: `SubSite`,
contentDigest: createContentDigest(node),
content: JSON.stringify(node),
description: `sub site`,
},
})
}
}