-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.ts
149 lines (133 loc) · 3.56 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*
* Because we used ts-node in gatsby-config.js, this file will automatically be
* imported by Gatsby instead of gatsby-node.js.
*/
import * as path from 'path'
import { kebabCase } from 'lodash'
import { createFilePath } from 'gatsby-source-filesystem'
// Use the type definitions that are included with Gatsby.
import { GatsbyNode } from 'gatsby'
export const onCreateNode: GatsbyNode['onCreateNode'] = ({ node, getNode, actions }) => {
const { createNodeField, createRedirect } = actions
if (node.internal.type === 'MarkdownRemark' || node.internal.type === 'Mdx') {
const slug = createFilePath({ node, getNode })
createNodeField({
name: 'slug',
node,
value: slug,
})
}
// For netlify
createRedirect({
fromPath: '/*', // your matchPath here
toPath: '/404', // the path to your 404 page here
statusCode: 404,
})
}
const groupCountBy = (field, edges) => {
const groupCounts = edges.reduce((acc, { node }) => {
const groups = node.frontmatter[field] || []
groups.forEach((group) => {
acc[group] = (acc[group] || 0) + 1
})
return acc
}, {})
return Object.entries<number>(groupCounts)
}
const createPaginatedPages = ({
createPage,
component,
total,
prefix = '',
limit = 10,
context = {},
}) => {
const pageTotal = Math.ceil(total / limit)
for (let page = 1; page <= pageTotal; page++) {
const path = page > 1 ? `${prefix}/${page}` : `${prefix}`
const skip = (page - 1) * limit
createPage({
path,
component,
context: {
...context,
total,
limit,
page,
pageTotal,
prefix,
skip,
},
})
}
}
const createBlogPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions
const { data, errors } = await graphql(`
query GatsbyCreatePage {
allMdx(
filter: { frontmatter: { draft: { ne: true } }, fields: { slug: { glob: "/blog/**" } } }
sort: { order: DESC, fields: [frontmatter___date] }
) {
edges {
node {
id
frontmatter {
tags
}
fields {
slug
}
}
}
}
}
`)
if (errors) {
reporter.panicOnBuild('Error fetching data', errors)
return
}
const blogPosts = data.allMdx.edges
// Create single content pages:
blogPosts.forEach(({ node }) => {
const { id, fields, frontmatter } = node
const slug = frontmatter.path || fields.slug
createPage({
path: slug,
tags: frontmatter.tags,
component: path.resolve(`src/templates/BlogPostTemplate.tsx`),
// additional data can be passed via context
context: {
id,
slug,
},
})
})
// Create blog content list:
const totalBlogPosts = blogPosts.length
reporter.info(`Blog posts (${totalBlogPosts})`)
createPaginatedPages({
total: totalBlogPosts,
createPage,
component: path.resolve('src/templates/BlogListTemplate.tsx'),
limit: 10,
prefix: '/blog/',
})
groupCountBy('tags', blogPosts).forEach(([tag, total]) => {
createPaginatedPages({
total,
createPage,
component: path.resolve('src/templates/BlogListByTagTemplate.tsx'),
prefix: `/blog/tags/${kebabCase(tag)}/`,
context: { tag },
})
reporter.info(`Tag: ${tag} (${Math.ceil(total / 10)})`)
})
}
export const createPages: GatsbyNode['createPages'] = async (gatsby) => {
await createBlogPages(gatsby)
}