-
Notifications
You must be signed in to change notification settings - Fork 19
/
gatsby-node.js
428 lines (399 loc) · 12 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
const _ = require(`lodash`)
const Promise = require(`bluebird`)
const path = require(`path`)
const req = require('require-yml')
const _docSidebarItems = req(`./src/data/sidebars/doc-links.yaml`)
const gitalkOptions = require('./gitalk-options')
const MD5 = require('crypto-js/md5')
const {GitalkPluginHelper} = require('gatsby-plugin-gitalk')
const mergePath = (basePath = '/', path = '')=>{
let result = "/" + basePath + "/" + path
result = result.replace(/\/+/g, '/')
return result
}
const getRandomInt = function (min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const createHash = link => {
let index = -1
if (link) index = link.indexOf(`#`)
return index >= 0 ? link.substr(index + 1) : false
}
// re generate uid,
const generateUid = (uid, uidObj, times=0)=>{
let tempUid = uid
if (times > 0 ) {
tempuid = uid + `_` + times
}
if (uidObj[tempUid]) {
return checkHasUid(uid, uidObj, times + 1)
} else {
return tempUid
}
}
const getPdfPath = (zipPath)=>{
const pos = zipPath.lastIndexOf('.zip')
const pdfPath = zipPath.substring(0, pos) + '.pdf'
return pdfPath
}
const extenditemList = itemList => {
if (!itemList) {
return []
}
const uidObj = {}
itemList.forEach(section => {
if (section && section.link){
const hash = createHash(section.link)
if (hash) {
section.hash = hash
}
}
section.parentUid = ''
const uid = generateUid(section.title, uidObj, 0)
section.uid = uid
if (section.items) extendItem(section.items, uid, uidObj)
})
return itemList
}
const extendItem = (items, parentUid, uidObj) => {
items.forEach(item => {
item.hash = createHash(item.link)
item.parentUid = parentUid
const uid = generateUid(parentUid + '___' + item.title, uidObj, 0)
item.uid = uid
uidObj[uid] = true
if (item.items) extendItem(item.items, uid, uidObj)
})
}
const docSidebarItems = extenditemList(_docSidebarItems).map(item => {
return { ...item, key: `docs` }
})
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage, createRedirect } = actions
const pageSize = 8;
const docPostTemplate = path.resolve(`src/templates/template-docs-markdown.js`)
const logPostTemplate = path.resolve(`src/templates/template-logs-markdown.js`)
const blogPostTemplate = path.resolve(`src/templates/template-blog-post.js`)
const paginatedPostsTemplate = path.resolve(`src/templates/template-blog-list.js`)
const tagTemplate = path.resolve(`src/templates/tags.js`)
const result = await graphql(
`
{
allStoryWriterMarkdown(
sort: { fields: [updateDate], order: DESC }, limit: 1000
) {
edges {
node {
id
title
toc
docType
docId
slug
zipPath
tags
createDate(formatString: "x")
updateDate
excerpt
}
}
}
}
`
)
if (result.errors) {
console.log(result.errors)
throw result.error
}
// Create blog posts pages.
if (!result.data) {
throw "no story writer markdown"
return
}
const slugMap = {}
const posts = result.data.allStoryWriterMarkdown.edges
const blogPosts = _.filter(result.data.allStoryWriterMarkdown.edges, edge=>{
const docType = _.get(edge, `node.docType`)
if (docType === `blogs`) {
return edge
}
return undefined
})
const gitalkCreateIssueToken = process.env.GITALK_CREATE_ISSUE_TOKEN
if (gitalkCreateIssueToken) {
// 仅更新最近的 10 篇文章 issue 是否需要创建评论,
// 防止 github api 请求限制超额问题
for(let index = 0; index < posts.length && index < 10; index++) {
//如果用户使用了像 github action, 并提供了 创建 issue 的 Token, 就直接先创建 issue
const post = posts[index]
const issueOptions = Object.assign({}, gitalkOptions, {
id: MD5(post.node.slug || post.node.id),
title: post.node.title,
description: post.node.excerpt,
url: 'https://suziwen.github.io' + mergePath('/', post.node.slug),
}, {
personalToken: gitalkCreateIssueToken
})
await GitalkPluginHelper.createIssue(issueOptions)
reporter.info(`create issue: ${post.node.title}`)
}
}
posts.forEach((post, index) => {
const slugKey = '/' + post.node.slug.replace(/^\/*|\/*$/g, '')
const idKey = '/' + post.node.docId.replace(/^\/*|\/*$/g, '')
if (post.node.docType === 'logs') {
slugMap[slugKey] = '/logs/' + post.node.slug + '/'
slugMap[idKey] = slugMap[slugKey]
}
if (post.node.docType === 'docs') {
slugMap[slugKey] = '/docs/' + post.node.slug + '/'
slugMap[idKey] = slugMap[slugKey]
}
if (post.node.docType === 'blogs') {
slugMap[slugKey] = '/blog/' + post.node.slug + '/'
slugMap[idKey] = slugMap[slugKey]
}
})
// Tag pages:
let tags = [];
// Iterate through each post, putting all found tags into `tags`
posts.forEach(edge => {
if (_.get(edge, "node.tags")) {
tags = tags.concat(edge.node.tags);
}
});
// Eliminate duplicate tags
tags = _.uniq(tags);
// Make tag pages
tags.forEach(tag => {
createPage({
path: `/tags/${_.kebabCase(tag)}/`,
component: tagTemplate,
context: {
tag,
},
});
});
const logNodes = []
const logSidebarItems = []
const visItems = []
posts.forEach(post => {
if (post.node.docType === 'logs') {
logNodes.push(post.node)
visItems.push({
id: post.node.id,
start: parseInt(post.node.createDate),
title: post.node.title,
link: encodeURI(`/logs/${post.node.slug}/`),
})
logSidebarItems.push({
title: post.node.title,
link: encodeURI(`/logs/${post.node.slug}/`),
hash: false,
disableExpandAll: true,
key: 'logs'
})
}
if (post.node.docType === 'docs') {
createPage({
path: `/docs/` + post.node.slug + `/`,
component: docPostTemplate,
context: {
slug: post.node.slug,
zipPath: post.node.zipPath,
pdfPath: getPdfPath(post.node.zipPath),
sidebarItems: docSidebarItems
},
})
}
})
createRedirect({
toPath: `/blog/vip/price/flow`,
fromPath: `/priceflow.html`,
redirectInBrowser: true,
isPermanent: true
})
createRedirect({
toPath: `/price`,
fromPath: `/price.html`,
redirectInBrowser: true,
isPermanent: true
})
createRedirect({
toPath: `/feature`,
fromPath: `/feature.html`,
redirectInBrowser: true,
isPermanent: true
})
for (const key in slugMap) {
createRedirect({
toPath: slugMap[key],
fromPath: key,
redirectInBrowser: true,
})
createRedirect({
toPath: slugMap[key],
fromPath: key + '/',
redirectInBrowser: true,
})
}
logNodes.forEach((node, index)=>{
if (index === 0) {
createPage({
path: `/logs/`,
component: logPostTemplate,
context: {
id: node.id,
slug: node.slug,
zipPath: node.zipPath,
pdfPath: getPdfPath(node.zipPath),
visItems: visItems,
sidebarItems: logSidebarItems
},
})
}
createPage({
path: `/logs/` + node.slug + `/`,
component: logPostTemplate,
context: {
id: node.id,
slug: node.slug,
zipPath: node.zipPath,
pdfPath: getPdfPath(node.zipPath),
visItems: visItems,
sidebarItems: logSidebarItems
},
})
})
blogPosts.forEach((post, index) => {
let related = posts.filter((p) => {
if(p.node.slug === post.node.slug) {
return false;
}
var filteredTags = post.node.tags.filter((tag) => {
if(p.node.tags.indexOf(tag) !== -1) {
return true;
}
return false;
});
if(filteredTags && filteredTags.length > 0) {
return true;
}
return false;
});
related = _.shuffle(related).slice(0,6);
const wrapperNode = (node)=>{
if (node) {
return {
title: node.title,
docType: node.docType,
slug: node.slug
}
}
return null;
}
related = related.map((_node)=>{
_node = _node && _node.node;
return wrapperNode(_node);
});
let next = index === 0 ? null : blogPosts[index - 1].node
next = wrapperNode(next);
let prev =
index === blogPosts.length - 1 ? null : blogPosts[index + 1].node
prev = wrapperNode(prev);
let blogSidebarItems = null
let enableScrollSync = null
if (post.node.toc) {
const _blogSidebarItems = JSON.parse(post.node.toc)
if (_blogSidebarItems.length > 0) {
const fixSlug = (items)=>{
items.forEach((item)=>{
item.link = encodeURI(`/blog/${item.link}`)
if (item.items) {
fixSlug(item.items)
}
})
}
fixSlug(_blogSidebarItems)
enableScrollSync = true
blogSidebarItems = extenditemList(_blogSidebarItems).map(item => {
return { ...item, key: `blog` }
})
}
}
//const _blogSidebarItems = JSON.parse(post.node.toc)
// const blogSidebarItems = extenditemList(_blogSidebarItems).map(item => {
// return { ...item, key: `blogs` }
// })
createPage({
path: `/blog/` + post.node.slug + `/`,
component: blogPostTemplate,
context: {
slug: post.node.slug,
zipPath: post.node.zipPath,
pdfPath: getPdfPath(post.node.zipPath),
sidebarItems: blogSidebarItems,
isSidebarDisabled: true,
enableScrollSync,
prev,
next,
related
},
})
})
// pagination blogPost
const chunkedPosts = _.chunk(blogPosts, pageSize);
chunkedPosts.forEach((chunk, index) => {
let path = `/blog/`
if (index > 0) {
path = `/blog/page/${index+1}/`
}
createPage({
path: path,
component: paginatedPostsTemplate,
context:
{
limit: pageSize,
skip: index * pageSize,
numPages: Math.ceil(blogPosts.length / pageSize),
currentPage: index + 1,
}
,
})
})
}
exports.onCreateWebpackConfig = ({ stage, actions }) => {
if (stage === `build-javascript`) {
actions.setWebpackConfig({
devtool: false,
})
}
}
exports.onCreatePage = ({page, actions})=> {
const { createPage } = actions
if(page.path.indexOf(`/docs/`) === 0) {
page.context.sidebarItems = docSidebarItems
createPage(page)
} else if (page.path.indexOf(`/logs/`) === 0) {
createPage(page)
} else {
createPage(page)
}
}
exports.onCreateNode = ({ node, actions, getNodesByType, getNodes }) => {
const { createNode, createNodeField } = actions
if (node.internal.type === 'StoryWriterMarkdown') {
node.haveCover = true
if (!node.cover) {
node.haveCover = false
const fileNodes = getNodesByType('File');
const coverNodes = fileNodes.filter((node)=>{
return node.sourceInstanceName == 'covers' && node.extension == 'png';
});
const coverNode = coverNodes[getRandomInt(0, coverNodes.length - 1)];
node.cover = path.relative(path.dirname(node.fileAbsolutePath), coverNode.absolutePath);
}
}
}