-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
177 lines (149 loc) · 4.58 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
const path = require(`path`)
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin')
const { createFilePath } = require('gatsby-source-filesystem')
const TerserPlugin = require('terser-webpack-plugin')
const config = require('./config')
exports.createPages = async ({ actions, graphql, reporter }) => {
const blogPostTemplate = path.resolve(`src/templates/Post.tsx`)
const result = await graphql(`
{
allMarkdownRemark(sort: { frontmatter: { date: DESC } }, limit: 1000) {
edges {
node {
fields {
slug
}
frontmatter {
title
tags
update(formatString: "YYYY-MM-DD")
date(formatString: "YYYY-MM-DD")
}
}
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
const getSeries = slug => {
const parts = slug.split('_')
if (parts.length < 3) {
const seriesNum = parts[parts.length - 1].replace('/', '')
return !isNaN(seriesNum) ? parseInt(seriesNum, 10) : 0
}
return 0
}
const { edges } = result.data.allMarkdownRemark
await Promise.all(
edges.map(async ({ node }) => {
const { fields, frontmatter } = node
const { slug } = fields
const { date, update } = frontmatter
let filteredEdges = []
const series = []
if (getSeries(slug)) {
filteredEdges = edges.filter(e => {
const fSlug = e.node.fields.slug
const splitedFSlug = fSlug.split('_')
if (splitedFSlug.length >= 3) return false
if (slug.split('_').length > 1 && slug.split('_')[0] === splitedFSlug[0]) {
return true
}
})
if (filteredEdges.length) {
for (const e of filteredEdges) {
const seriesNum = getSeries(e.node.fields.slug)
if (seriesNum) {
series.push({
slug: e.node.fields.slug,
title: e.node.frontmatter.title,
num: seriesNum,
})
}
}
series.sort((a, b) => a.num - b.num)
}
}
actions.createPage({
path: slug,
component: blogPostTemplate,
context: { slug, series, lastmod: update.includes('0001') ? date : update },
})
})
)
}
exports.onCreateNode = ({ node, actions, getNode }) => {
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages` })
const rewriteSlug = slug => {
if (slug.match(/\//g).length > 2) {
return `/${slug.split('/').slice(-2, -1)[0]}/`
}
return slug.replace(/\/(18|19|20|21)\d{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[0-1])-/, '/')
}
const rewriteNode = node => {
if (node.frontmatter.title.includes(`"`)) {
console.log('')
console.warn(`
It is not recommended to include " in the title.
- file: ${node.fileAbsolutePath}
- title: ${node.frontmatter.title}
`)
}
if (!node.frontmatter.keywords) {
node.frontmatter.keywords = [config.title, config.author]
}
if (!node.frontmatter.tags) {
node.frontmatter.tags = ['undefined']
} else if (typeof node.frontmatter.tags === 'string') {
node.frontmatter.tags = [node.frontmatter.tags]
}
if (node.frontmatter.date.includes('+')) {
node.frontmatter.date = new Date(node.frontmatter.date.split('+')[0])
} else {
node.frontmatter.date = new Date(node.frontmatter.date)
}
if (!node.frontmatter.update) {
node.frontmatter.update = '0001-01-01T00:00:00.000Z'
}
return node
}
const newSlug = rewriteSlug(slug)
const newNode = rewriteNode(node)
actions.createNodeField({
name: `slug`,
node: newNode,
value: newSlug,
})
}
}
exports.onCreateWebpackConfig = ({ stage, actions, getConfig }) => {
const config = getConfig()
if (config.mode === 'production') {
actions.setWebpackConfig({ devtool: false })
}
if (stage === 'build-javascript') {
config.optimization.minimizer = [
...config.optimization.minimizer,
new CssMinimizerPlugin(),
new TerserPlugin({
test: /\.(js|jsx|ts|tsx)$/,
parallel: true,
terserOptions: {
format: {
comments: false,
},
compress: {
warnings: false,
drop_console: true,
},
extractComments: false,
},
}),
]
actions.replaceWebpackConfig(config)
}
}