-
Notifications
You must be signed in to change notification settings - Fork 6
/
gatsby-node.ts
108 lines (92 loc) · 2.5 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
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 { createNode, createNodeField } = actions;
if (node.internal.type === 'Mdx') {
const slug = path.basename(node.fileAbsolutePath, '.md');
const absolutePath = node.fileAbsolutePath;
console.log('Type of ' + typeof absolutePath);
const pathDirectory = path.dirname(absolutePath);
console.log(`path dir: ${pathDirectory}`);
let pathArray = pathDirectory.split(path.sep);
let contentType = pathArray[pathArray.length - 1]; // parent directory name will be the content type.
createNodeField({
node,
name: 'slug',
value: slug,
});
createNodeField({
node,
name: 'contentType',
value: contentType,
});
}
};
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: { order: ASC, fields: [frontmatter___date] }
) {
edges {
node {
fields {
slug
contentType
}
frontmatter {
title
}
}
}
}
}
`);
const projectOnlyResult = await graphql(`
query {
allMdx(filter: { fields: { contentType: { eq: "projects" } } }) {
edges {
node {
fields {
slug
contentType
}
}
}
}
}
`);
const posts = postOnlyResult.data.allMdx.edges;
posts.forEach((edge, index) => {
createPage({
component: blogTemplate,
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,
path: `/projects/${edge.node.fields.slug}`,
context: {
slug: edge.node.fields.slug,
},
});
});
};