-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
139 lines (137 loc) · 3.77 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// const { createFilePath } = require("gatsby-source-filesystem")
const path = require("path")
// ______________________________________________________
//
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
// ______________________________________________________
//
/* ブログ記事のページを作成 */
const blogTemplate = path.resolve(`./src/template/post-page.tsx`)
const allPostData = await graphql(`
{
allContentfulBlogPost {
edges {
node {
slug
title
tags
updatedAt(formatString: "YYYY-MM-DD")
body {
childMarkdownRemark {
html
tableOfContents
}
}
heroImage {
gatsbyImageData(formats: PNG, layout: FULL_WIDTH)
title
}
svgContent {
svg {
content
}
}
}
}
}
}
`)
allPostData.data.allContentfulBlogPost.edges.forEach(({ node }) => {
createPage({
path: `/post/${node.slug}`,
component: blogTemplate,
context: {
head: {
title: node.title,
date: node.updatedAt,
},
tagInfo: {
tag: node.tags?.[0],
svg: node.svgContent?.svg.content,
},
assets: {
image: node.heroImage.gatsbyImageData,
alt: node.heroImage.title
},
body: node.body.childMarkdownRemark.html,
postIndex: node.body.childMarkdownRemark.tableOfContents
}
})
})
// ______________________________________________________
//
// ______________________________________________________
//
/* タグごとのページを作成 */
const tagPageTemplate = path.resolve(`./src/template/tag-page.tsx`)
const allTagsData = await graphql(`
{
allContentfulBlogPost {
edges {
node {
slug
title
updatedAt(formatString: "YYYY-MM-DD")
tags
svgContent {
svg {
content
}
}
heroImage {
gatsbyImageData(
layout: CONSTRAINED,
placeholder: BLURRED,
aspectRatio: 1.33
)
title
}
}
}
}
}
`)
const allTags = allTagsData.data.allContentfulBlogPost.edges.map(({node}) => {
return {
tag: node.tags[0],
svg: node.svgContent?.svg.content || 'Non image'
}
})
const allBlogCardInfo = allTagsData.data.allContentfulBlogPost.edges.map(({node}) => {
return {
tag: node.tags[0],
slug: node.slug,
title: node.title,
date: node.updatedAt,
image: node.heroImage.gatsbyImageData,
alt: node.heroImage.title
}
})
const distinctTags = allTags.filter((el, i, self) => self.findIndex(e => e.tag === el.tag) === i)
/* タグごとの投稿数を含んだ配列を作成 */
const postForEachTagsData = distinctTags.map(el => {
const postCount = allTags.filter(e => el.tag === e.tag).length
return {
...el,
postCount
}
})
postForEachTagsData.forEach(el => {
const blogCardInfo = allBlogCardInfo.filter(e => el.tag === e.tag)
createPage({
path: `tags/${el.tag}`,
component: tagPageTemplate,
context: {
tag: el.tag,
svg: el.svg,
postCount: el.postCount,
blogCardInfo: blogCardInfo
}
})
})
// ______________________________________________________
//
}
// ______________________________________________________
//