-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.esm.js
77 lines (70 loc) · 2.04 KB
/
gatsby-node.esm.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
import { GrepTags, GrepPostsByTags, GrepCategories, GrepPostsByCategories } from "./src/components/Grep.js"
const path = require("path")
const createTagPages = (createPage, posts) => {
const singleTagIndexTemplate = path.resolve("src/templates/SingleTagIndex.js")
const tags = GrepTags(posts)
tags.forEach(tagName => {
const filteredPosts = GrepPostsByTags(posts)(tagName)
createPage({
path: `/tags/${tagName}`,
component: singleTagIndexTemplate,
context: {
filteredPosts,
tagName,
},
})
})
}
const createCategoriesPages = (createPage, posts) => {
const singleCategoryIndexTemplate = path.resolve("src/templates/SingleCategoryIndex.js")
const categories = GrepCategories(posts)
categories.forEach(categoryName => {
const filteredPosts = GrepPostsByCategories(posts)(categoryName)
createPage({
path: `/categories/${categoryName}`,
component: singleCategoryIndexTemplate,
context: {
filteredPosts,
categoryName,
},
})
})
}
export const createPages = async ({ actions: { createPage }, graphql }) => {
const blogPostTemplate = path.resolve("src/templates/blogPost.js")
const result = await graphql(
`query {
allMarkdownRemark {
edges {
node {
frontmatter {
path
tags
title
categories
}
}
}
}
}
`)
if (result.error) {
console.error("Something went wrong!")
return
}
const posts = result.data.allMarkdownRemark.edges
createCategoriesPages(createPage, posts)
createTagPages(createPage, posts)
posts.forEach(({ node }, index) => {
const path = node.frontmatter.path
createPage({
path,
component: blogPostTemplate,
context: {
pathSlug: path,
prev: index === 0 ? null : posts[index - 1].node,
next: index === posts.length - 1 ? null : posts[index + 1].node,
},
})
})
}