-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
72 lines (63 loc) · 1.7 KB
/
gatsby-node.js
File metadata and controls
72 lines (63 loc) · 1.7 KB
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 { createRemoteFileNode } = require("gatsby-source-filesystem")
const path = require('path')
const { getAllTags } = require('./static/data')
if (process.env.NODE_ENV === 'development') {
process.env.GATSBY_WEBPACK_PUBLICPATH = '/'
}
module.exports.onCreateNode = async ({ node, actions }) => {
const { createNodeField } = actions
if (node.internal.type === 'Mdx') {
const slug = path.basename(node.fileAbsolutePath, '.md')
createNodeField({
node,
name: 'slug',
value: slug,
})
}
}
module.exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const blogTemp = path.resolve('./src/templates/blog.js')
const tagTemp = path.resolve('./src/templates/tag.js')
const response = await graphql(`
query {
allMdx(sort: { fields: frontmatter___date, order: DESC }) {
edges {
node {
frontmatter {
slug
tags
title
}
}
}
}
}
`)
//Create /tags/${tag} pages
const allTags = getAllTags(response.data.allMdx)
Object.keys(allTags).map((tag) => {
createPage({
component: tagTemp,
path: `/tags/${tag}`,
context: {
tag,
},
})
})
const posts = response.data.allMdx.edges
//Create /blogs/${slug} pages
posts.forEach((edge, index) => {
const previous = index === 0 ? null : posts[index - 1]
const next = index === posts.length - 1 ? null : posts[index + 1]
createPage({
component: blogTemp,
path: `/blogs/${edge.node.frontmatter.slug}`,
context: {
slug: edge.node.frontmatter.slug,
prevPost: previous,
nextPost: next,
},
})
})
}