-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.ts
114 lines (100 loc) · 2.71 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
import type { GatsbyNode } from "gatsby";
import path from "path";
import readingTime from "reading-time";
export const onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `Mdx`) {
createNodeField({
node,
name: `timeToRead`,
value: readingTime(node.body),
});
}
};
export const createPages: GatsbyNode["createPages"] = async ({
graphql,
actions,
reporter,
}) => {
const blogPostTemplate = path.resolve("src/templates/blog-post-template.tsx");
const result = await graphql<Queries.BlogPagesQuery>(
`
query BlogPages {
allMdx(
sort: { frontmatter: { date: DESC } }
filter: { frontmatter: { draft: { eq: false } } }
) {
nodes {
id
body
frontmatter {
title
slug
}
internal {
contentFilePath
}
}
}
}
`
);
if (result.errors) {
reporter.panicOnBuild("Error loading MDX result", result.errors);
}
const posts = result.data?.allMdx.nodes;
posts?.forEach((post, index) => {
const previousPostId = index === 0 ? null : posts[index - 1].id;
const nextPostId = index === posts.length - 1 ? null : posts[index + 1].id;
actions.createPage({
path: `blog/${post.frontmatter.slug}`,
component: `${blogPostTemplate}?__contentFilePath=${post.internal.contentFilePath}`,
context: {
id: post.id,
previousPostId,
nextPostId,
},
});
});
};
export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] =
({ actions }) => {
const { createTypes, printTypeDefinitions } = actions;
createTypes(`
type Site implements Node {
siteMetadata: SiteSiteMetadata!
}
type SiteSiteMetadata {
title: String!
description: String!
author: String!
siteUrl: String!
image: String!
imageAlt: String!
social: SiteSiteMetadataSocial!
}
type SiteSiteMetadataSocial {
twitter: String!
}
type SoftwareJson implements Node {
_2019: [String!]!
_2020: [String!]!
}
type MdxFrontmatter @dontInfer {
title: String!
date: Date! @dateformat
updated: Date @dateformat
description: String!
category: String
image: File! @fileByRelativePath
imageAlt: String!
linkText: String
draft: Boolean!
slug: String!
}
type Mdx implements Node {
timeToRead: Float @proxy(from: "fields.timeToRead.minutes")
}
`);
// printTypeDefinitions({ path: "./typeDefs.txt" });
};