-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgatsby-node.js
More file actions
184 lines (163 loc) · 4.18 KB
/
gatsby-node.js
File metadata and controls
184 lines (163 loc) · 4.18 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
172
173
174
175
176
177
178
179
180
181
182
183
184
const path = require("path")
const _ = require("lodash")
exports.createPages = async ({ graphql, actions }) => {
const { createRedirect, createPage } = actions
const notFoundTemplate = path.resolve("./src/templates/404.tsx")
createPage({
path: "404.html",
component: notFoundTemplate
})
const pageTemplate = path.resolve("./src/templates/page.tsx")
const pageResult = await graphql(
`
{
allMdx(
filter: { frontmatter: {layout: {eq: "page"}}}
) {
edges {
node {
fields {
path
}
frontmatter {
title
layout
}
}
}
}
}
`
)
if (pageResult.errors) {
throw pageResult.errors
}
// Create pages.
const pages = pageResult.data.allMdx.edges
pages.forEach((page) => {
createPage({
path: page.node.fields.path,
component: pageTemplate,
context: {
},
})
})
const postTemplate = path.resolve("./src/templates/post.tsx")
const result = await graphql(
`
{
allMdx(
sort: { fields: [frontmatter___date], order: DESC }
filter: { frontmatter: {layout: {eq: "post"}}}
) {
edges {
node {
fields {
path
}
frontmatter {
title
date
layout
}
}
}
}
}
`
)
if (result.errors) {
throw result.errors
}
// Create blog posts pages.
const posts = result.data.allMdx.edges
posts.forEach((post, index) => {
const previous = index === posts.length - 1 ? null : posts[index + 1].node
const next = index === 0 ? null : posts[index - 1].node
createPage({
path: post.node.fields.path,
component: postTemplate,
context: {
previous,
next,
},
})
// Redirect for AMP pages from old site
createRedirect({
fromPath: `${post.node.fields.path}amp/`,
toPath: post.node.fields.path,
isPermanent: true
})
})
const chunkTemplate = path.resolve("./src/templates/chunk.tsx")
const postsPerPage = 5
const numPages = Math.ceil(posts.length / postsPerPage)
Array.from({ length: numPages }).forEach((_, i) => {
const currentPage = (i + 1)
createPage({
path: `/posts/${currentPage}`,
component: chunkTemplate,
context: {
limit: postsPerPage,
skip: i * postsPerPage,
numPages,
currentPage,
previousPage: currentPage > 1 ? currentPage - 1 : null,
nextPage: currentPage >= numPages ? null : currentPage + 1
},
})
})
const archivesTemplate = path.resolve("./src/templates/archives.tsx")
createPage({
path: "/blog/archives/",
component: archivesTemplate,
})
const categoriesTemplate = path.resolve("./src/templates/categories.tsx")
createPage({
path: "/blog/categories/",
component: categoriesTemplate,
})
const categoryTemplate = path.resolve("./src/templates/category.tsx")
const categoryResult = await graphql(`
{
categoriesGroup: allMdx {
group(field: frontmatter___categories) {
fieldValue
}
}
}
`)
if (categoryResult.errors) {
throw categoryResult.errors
}
const categories = categoryResult.data.categoriesGroup.group
categories.forEach(category => {
createPage({
path: `/blog/categories/${_.kebabCase(category.fieldValue)}/`,
component: categoryTemplate,
context: {
category: category.fieldValue
}
})
})
}
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions
if (node.internal.type === "Mdx") {
if (node.frontmatter.layout === "post") {
const url = node.fileAbsolutePath.replace(/.+\/(\d+)-(\d+)-(\d+)-([\S]+)\.mdx?$/, "/blog/$1/$2/$3/$4/")
createNodeField({
name: "path",
node,
value: url
})
}
if (node.frontmatter.layout === "page") {
createNodeField({
name: "path",
node,
value: node.fileAbsolutePath.replace(/.+\/([\w*-]+)\.mdx?$/, "/$1/")
})
}
}
}