-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgatsby-node.ts
More file actions
124 lines (110 loc) · 3.08 KB
/
gatsby-node.ts
File metadata and controls
124 lines (110 loc) · 3.08 KB
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
import path from 'path';
export const onCreateWebpackConfig = ({ stage, actions }) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, 'src/'), 'node_modules'],
},
});
};
export const onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === 'Mdx') {
const contentFilePath = node.internal.contentFilePath;
// Support both .md and .mdx extensions
const slug = path.basename(contentFilePath).replace(/\.mdx?$/, '');
const pathDirectory = path.dirname(contentFilePath);
const pathArray = pathDirectory.split(path.sep);
const contentType = pathArray[pathArray.length - 1]; // parent directory name will be the content type.
const wordCount = String(node.body ?? '').split(/\s+/).filter(Boolean).length;
const timeToRead = Math.max(1, Math.ceil(wordCount / 200));
createNodeField({
node,
name: 'slug',
value: slug,
});
createNodeField({
node,
name: 'contentType',
value: contentType,
});
createNodeField({
node,
name: 'timeToRead',
value: timeToRead,
});
}
};
export const createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const blogTemplate = path.resolve('./src/templates/BlogPage/BlogPage.tsx');
const projectTemplate = path.resolve(
'./src/templates/ProjectPage/ProjectPage.tsx'
);
const postOnlyResult = await graphql(`
query {
allMdx(
filter: { fields: { contentType: { eq: "posts" } } }
sort: { frontmatter: { date: ASC } }
) {
edges {
node {
internal {
contentFilePath
}
fields {
slug
contentType
}
frontmatter {
title
}
}
}
}
}
`);
const projectOnlyResult = await graphql(`
query {
allMdx(filter: { fields: { contentType: { eq: "projects" } } }) {
edges {
node {
internal {
contentFilePath
}
fields {
slug
contentType
}
}
}
}
}
`);
if (postOnlyResult.errors) {
throw postOnlyResult.errors[0];
}
if (projectOnlyResult.errors) {
throw projectOnlyResult.errors[0];
}
const posts = postOnlyResult.data.allMdx.edges;
posts.forEach((edge, index) => {
createPage({
component: `${blogTemplate}?__contentFilePath=${edge.node.internal.contentFilePath}`,
path: `/blog/${edge.node.fields.slug}`,
context: {
slug: edge.node.fields.slug,
prev: index === 0 ? null : posts[index - 1].node,
next: index === posts.length - 1 ? null : posts[index + 1].node,
},
});
});
projectOnlyResult.data.allMdx.edges.forEach((edge) => {
createPage({
component: `${projectTemplate}?__contentFilePath=${edge.node.internal.contentFilePath}`,
path: `/projects/${edge.node.fields.slug}`,
context: {
slug: edge.node.fields.slug,
},
});
});
};