-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgatsby-node.js
More file actions
102 lines (94 loc) · 2.68 KB
/
Copy pathgatsby-node.js
File metadata and controls
102 lines (94 loc) · 2.68 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
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
const path = require("path")
const _ = require("lodash")
const { createFilePath } = require("gatsby-source-filesystem")
const createPaginatedPages = require("gatsby-paginate")
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, reporter }) => {
const { createPage } = actions
const result = await graphql(`
query {
allMarkdownRemark(sort: { fields: [frontmatter___date], order: DESC }) {
edges {
node {
id
timeToRead
frontmatter {
title
category
cover
date
pageDescription
}
fields {
slug
}
}
}
}
tagsGroup: allMarkdownRemark(limit: 2000) {
group(field: frontmatter___tags) {
fieldValue
}
}
categoriesGroup: allMarkdownRemark {
group(field: frontmatter___category) {
fieldValue
}
}
}
`)
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
// let { title } = node.frontmatter;
// imgPath = './images';
// const imageSlug = createImage(title, imgPath);
createPage({
path: node.fields.slug,
component: path.resolve(
"./src/components/Blog/Templates/IndividualPost.js"
),
context: {
slug: node.fields.slug,
},
})
})
createPaginatedPages({
edges: result.data.allMarkdownRemark.edges,
createPage: createPage,
pageTemplate: "./src/components/Blog/Templates/Posts.js",
pageLength: 10, // This is optional and defaults to 10 if not used
pathPrefix: "", // This is optional and defaults to an empty string if not used
context: {}, // This is optional and defaults to an empty object if not used
})
result.data.tagsGroup.group.forEach((tag) => {
createPage({
path: `tags/${_.kebabCase(tag.fieldValue)}/`,
component: path.resolve(`./src/components/Blog/Views/TagPage.js`),
context: {
tag: tag.fieldValue,
},
})
})
result.data.categoriesGroup.group.forEach((category) => {
createPage({
path: `category/${_.kebabCase(category.fieldValue)}/`,
component: path.resolve(`./src/components/Blog/Views/CategoryPage.js`),
context: {
category: category.fieldValue,
},
})
})
}