-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
94 lines (79 loc) · 2.17 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
const path = require('path');
exports.onCreateNode = ({node, actions, getNode}) => {
const {createNodeField} = actions;
if (node.internal.type === 'MarkdownRemark') {
const {permalink} = node.frontmatter;
const {relativePath} = getNode(node.parent);
let slug = permalink;
const parsedFilePath = path.parse(relativePath);
const {dir, name} = parsedFilePath;
if (dir === ``) {
slug = name;
} else {
slug = dir;
}
createNodeField({
node,
name: 'path',
value: `/${slug}`,
});
createNodeField({
node,
name: 'slug',
value: slug,
});
}
};
exports.createPages = async ({graphql, actions}) => {
const {createPage} = actions;
const postComponent = path.resolve(`./src/templates/post.jsx`);
const allMarkdown = await graphql(`
{
allMarkdownRemark(filter: {frontmatter: {draft: {ne: true}}}, sort: {fields: [frontmatter___date], order: DESC}) {
edges {
node {
fields {
path
slug
}
frontmatter {
title
type
}
}
}
}
}
`);
if (allMarkdown.errors) {
console.error(allMarkdown.errors);
throw new Error(allMarkdown.errors);
}
const posts = allMarkdown.data.allMarkdownRemark.edges;
const articles = posts.filter(edge => edge.node.frontmatter.type === `article`);
const projects = posts.filter(edge => edge.node.frontmatter.type === `project`);
articles.forEach(({node}, index) => {
const {slug} = node.fields;
createPage({
path: slug, // required
component: postComponent,
context: {
slug,
prev: index === 0 ? null : articles[index - 1].node,
next: index === articles.length - 1 ? null : articles[index + 1].node,
},
});
});
projects.forEach(({node}, index) => {
const {slug} = node.fields;
createPage({
path: slug, // required
component: postComponent,
context: {
slug,
prev: index === 0 ? null : projects[index - 1].node,
next: index === projects.length - 1 ? null : projects[index + 1].node,
},
});
});
};