-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
104 lines (87 loc) · 2.35 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
const path = require("path");
const _ = require("lodash");
//Generate slug for blog posts path.
exports.onCreateNode = ({ node, actions }) => {
const { createNodeField } = actions;
let slug;
if (node.internal.type === "MarkdownRemark") {
slug = path.basename(node.fileAbsolutePath, ".md");
createNodeField({
node,
name: "slug",
value: slug,
})
console.log(slug);
}
}
//Create pages and coresponding url for each markdown file.
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const response = await graphql(`
query {
site {
siteMetadata {
domain: siteUrl
}
}
allMarkdownRemark (sort:{ order: ASC, fields: [frontmatter___date]}
limit: 1000
){
edges {
node {
fields {
slug
}
frontmatter {
tags
title
}
}
}
}
tagsGroup: allMarkdownRemark(limit: 2000) {
group(field: frontmatter___tags) {
fieldValue
}
}
}
`)
// Create blog-list pages
const articles = response.data.allMarkdownRemark.edges;
const postsPerPage = 10;
const numPages = Math.ceil(articles.length / postsPerPage);
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/blog/` : `/blog/${i + 1}/`,
component: path.resolve("./src/templates/blog-list.js"),
context: {
limit: postsPerPage,
skip: i * postsPerPage,
numPages,
currentPage: i + 1,
},
})
})
//create page for each post
articles.forEach(({ node }, index) => {
createPage({
path: `/blog/${node.fields.slug}/`,
component: path.resolve("./src/templates/blog-post.js"),
context: {
slug: node.fields.slug,
prev: index === 0 ? null : articles[index - 1].node,
next: index === articles.length - 1 ? null : articles[index + 1].node
},
})
})
// Extract tag data from query and create page
response.data.tagsGroup.group.forEach(tag => {
createPage({
path: `/tags/${_.kebabCase(tag.fieldValue)}/`,
component: path.resolve("./src/templates/tags.js"),
context: {
tag: tag.fieldValue,
},
})
})
}