-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.ts
161 lines (150 loc) · 3.86 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
import type { GatsbyNode } from "gatsby"
import { createFilePath } from "gatsby-source-filesystem"
import path from "path"
export const onCreateWebpackConfig: GatsbyNode["onCreateWebpackConfig"] = ({
actions,
}) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, "src"), "node_modules"],
alias: { "@": path.resolve(__dirname, "src") },
},
})
}
export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] =
async ({ actions: { createTypes } }) => {
createTypes(`
type MarkdownRemark implements Node {
slug: File @link(from: "fields.slug")
}
`)
}
export const onCreateNode: GatsbyNode["onCreateNode"] = async ({
node,
getNode,
actions: { createNodeField },
}) => {
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, trailingSlash: false })
createNodeField({
node,
name: `slug`,
value: `/articles${slug.toLowerCase()}`,
})
}
}
export const createPages: GatsbyNode["createPages"] = async ({
graphql,
actions,
reporter,
}) => {
const { createPage } = actions
const getArticleGroupQuery = (groupField: string) => `
query allMarkdownRemarkGroupBy${groupField} {
allMarkdownRemark(
filter: {frontmatter: {draft: {ne: true}}}
sort: {frontmatter: {date: DESC}}
) {
group(field: {frontmatter: {${groupField.toLowerCase()}: SELECT}}) {
totalCount
field
fieldValue
edges {
node {
id
fields {
slug
}
}
}
}
}
}
`
const { data: categoryNodes, errors: categoryErrors } =
await graphql<Queries.Query>(getArticleGroupQuery("Categories"))
const { data: tagNodes, errors: tagErrors } = await graphql<Queries.Query>(
getArticleGroupQuery("Tags"),
)
const { data: seriesNodes, errors: seriesErrors } =
await graphql<Queries.Query>(getArticleGroupQuery("Series"))
if (categoryErrors || tagErrors || seriesErrors) {
console.log(categoryErrors || tagErrors || seriesErrors)
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
createPage({
path: `/articles`,
component: path.resolve(`src/templates/articles.tsx`),
context: {
_pathname: `/articles`,
groupBy: {
categories: categoryNodes?.allMarkdownRemark.group,
tags: tagNodes?.allMarkdownRemark.group,
series: seriesNodes?.allMarkdownRemark.group,
},
},
})
const { data: articleList, errors } = await graphql(`
query ArticleList {
allMarkdownRemark(
filter: { frontmatter: { draft: { ne: true } } }
sort: { frontmatter: { date: DESC } }
) {
edges {
node {
id
fields {
slug
}
}
next {
id
fields {
slug
}
frontmatter {
title
}
}
previous {
id
fields {
slug
}
frontmatter {
title
}
}
}
}
}
`)
if (errors) {
reporter.panicOnBuild("Error while running GraphQL query.")
return
}
articleList.allMarkdownRemark.edges.forEach(({ node, next, previous }) => {
createPage({
path: `${node.fields?.slug}`,
component: path.resolve(`src/templates/article.tsx`),
context: {
_pathname: `${node.fields?.slug}`,
articleId: node.id,
next,
previous,
},
})
})
}
export const onCreatePage: GatsbyNode["onCreatePage"] = ({ page, actions }) => {
const { createPage, deletePage } = actions
deletePage(page)
createPage({
...page,
context: {
...page.context,
_pathname: page.path,
},
})
}