-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
210 lines (182 loc) · 5.63 KB
/
gatsby-node.js
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
const { createFilePath } = require('gatsby-source-filesystem');
const path = require('path');
const slugify = require('underscore.string/slugify');
const slug = (path) => slugify(path.replace(/\./g, 'dot'));
const createSlugField = (createNodeField, node, getNode) => {
const parentNode = getNode(node.parent);
const slug = parentNode.sourceInstanceName === 'posts' ? getSlugForPost(node, getNode) : null;
createNodeField({
name: 'slug',
node,
value: slug,
});
};
const getSlugForPost = (node, getNode) => {
const value = createFilePath({ node, getNode });
const [, date, title] = value.match(/^\/([\d]{4}-[\d]{2}-[\d]{2})-{1}(.+)\/$/);
return `/${date.replace(/-/g, '/')}/${title}/`;
};
const createMdxSource = (id, data, parentNode, actions, createNodeId, createContentDigest) => {
const { createNode, createParentChildLink } = actions;
const node = {
id: createNodeId(`${parentNode.id} >> ${id}`),
parent: parentNode.id,
children: [],
internal: {
type: `MdxSource`,
content: data,
contentDigest: createContentDigest(data),
mediaType: `text/markdown`,
},
};
createNode(node);
createParentChildLink({
parent: parentNode,
child: node,
});
};
exports.onCreateNode = ({ node, actions, getNode, createNodeId, createContentDigest }) => {
const { createNodeField } = actions;
// eslint-disable-next-line default-case
switch (node.internal.type) {
case 'Mdx':
createSlugField(createNodeField, node, getNode);
break;
case 'Site':
// eslint-disable-next-line no-case-declarations
const { disclaimer, authorDetails } = node.siteMetadata;
createMdxSource('disclaimer', disclaimer, node, actions, createNodeId, createContentDigest);
if (authorDetails && authorDetails.biography) {
createMdxSource('biography', authorDetails.biography, node, actions, createNodeId, createContentDigest);
}
break;
}
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = [
`type Site implements Node @infer {
siteMetadata: SiteSiteMetadata!
}`,
`type SiteSiteMetadata @infer {
title: String!
description: String
author: String!
siteUrl: String!
lang: String
logo: File @link(by: "relativePath")
disclaimer: Mdx @link(by: "rawBody")
authorDetails: SiteSiteMetadataAuthorDetails
sidebar: SiteSiteMetadataSidebar
twitter: SiteSiteMetadataTwitter
}`,
`type SiteSiteMetadataAuthorDetails @infer {
picture: File @link(by: "relativePath")
biography: Mdx @link(by: "rawBody")
}`,
`type SiteSiteMetadataSidebar @infer {
logos: [SiteSiteMetadataSidebarLogos]
}`,
`type SiteSiteMetadataTwitter @infer {
username: String!
}`,
`type SiteSiteMetadataSidebarLogos @infer {
image: File! @link(by: "relativePath")
title: String!
}`,
`type SitemapYaml implements Node @infer {
sub: [SitemapYamlSub]
}`,
`type SitemapYamlSub @infer {
sub: [SitemapYamlSub]
}`,
`type Mdx implements Node @infer {
frontmatter: MdxFrontmatter
}`,
`type MdxFrontmatter @infer {
categories: [String]
date: Date
dateFormatted: Date @dateformat(formatString: "MMMM D, YYYY") @proxy(from: "date")
image: File @fileByRelativePath
last_modified_at: Date
tags: [String]
title: String
}`,
];
createTypes(typeDefs);
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const { data } = await graphql(`
{
allMdx(filter: { fields: { slug: { ne: null } } }, sort: { order: DESC, fields: [frontmatter___date] }) {
tags: distinct(field: frontmatter___tags)
categories: distinct(field: frontmatter___categories)
edges {
next {
id
}
node {
id
fields {
slug
}
frontmatter {
date
last_modified_at
}
}
previous {
id
}
}
}
}
`);
const { tags, categories, edges } = data.allMdx;
tags.forEach((tag) => {
createPage({
path: `/tag/${slug(tag)}/`,
component: path.resolve(`./src/components/tagArchiveLayout.tsx`),
context: {
tag,
},
});
});
categories.forEach((category) => {
createPage({
path: `/category/${slug(category)}/`,
component: path.resolve(`./src/components/categoryArchiveLayout.tsx`),
context: {
category,
},
});
});
edges.forEach(({ node, previous, next }) => {
createPage({
path: node.fields.slug,
component: path.resolve(`./src/components/postLayout/postLayout.tsx`),
context: {
id: node.id,
previousId: next && next.id,
nextId: previous && previous.id,
lastModified: (node.frontmatter && (node.frontmatter.last_modified_at || node.frontmatter.date)) || null,
},
});
});
const pageSize = 8;
const totalPages = Math.ceil(edges.length / pageSize);
for (let index = 0; index < totalPages; index++) {
const groupedPosts = edges.slice(index * pageSize, (index + 1) * pageSize);
const pageIndex = index + 1;
createPage({
path: pageIndex === 1 ? `/` : `/page/${pageIndex}/`,
component: path.resolve(`./src/components/indexLayout/indexLayout.tsx`),
context: {
ids: groupedPosts.map((edge) => edge.node.id),
previousPageIndex: pageIndex > 1 ? pageIndex - 1 : undefined,
nextPageIndex: pageIndex < totalPages ? pageIndex + 1 : undefined,
},
});
}
};