-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgatsby-node.js
103 lines (92 loc) · 2.87 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
const path = require(`path`);
const { createFilePath } = require(`gatsby-source-filesystem`);
const slugify = require('./src/utils/slugify');
const getMdxDataForType = async ({ type, graphql }) => {
const { data } = await graphql(`
{
allMdx(filter: { frontmatter: { type: { eq: "${type}" } } }) {
nodes {
id
frontmatter {
title
name
coverImage
}
}
}
}
`);
console.log(
`🔥 Found ${data.allMdx.nodes.length} .mdx files with type: ${type}`
);
return data.allMdx.nodes;
};
const turnBlogPostsIntoPages = async ({ graphql, actions }) => {
const blogPostTemplate = path.resolve('./src/templates/blog/Post.js');
// Loop over the posts
const posts = await getMdxDataForType({ type: 'blog', graphql });
posts.forEach((post, i) => {
let prevPost;
let nextPost;
if (posts.length > 1) {
if (i !== 0) prevPost = posts[i - 1];
if (i !== post.length - 1) nextPost = posts[i + 1];
}
actions.createPage({
// What is the URL?
path: `blog/${slugify(post.frontmatter.title)}`,
// What react component should we use to render this page?
component: blogPostTemplate,
// What data should be surfaced to the Component or Query on this page?
context: {
coverImage: post.frontmatter.coverImage,
id: post.id,
prev: prevPost ? prevPost.frontmatter.slug : null,
next: nextPost ? nextPost.frontmatter.slug : null,
},
});
});
};
const turnBooksIntoPages = async ({ graphql, actions }) => {
const bookTemplate = path.resolve('./src/templates/book/BookPage.js');
const books = await getMdxDataForType({ type: 'book', graphql });
books.forEach((book, i) => {
actions.createPage({
path: `books/${slugify(book.frontmatter.title)}`,
component: bookTemplate,
context: {
id: book.id,
},
});
});
};
const turnAuthorsIntoPages = async ({ graphql, actions }) => {
const authorTemplate = path.resolve('./src/templates/author/AuthorPage.js');
const authors = await getMdxDataForType({ type: 'author', graphql });
authors.forEach(author => {
actions.createPage({
path: `author/${slugify(author.frontmatter.name)}`,
component: authorTemplate,
context: {
id: author.id,
name: author.frontmatter.name,
},
});
});
};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode, basePath: `pages` });
createNodeField({
node,
name: `slug`,
value: slug,
});
}
};
exports.createPages = async ({ graphql, actions }) => {
await turnBlogPostsIntoPages({ graphql, actions });
await turnBooksIntoPages({ graphql, actions });
await turnAuthorsIntoPages({ graphql, actions });
};