-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.ts
178 lines (155 loc) · 4.09 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
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-var-requires, import/no-extraneous-dependencies */
/* eslint-disable import/no-extraneous-dependencies */
import { each, first, compact, split, chunk, filter } from 'lodash'
import path from 'path'
import { createFilePath } from 'gatsby-source-filesystem'
import wordCount from 'word-count'
import { GatsbyNode } from 'gatsby'
const SRC = path.resolve(__dirname, './src')
const TEMPLATES = {
blog: path.resolve(SRC, 'templates/blog-post.tsx'),
about: path.resolve(SRC, 'templates/page.tsx'),
}
const ITEM_PER_PAGE = 10
export const onCreateNode: GatsbyNode['onCreateNode'] = ({
node,
getNode,
actions,
}) => {
const { createNodeField } = actions
if (node.internal.type === 'MarkdownRemark') {
const p = createFilePath({ node, getNode, basePath: 'content' })
createNodeField({
node,
value: p,
name: 'slug',
})
createNodeField({
node,
value: first(compact(split(p, '/'))),
name: 'type',
})
const words = wordCount(
(node as unknown as Queries.MarkdownRemark).rawMarkdownBody!,
)
const WORD_PER_MINUTE = 160
createNodeField({
node,
value: {
words,
minutes: Math.round(words / WORD_PER_MINUTE),
},
name: 'timeToRead',
})
}
}
export const createPages: GatsbyNode['createPages'] = async ({
graphql,
actions,
}) => {
const { createPage, createRedirect } = actions
const result = await graphql<Queries.CreatePagesQuery>(`
query CreatePages {
allMarkdownRemark(sort: [{ frontmatter: { publish_date: DESC } }]) {
edges {
node {
id
fields {
slug
type
timeToRead {
words
minutes
}
}
frontmatter {
title
publish_date
}
}
}
}
}
`)
if (result.errors) {
console.error(result.errors)
return Promise.reject(result.errors)
}
const posts = result?.data?.allMarkdownRemark.edges
if (!posts) {
return Promise.resolve()
}
each(
chunk(
filter(posts, (p) => p?.node?.fields?.type === 'blog'),
ITEM_PER_PAGE,
),
(slice, index, slices) => {
// pagination starts from 1
const page = index + 1
createPage({
path: index === 0 ? '/blog' : `/blog/page/${page}`,
component: path.resolve(SRC, 'templates/paginated-blog-index.tsx'),
context: {
page,
total: posts?.length,
pages: slices.length,
prev: page - 1,
next: page === slices.length ? 0 : page + 1,
limit: ITEM_PER_PAGE,
items: slice,
},
})
},
)
createRedirect({ fromPath: '/blog/page/1', toPath: '/blog' })
each(posts, (post, index) => {
const previous = index === posts.length - 1 ? null : posts[index + 1].node
createPage({
path: post?.node?.fields?.slug!,
component:
TEMPLATES[post?.node?.fields?.type as keyof typeof TEMPLATES] ||
path.resolve(SRC, 'templates/page.tsx'),
context: {
previous,
slug: post?.node?.fields?.slug,
},
})
})
return Promise.resolve()
}
export const onCreateWebpackConfig: GatsbyNode['onCreateWebpackConfig'] = ({
actions,
stage,
plugins,
}) => {
if (stage === 'build-javascript' || stage === 'develop') {
actions.setWebpackConfig({
plugins: [plugins.provide({ process: 'process/browser' })],
})
}
}
export const onCreateBabelConfig: GatsbyNode['onCreateBabelConfig'] = ({
actions,
}) => {
actions.setBabelPreset({
name: 'babel-preset-gatsby',
options: {
reactRuntime: 'automatic',
},
})
}
export const createSchemaCustomization: GatsbyNode['createSchemaCustomization'] =
({ actions }) => {
actions.createTypes(`
type Site {
siteMetadata: SiteMetadata!
}
type SiteMetadata {
title: String!
siteUrl: String!
}
`)
}