-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.ts
166 lines (148 loc) · 4.34 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
162
163
164
165
166
import path from "path";
import type { GatsbyNode, Actions } from "gatsby";
import { createFilePath } from "gatsby-source-filesystem";
// Helper function to run graphql query to collect Markdown pages, and build up nodes.
async function createMdPages(createPage: Actions["createPage"], graphql) {
const blogPost = path.resolve(`./src/templates/md-post.tsx`);
// Query for all markdown pages, sorted ascending by date
const result = await graphql(`
{
allMarkdownRemark(sort: { frontmatter: { date: ASC } }, limit: 1000) {
edges {
node {
fields {
slug
}
frontmatter {
title
}
}
}
}
}
`);
if (result.errors) {
throw result.errors;
}
// Create blog posts pages.
const allPosts: { node: any }[] = result.data.allMarkdownRemark.edges;
// Split up by category based on the folder: e.g. blog, poetry, etc.
const postMap = allPosts.reduce(
(acc, cur) => {
const key = cur.node.fields.slug.split("/")[1];
if (!(key in acc)) {
acc[key] = [];
}
acc[key].push(cur);
return acc;
},
{} as Record<string, { node: any }[]>
);
// Go through all of our markdown objects and add links to previous/next to show in the footer
Object.entries(postMap).forEach(([key, posts]) => {
console.log("Generating", key);
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.slug,
component: blogPost,
context: {
slug: post.node.fields.slug,
previous,
next,
},
});
});
});
}
// Helper function to build theater pages
async function createTheaterPages(createPage, graphql) {
const theaterPost = path.resolve(`./src/templates/fountain-post.tsx`);
// Theater
const result = await graphql(`
{
allFountain {
edges {
node {
fields {
slug
}
frontmatter {
title
}
}
}
}
}
`);
if (result.errors) {
throw result.errors;
}
const posts = result.data.allFountain.edges;
posts.forEach((post) => {
createPage({
path: post.node.fields.slug,
component: theaterPost,
// this context is used by the page query for each fountain-post.tsx
context: {
slug: post.node.fields.slug,
},
});
});
}
// Helper function to build indices (blog, poetry, tech)
async function createMdIndexPages(createPage) {
const indices = [
{
slug: "poetry",
title: "Poetry",
description: "No fancy description. Just poetry. This is serious business.",
},
// {
// slug: "tech",
// title: "Tech Notes",
// description:
// "A small contribution to the massive collection of information about the tech world.",
// seo: [`tech`, `gatsby`, `javascript`, `react`],
// },
// {
// slug: "blog",
// title: "Life Enthusiasm",
// description: "The world is a beautiful place.",
// },
];
indices.forEach((ind) =>
createPage({
path: `/${ind.slug}/`,
matchPath: `/${ind.slug}`,
component: path.resolve(`./src/templates/md-index.tsx`),
context: {
myPath: `//${ind.slug}/.*/`,
title: ind.title,
description: ind.description,
},
})
);
}
// Create all of our dynamic pages (i.e. outside of `src/pages`)
export const createPages: GatsbyNode["createPages"] = async ({ graphql, actions }) => {
const { createPage } = actions;
await createMdPages(createPage, graphql);
await createTheaterPages(createPage, graphql);
await createMdIndexPages(createPage);
};
// For markdown and fountain pages, extend the node's fields by appending the slug. This allows us
// to use the slug in data queries on these objects
export const onCreateNode: GatsbyNode["onCreateNode"] = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
// TODO make these shared constants
if (node.internal.type === `MarkdownRemark` || node.internal.type === `Fountain`) {
const value = createFilePath({ node, getNode });
createNodeField({
name: `slug`,
node,
value,
});
}
};