-
Notifications
You must be signed in to change notification settings - Fork 5
/
gatsby-node.js
94 lines (81 loc) · 2.16 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");
const { paginate } = require("gatsby-awesome-pagination");
const _ = require("lodash");
const { createFilePath } = require(`gatsby-source-filesystem`);
exports.createPages = async ({ actions, graphql }) => {
const { createPage } = actions;
const query = await graphql(`
{
site {
siteMetadata {
postsPerPage
}
}
posts: allMdx(
sort: { order: DESC, fields: frontmatter___date }
filter: { fileAbsolutePath: { regex: "/blog/" } }
) {
nodes {
slug
}
}
tags: allMdx(sort: { order: DESC, fields: frontmatter___tags }) {
group(field: frontmatter___tags) {
tag: fieldValue
}
}
}
`);
// Check for any errors
if (query.errors) {
throw new Error(query.errors);
}
const posts = query.data.posts.nodes;
const tags = query.data.tags.group;
const blogIndexTemplate = path.resolve(`src/templates/blog.js`);
const tagsTemplate = path.resolve(`src/templates/tag.js`);
// Create tag pages
tags.forEach((tag) => {
createPage({
path: `/tags/${_.kebabCase(tag.tag)}`,
component: tagsTemplate,
context: {
tag: tag.tag,
},
});
});
// Create your paginated pages
paginate({
createPage, // The Gatsby `createPage` function
items: posts, // An array of objects
itemsPerPage: query.data.site.siteMetadata.postsPerPage,
pathPrefix: "/blog", // Creates pages like `/blog`, `/blog/2`, etc
component: blogIndexTemplate,
});
};
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === `Mdx`) {
const value = createFilePath({ node, getNode });
createNodeField({
name: `slug`,
node,
value,
});
}
};
// Define schema for Hello Bar & frontmatter
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type HelloBarYaml implements Node {
display: Boolean
title: String
end_date: Date
background_color: String
link_text: String
link_url: String
}
`;
createTypes(typeDefs);
};