-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.ts
147 lines (137 loc) · 3.74 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
import * as React from 'react'
import path from 'node:path'
import { createFilePath } from 'gatsby-source-filesystem'
import type { GatsbyNode } from 'gatsby'
import type { Post } from 'types/posts'
export const onCreateWebpackConfig: GatsbyNode['onCreateWebpackConfig'] = ({
actions
}) => {
actions.setWebpackConfig({
resolve: {
alias: {
'@/': path.resolve(__dirname, 'src/'),
// '@/': require.resolve('./src/'),
'@/assets': path.resolve(__dirname, 'content/assets'),
// '@/assets': require.resolve('./content/assets'),
'@/components': path.resolve(__dirname, 'src/components'),
// '@/components': require.resolve('./src/components'),
'@/lib': path.resolve(__dirname, 'src/lib'),
// '@/lib': require.resolve('./src/lib'),
'@/images': path.resolve(__dirname, 'src/images'),
// '@/images': require.resolve('./src/images'),
'@/pages': path.resolve(__dirname, 'src/pages'),
// '@/pages': require.resolve('./src/pages'),
'@/projects': path.resolve(__dirname, 'content/projects'),
// '@/projects': require.resolve('./content/projects'),
'@/styles': path.resolve(__dirname, 'src/components/styles'),
// '@/styles': require.resolve('./src/components/styles'),
'@/templates': path.resolve(__dirname, 'src/templates')
// '@/templates': require.resolve('./src/templates')
// assets: require.resolve(__dirname, 'content/assets')
}
}
})
}
export const createPages: GatsbyNode['createPages'] = async ({
graphql,
actions
}) => {
const { createPage } = actions
const blogPost = path.resolve(`./src/templates/blog-post.tsx`)
return await graphql(`
query allMarkdownPosts {
allMarkdownRemark(sort: { frontmatter: { date: DESC } }, limit: 1000) {
edges {
node {
fields {
slug
}
frontmatter {
title
}
}
}
}
}
`)
// @ts-expect-error incompatible types
.then((result: PostQueryResult) => {
if (result.errors) {
throw result.errors
}
const { edges } = result?.data?.allMarkdownRemark
edges?.forEach((edge, index: number) => {
const previous =
index === edges.length - 1 ? null : edges[index + 1].node
const next = index === 0 ? null : edges[index - 1].node
const {
node: {
fields: { slug },
frontmatter: { title }
}
} = edge as Post
createPage({
path: slug,
component: blogPost,
context: {
slug,
previous,
next,
title
}
})
})
})
}
export const onCreateNode: GatsbyNode['onCreateNode'] = ({
node,
actions,
getNode
}) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({
node,
getNode,
basePath: './content/projects'
})
createNodeField({
name: `slug`,
node,
value
})
createNodeField({
name: `url`,
node,
value
})
}
}
export const createSchemaCustomization: GatsbyNode['createSchemaCustomization'] =
({ actions }) => {
actions.createTypes(`
type Site {
siteMetadata: SiteMetadata!
}
type SiteMetadata {
title: String!
}
`)
}
// type Fields {
// slug: String
// url: String
// }
// type MarkdownRemark implements Node @dontInfer {
// frontmatter: Frontmatter
// fields: Fields
// excerpt: String
// }
// type Frontmatter {
// date: Date @dateformat
// title: String!
// description: String
// url: String
// tags: [String]
// thumbnail: File @fileByRelativePath
// }