-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgatsby-node.js
68 lines (61 loc) · 2.04 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
const { graphql } = require('gatsby')
const path = require('path')
exports.createPages = async({ actions, graphql, reporter }) => {
const { createPage } = actions
const blog_post_template = path.resolve('src/templates/blog-post.js')
const event_post_template = path.resolve('src/templates/event-post.js')
const res = await graphql(`
{
allMarkdownRemark(sort: { order: DESC, fields: [frontmatter___date] }) {
edges {
node {
fileAbsolutePath
frontmatter {
title
path
}
}
}
}
}
`)
if(res.errors) {
reporter.panicOnBuild('Error while creating blog pages (GraphQL query)')
return
}
var nodes = res.data.allMarkdownRemark.edges
var blog_nodes = nodes.filter(({ node }) => get_post_type(node.fileAbsolutePath) == 'blog_post')
blog_nodes.forEach(({ node }, i) => {
var prev = i === 0 ? null : blog_nodes[i - 1].node
var next = i === blog_nodes.length - 1 ? null : blog_nodes[i + 1].node
createPage({
path: node.frontmatter.path,
component: blog_post_template,
context: {
prev,
next
}
})
})
var event_nodes = nodes.filter(({ node }) => get_post_type(node.fileAbsolutePath) == 'event_post')
event_nodes.forEach(({ node }, i) => {
var prev = i === 0 ? null : event_nodes[i - 1].node
var next = i === event_nodes.length - 1 ? null : event_nodes[i + 1].node
createPage({
path: node.frontmatter.path,
component: event_post_template,
context: {
prev,
next
}
})
})
}
function get_post_type(abs_path) {
if(/\/posts\//.test(abs_path)) {
return 'blog_post'
} else if(/\/events\//.test(abs_path)) {
return 'event_post'
}
return ''
}