Repository navigation
Expand file tree
/
Copy pathgatsby-node.js
More file actions
84 lines (78 loc) · 1.86 KB
/
Copy pathgatsby-node.js
File metadata and controls
84 lines (78 loc) · 1.86 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require("path")
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
const productTemplate = path.resolve("src/templates/product.js")
const blogTemplate = path.resolve("src/templates/blog.js")
return graphql(`
{
products: allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/products/" } }
) {
edges {
node {
html
id
frontmatter {
path
name
author
}
}
}
}
blogs: allMarkdownRemark(
sort: { order: DESC, fields: frontmatter___date }
filter: { fileAbsolutePath: { regex: "/blog/" } }
) {
edges {
next {
frontmatter {
path
}
}
node {
frontmatter {
path
author
title
date
}
}
previous {
frontmatter {
path
}
}
}
}
}
`).then((res) => {
if (res.errors) {
return Promise.reject(res.errors)
}
res.data.blogs.edges.forEach(({ previous, node, next }) => {
const path = `/blog/${node.frontmatter.path}`
createPage({
path,
component: blogTemplate,
context: {
previousPath: next ? next.frontmatter.path : null,
author: node.frontmatter.author,
shortPath: node.frontmatter.path,
nextPath: previous ? previous.frontmatter.path : null,
},
})
})
res.data.products.edges.forEach(({ node }) => {
createPage({
path: node.frontmatter.path,
component: productTemplate,
})
})
})
}