-
Notifications
You must be signed in to change notification settings - Fork 5
/
gatsby-node.js
118 lines (104 loc) · 3.59 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require(`path`)
const get = require("lodash/get")
const groupBy = require("lodash/groupBy")
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
const blogPostTemplate = path.resolve(`src/templates/blogTemplate.js`)
const learningTemplate = path.resolve(`src/templates/learningTemplate.js`)
const galleryTemplate = path.resolve(`src/templates/galleryTemplate.js`)
const getTemplate = frontmatter => {
// If path starts with /learning it's a learning article
// If it has a "submitted_by" attribute it's gallery
// Otherwise it's a blog post
const path = get(frontmatter, "path")
if (path.indexOf("/learning") === 0) {
return learningTemplate
} else if (frontmatter.layout === "gallery_project") {
return galleryTemplate
} else {
return blogPostTemplate
}
}
const getPostType = frontmatter => {
// If path starts with /learning it's a learning article
// If it has a "submitted_by" attribute it's gallery
// Otherwise it's a blog post
const path = get(frontmatter, "path")
if (path.indexOf("/learning") === 0) {
return 'learning'
} else if (frontmatter.layout === "gallery_project") {
return 'gallery'
} else {
return 'blog'
}
}
return graphql(`
{
allMarkdownRemark(sort: { order: DESC, fields: [frontmatter___date] }) {
edges {
node {
frontmatter {
path
layout
title
}
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors)
}
const nodesByType = groupBy(result.data.allMarkdownRemark.edges, edge => getPostType(edge.node.frontmatter))
let blogPages = get(nodesByType, 'blog', []).map(({ node }) => {
return createPage({
path: node.frontmatter.path,
component: blogPostTemplate,
context: {}, // additional data can be passed via context
})
})
const galleryNodes= get(nodesByType, 'gallery', [])
let galleryPages = galleryNodes.map(({ node }, i) => {
return createPage({
path: node.frontmatter.path,
component: galleryTemplate,
context: {
nextPage: i < galleryNodes.length - 1 ? {path: galleryNodes[i+1].node.frontmatter.path, title: galleryNodes[i+1].node.frontmatter.title} : undefined,
prevPage: i > 0 ? {path: galleryNodes[i-1].node.frontmatter.path, title: galleryNodes[i-1].node.frontmatter.title} : undefined
}, // additional data can be passed via context
})
})
let learningPages = get(nodesByType, 'learning', []).map(({ node }) => {
return createPage({
path: node.frontmatter.path,
component: learningTemplate,
context: {}, // additional data can be passed via context
})
})
// Create blog-list pages
const posts = result.data.allMarkdownRemark.edges.filter(
x => get(x, "node.frontmatter.layout") !== "gallery_project"
)
const postsPerPage = 9
const numPages = Math.ceil(posts.length / postsPerPage)
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/blog` : `/blog/${i + 1}`,
component: path.resolve("./src/templates/blogListTemplate.js"),
context: {
limit: postsPerPage,
skip: i * postsPerPage,
numPages,
currentPage: i + 1,
},
})
})
return {blogPages, galleryPages, learningPages}
})
}