-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
189 lines (166 loc) · 4.42 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
/*--------------------------------------------------------*\
| ██████ ██ |
| ██ ██ ██ |
| ██ ██ ██ |
| ██████ ██ | binary : tech art
|
| Gatsby static site building scripts.
|----------------------------------------------------------
| Copyright(C) 2021 Valeriy Novytskyy
\*---------------------------------------------------------*/
import { paginate } from 'gatsby-awesome-pagination';
import { createFilePath } from 'gatsby-source-filesystem';
import { CONTENT } from './src/routes';
const ARTICLES_PER_PAGE = 5;
const Post = require.resolve('./src/components/Post/Post.tsx');
const Page = require.resolve('./src/components/Page.tsx');
const getPageUrl = (collection, relativeFilePath) => {
if (collection === 'logs') {
const [, projectPath, logPath] = relativeFilePath.split('/');
return `/projects/${projectPath}/${logPath}`;
}
return `/${collection}${relativeFilePath}`;
};
const getPageSubcollection = (relativeFilePath) => {
if (relativeFilePath.indexOf('/logs/') >= 0) {
const [, , projectPath] = relativeFilePath.split('/');
return projectPath;
}
};
const getPagePath = (collection, slug) => {
if (collection === 'logs') {
const [, projectPath, logPath] = slug.split('/');
return `/projects/${projectPath}/${logPath}`;
} else if (collection === 'pages') {
return `/${slug}`;
}
return `${collection}/${slug}`;
};
const getPageCollection = (collection, relativeFilePath) =>
collection === 'projects' && relativeFilePath.startsWith('/logs/')
? 'logs'
: collection;
const getComponentForPage = (collection) =>
collection === 'pages' ? Page : Post;
exports.createPages = async ({
actions: { createPage },
graphql,
reporter,
}) => {
const {
data: {
allMdx: { nodes },
},
errors: postErrors,
} = await graphql(`
{
allMdx {
nodes {
slug
fields {
collection
subCollection
}
frontmatter {
description
tags
title
}
rawBody
}
}
}
`);
const {
data: {
allMdx: { group: tags },
},
errors: tagErrors,
} = await graphql(`
{
allMdx {
group(field: frontmatter___tags) {
tag: fieldValue
}
}
}
`);
if (postErrors || tagErrors) {
reporter.panicOnBuild(`Error while running GraphQL queries`);
return;
}
// Generate posts
nodes.forEach(({ slug, fields: { collection, subCollection } }) => {
const path = getPagePath(collection, slug);
const component = getComponentForPage(collection, slug);
createPage({
path,
component,
context: {
slug,
collection,
subCollection,
},
});
});
// Generate top-level index pages
const postIndex = require.resolve('./src/components/PostIndex/PostIndex.tsx');
CONTENT.forEach(({ path, collection }) => {
paginate({
createPage,
items: nodes.filter((node) => node.fields.collection === collection),
itemsPerPage: ARTICLES_PER_PAGE,
pathPrefix: path,
component: postIndex,
context: {
collection,
},
});
});
// Generate tags pages
CONTENT.forEach(({ path, collection }) => {
tags.forEach(({ tag }) => {
createPage({
path: `${path.substring(1)}/tags/${tag}`,
component: postIndex,
context: {
collection,
tag,
},
});
});
});
};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `Mdx`) {
const relativeRootPath = createFilePath({ node, getNode });
const collection = getPageCollection(getNode(node.parent).sourceInstanceName, relativeRootPath);
const subCollection = getPageSubcollection(relativeRootPath);
const relativeFilePath = createFilePath({
node,
getNode,
basePath: collection
});
createNodeField({
node,
name: 'collection',
value: collection
});
createNodeField({
node,
name: 'subCollection',
value: subCollection
});
createNodeField({
node,
name: 'url',
value: getPageUrl(collection, relativeFilePath)
});
createNodeField({
node,
name: 'tags',
value: node.frontmatter.tags ? node.frontmatter.tags.map(t => t.trim().toLowerCase()) : []
});
}
};