-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.ts
151 lines (135 loc) · 3.64 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
import type { GatsbyNode } from "gatsby"
import { createFilePath } from "gatsby-source-filesystem"
import path from "path"
import { ArtCollectionPageContext, BlogPostContext } from "./src/common/types"
export const onCreateNode: GatsbyNode["onCreateNode"] = ({
node,
getNode,
actions,
}) => {
const { createNodeField } = actions
if (node.internal.type === "Mdx") {
const relativeFilePath = createFilePath({
node,
getNode,
basePath: "src/content",
trailingSlash: false,
})
createNodeField({
node,
name: "path",
value: `/blog${relativeFilePath}`,
})
createNodeField({
node,
name: "legacyPath",
value: relativeFilePath,
})
}
}
export const createPages: GatsbyNode["createPages"] = async ({
graphql,
actions,
}) => {
const createBlogPosts = async () => {
const { data } = await graphql<Queries.AllBlogPostsQuery>(`
query AllBlogPosts {
allMdx(sort: { frontmatter: { date: ASC } }) {
edges {
node {
id
internal {
contentFilePath
}
fields {
legacyPath
path
}
frontmatter {
legacy
}
}
}
}
}
`)
const template = path.resolve(`./src/templates/blogPost.tsx`)
const pages = data?.allMdx.edges.flatMap(e => e.node)
pages?.forEach((node, idx) => {
const previous = idx !== 0 ? pages[idx - 1] : null
const next = idx < pages.length - 1 ? pages[idx + 1] : null
const createPage = (path: string) =>
actions.createPage<BlogPostContext>({
path: path,
component: `${template}?__contentFilePath=${node.internal.contentFilePath}`,
context: {
id: node.id,
previousId: previous?.id ?? "",
nextId: next?.id ?? "",
},
})
const { path, legacyPath } = node?.fields!
if (path != null && legacyPath != null) {
createPage(path)
if (node?.frontmatter?.legacy) {
createPage(legacyPath)
}
}
})
}
const createGalleryPages = async () => {
const { data } = await graphql<Queries.GalleryCollectionPagesQuery>(`
query GalleryCollectionPages {
allFile(filter: { base: { eq: "gallery-index.json" } }) {
nodes {
relativeDirectory
childrenJson {
title
description
}
}
}
}
`)
const template = path.resolve(`./src/templates/artCollection.tsx`)
data?.allFile.nodes
.filter(n => n.childrenJson != null)
.forEach(n => {
actions.createPage<ArtCollectionPageContext>({
path: `art/${n.relativeDirectory}`,
component: `${template}`,
context: {
title: n.childrenJson![0]!.title!,
description: n.childrenJson![0]!.description!,
directory: n.relativeDirectory,
},
})
})
}
await createBlogPosts()
await createGalleryPages()
}
export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] =
({ actions }) => {
const { createTypes } = actions
createTypes(`
type SiteSiteMetadata {
author: String!
currentYear: Int!
}
type Mdx implements Node {
frontmatter: Frontmatter!
fields: Fields!
}
type Frontmatter {
title: String!
date: Date! @dateformat
preview: String!
tags: [String!]!
}
type Fields {
path: String!
legacyPath: String!
}
`)
}