-
Notifications
You must be signed in to change notification settings - Fork 3
/
gatsby-node.ts
81 lines (76 loc) · 2.1 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
import { type GatsbyNode, type NodeInput } from "gatsby";
import { getAllBlogs } from "./src/lib/blog-fetch";
import { getMembers } from "./src/lib/member-fetch";
import path from "path";
export const sourceNodes: GatsbyNode["sourceNodes"] = async (api) => {
const members = await getMembers();
for (const member of members) {
const id = api.createNodeId(member.discordId);
const node = {
...member,
id,
_id: member.discordId,
parent: null,
children: [],
internal: {
type: "Member",
contentDigest: api.createContentDigest(member),
},
} satisfies NodeInput;
api.actions.createNode(node);
}
const blogs = await getAllBlogs();
for (const blog of blogs) {
const id = api.createNodeId(blog.slug);
const node = {
...blog,
id,
_id: blog.slug,
parent: null,
children: [],
internal: {
type: "Blog",
contentDigest: api.createContentDigest(blog),
},
} satisfies NodeInput;
api.actions.createNode(node);
}
};
export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] = (api) => {
api.actions.createTypes(`
enum SNSKind {
twitter
github
}
type SNSLinkInfo {
type: SNSKind!
name: String!
}
type Member implements Node {
username: String!
discordId: ID!
associatedLinks: [SNSLinkInfo!]!
}
`);
};
export const createPages: GatsbyNode["createPages"] = async (api) => {
const pages = await getAllBlogs();
const component = path.resolve("src/templates/blog-post.tsx");
api.reporter.info(`generating ${pages.length} pages`);
for (let i = 0; i < pages.length; ++i) {
const slug = pages[i].slug;
const prevSlug = i > 0 ? path.basename(pages[i - 1].slug) : null;
const nextSlug = i < pages.length - 1 ? path.basename(pages[i + 1].slug) : null;
api.actions.createPage({
path: `/blog/${slug}`,
component,
context: {
slug,
content: pages[i].markdownBody,
frontmatter: pages[i].frontmatter,
prevSlug,
nextSlug,
},
});
}
};