generated from sonho463/starter-microcms-gc
/
gatsby-node.js
94 lines (84 loc) · 2.36 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
const { notDeepEqual } = require("assert")
const path = require("path")
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions
const blogresult = await graphql(`
query {
microcmsConfig {
title
subtitle
}
allMicrocmsPosts(sort: { fields: updatedAt, order: DESC }) {
edges {
node {
id
link
}
previous {
title
link
}
next {
title
link
}
}
}
allMicrocmsCategory {
edges {
node {
categorySlug
id
category
}
}
}
}
`)
if (blogresult.errors) {
reporter.panicOnBuild(`GraphQLのクエリでエラーが発生しました`)
}
blogresult.data.allMicrocmsPosts.edges.forEach(({ node, previous, next }) => {
createPage({
path: `/blog/posts/${node.link}`,
component: path.resolve(`./src/templates/post-template.jsx`),
context: {
id: node.id,
previous,
next,
},
})
})
const blogPostsPerPage = 4 // 1ページあたりの記事数
const blogPosts = Object.keys(blogresult.data.allMicrocmsPosts.edges).length // 記事総数
const blogPages = Math.ceil(blogPosts / blogPostsPerPage) // 記事一覧ページの数
const blogTitle = blogresult.data.microcmsConfig.title
const blogSubtitle = blogresult.data.microcmsConfig.subtitle
Array.from({ length: blogPages }).forEach((_, i) => {
// blogPosts.forEach((_, i) => {
createPage({
path: i === 0 ? `/` : `/${i + 1}/`,
component: path.resolve("./src/templates/index-template.jsx"),
context: {
skip: blogPostsPerPage * i,
limit: blogPostsPerPage,
currentPage: i + 1, //最初のページNO
isFirst: i + 1 === 1, // 最初のページかどうか
isLast: i + 1 === blogPages, //最後のページかどうか
title: blogTitle,
subtitle: blogSubtitle,
},
})
})
// カテゴリページの生成
blogresult.data.allMicrocmsCategory.edges.forEach(({ node }) => {
createPage({
path: `/cat/${node.categorySlug}/`,
component: path.resolve(`./src/templates/category-template.jsx`),
context: {
catName: node.category,
catSlug: node.categorySlug,
},
})
})
}