-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
executable file
·72 lines (64 loc) · 1.77 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
const { createFilePath } = require(`gatsby-source-filesystem`)
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
const slug = value.replace('/blog/', '').replace(/\/$/, '')
const url = `/blog/${slug}`
createNodeField({
name: `slug`,
node,
value: url,
})
}
}
const path = require(`path`)
// 1. This is called once the data layer is bootstrapped to let plugins create pages from data.
exports.createPages = ({ graphql, actions }) => {
// 1.1 Getting the method to create pages
const { createPage } = actions
// 1.2 Tell which layout Gatsby should use to thse pages
const blogLayout = path.resolve(`./src/components/BlogPost/BlogPost.tsx`)
// 2 Return the method with the query
return graphql(`
query blogPosts {
allMarkdownRemark(sort: { fields: [frontmatter___date], order: DESC }) {
edges {
node {
fields {
slug
}
frontmatter {
title
date
author
category
tags
featured
}
html
}
}
}
}
`).then(result => {
// 2.1 Handle the errors
if (result.errors) {
console.error(result.errors)
reject(result.errors)
}
// 2.2 Our posts are here
const posts = result.data.allMarkdownRemark.edges
// 3 Loop throught all posts
posts.forEach((post, index) => {
// 3.1 Finally create posts
createPage({
path: post.node.fields.slug,
component: blogLayout,
context: {
slug: post.node.fields.slug,
},
})
})
})
}