-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.ts
211 lines (183 loc) · 5.25 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import type { GatsbyNode } from "gatsby";
import { createFilePath } from "gatsby-source-filesystem";
import path from "path";
import { getLibraryManifest } from "./src/api/cdn";
import { listPlans } from "./src/api/subscriptions";
import { BlogListLayoutPageContext } from "./src/layouts/blog-list";
export const createSchemaCustomization: GatsbyNode["createSchemaCustomization"] =
({ actions: { createTypes } }) =>
createTypes(`
type Site {
siteMetadata: SiteMetadata!
}
type SiteMetadata {
name: String!
tagline: String!
description: String!
twitter: String!
siteUrl: String!
googlePlayUrl: String!
}
type Mdx implements Node {
frontmatter: Frontmatter
fields: Fields!
}
type Frontmatter {
redirect: Redirect
publishedAt: Date @dateformat
updatedAt: Date @dateformat
permalink: String
}
type Redirect {
url: String!
permanent: Boolean
}
type Fields {
slug: String!
}
type SoundLibraryInfo implements Node {
totalSoundCount: Int!
premiumSoundCount: Int!
freeSoundCount: Int!
freeSoundWithPremiumSegmentsCount: Int!
}
type PremiumPlan implements Node {
billingPeriodMonths: Int!
priceInIndianPaise: Int!
trialPeriodDays: Int!
}
`);
export const sourceNodes: GatsbyNode["sourceNodes"] = async ({
actions: { createNode },
createContentDigest,
}) => {
// Both Stripe and Google Play plans should be identical.
(await listPlans("stripe")).forEach((plan) => {
createNode({
...plan,
internal: {
type: "PremiumPlan",
contentDigest: createContentDigest(plan),
},
});
});
const sounds = (await getLibraryManifest()).sounds;
const premiumSoundCount = sounds.filter((sound) =>
sound.segments.every((segment) => !segment.isFree)
).length;
const freeSoundWithPremiumSegmentsCount = sounds.filter(
(sound) =>
sound.segments.some((segment) => segment.isFree) &&
sound.segments.some((segment) => !segment.isFree)
).length;
const libraryInfo = {
totalSoundCount: sounds.length,
premiumSoundCount: premiumSoundCount,
freeSoundCount: sounds.length - premiumSoundCount,
freeSoundWithPremiumSegmentsCount: freeSoundWithPremiumSegmentsCount,
};
createNode({
id: createContentDigest(libraryInfo),
...libraryInfo,
internal: {
type: "SoundLibraryInfo",
contentDigest: createContentDigest(libraryInfo),
},
});
};
export const onCreateNode: GatsbyNode["onCreateNode"] = ({
node,
actions: { createNodeField },
getNode,
}) => {
if (node.internal.type === `Mdx`) {
const { permalink } = node.frontmatter as Queries.Frontmatter;
const slug = (permalink || createFilePath({ node, getNode }))
.replace(/\/$/, "") // remove trailing slash
.replace(/^\//, ""); // remove leading slash
createNodeField({
node,
name: "slug",
value: slug,
});
}
};
export const createPages: GatsbyNode["createPages"] = async ({
actions: { createPage, createRedirect },
graphql,
reporter,
}) => {
const result: { data?: Queries.AllMdxQuery; errors?: unknown } =
await graphql(`
query AllMdx {
allMdx {
nodes {
id
frontmatter {
layout
redirect {
url
permanent
}
}
fields {
slug
}
internal {
contentFilePath
}
}
}
}
`);
if (result.errors) {
reporter.panicOnBuild("Error while running GraphQL query.");
return;
}
const { allMdx: { nodes } = { nodes: [] } } = result.data || {};
nodes.forEach((node) => {
// only render markdown pages that specify a layout.
if (node.frontmatter?.layout == null) {
return;
}
const layout = path.resolve(`src/layouts/${node.frontmatter.layout}.tsx`);
createPage({
path: node.fields.slug,
component: `${layout}?__contentFilePath=${node.internal.contentFilePath}`,
context: {
id: node.id,
},
});
});
// render blog list
const blogPostCount = nodes.filter((node) =>
node.fields.slug.startsWith("blog/")
).length;
const blogPostsPerPage = 6;
const blogListPageCount = Math.ceil(blogPostCount / blogPostsPerPage);
for (let i = 0; i < blogListPageCount; i += 1) {
const pageContext: BlogListLayoutPageContext = {
limit: blogPostsPerPage,
skip: i * blogPostsPerPage,
currentPage: i + 1,
totalPageCount: blogListPageCount,
prevPageHref: i === 0 ? null : i > 1 ? `/blog/page/${i}` : "/blog",
nextPageHref: i + 2 > blogListPageCount ? null : `/blog/page/${i + 2}`,
};
createPage({
path: i === 0 ? "/blog" : `/blog/page/${i + 1}`,
component: path.resolve("src/layouts/blog-list.tsx"),
context: pageContext,
});
}
nodes.forEach((node) => {
if (node.frontmatter?.redirect == null) {
return;
}
createRedirect({
fromPath: node.fields.slug,
toPath: node.frontmatter.redirect.url,
isPermanent: node.frontmatter.redirect.permanent ?? undefined,
});
});
};