Skip to content

Commit 8a31a33

Browse files
committed
use contentful for headless cms
1 parent a92b796 commit 8a31a33

14 files changed

+6499
-857
lines changed

gatsby-config.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,20 @@ module.exports = {
6969
name: `pages`,
7070
},
7171
},
72+
{
73+
resolve: `gatsby-source-contentful`,
74+
options: {
75+
spaceId: process.env.CONTENTFUL_SPACE_ID,
76+
// Learn about environment variables: https://gatsby.dev/env-vars
77+
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
78+
// host: process.env.NODE_ENV !== 'production' ? `preview.contentful.com` : `cdn.contentful.com`,
79+
downloadLocal: true,
80+
},
81+
},
7282
{
7383
resolve: `gatsby-transformer-remark`,
7484
options: {
7585
plugins: [
76-
`gatsby-remark-normalize-paths`,
7786
`gatsby-remark-check-links`,
7887
`gatsby-remark-external-links`,
7988
{
@@ -87,13 +96,14 @@ module.exports = {
8796
}
8897
},
8998
{
90-
resolve: `gatsby-remark-images`,
99+
resolve: `gatsby-remark-images-contentful`,
91100
options: {
101+
// max-w-3xlのwidthと同じサイズ
92102
maxWidth: 768,
93103
linkImagesToOriginal: false,
94104
backgroundColor: "transparent",
95105
withWebp: true,
96-
showCaptions: ['alt'],
106+
showCaptions: true,
97107
wrapperStyle: `white-space: normal; text-align: center;`,
98108
},
99109
},

gatsby-node.js

+29-88
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const path = require(`path`)
2-
const { createFilePath } = require(`gatsby-source-filesystem`)
32

43
exports.createPages = async ({ graphql, actions, reporter }) => {
54
const { createPage } = actions
@@ -8,90 +7,73 @@ exports.createPages = async ({ graphql, actions, reporter }) => {
87
const verticalArticle = path.resolve(`./src/templates/vertical-article.tsx`)
98
const horizontalArticle = path.resolve(`./src/templates/horizontal-article.tsx`)
109

11-
const resultAllMarkdowns = await graphql(
10+
const resultContentfulMarkdowns = await graphql(
1211
`
1312
{
14-
allMarkdownRemark(
15-
filter: { fields: { draft: { eq: false } } }
16-
sort: { fields: [frontmatter___publishedAt], order: DESC }
17-
limit: 1000
18-
) {
19-
nodes {
20-
id
21-
frontmatter {
22-
vol
23-
writing
24-
}
25-
fields {
13+
allContentfulMarkdownArticle(sort: {fields: publishedAt, order: DESC}) {
14+
edges {
15+
node {
16+
id
2617
slug
18+
isVirticalWriting
19+
publishedAt
20+
}
21+
next {
22+
id
23+
}
24+
previous {
25+
id
2726
}
2827
}
2928
}
3029
}
3130
`
3231
)
3332

34-
if (resultAllMarkdowns.errors) {
33+
if (resultContentfulMarkdowns.errors) {
3534
reporter.panicOnBuild(
3635
`There was an error loading your article posts`,
37-
resultAllMarkdowns.errors
36+
resultContentfulMarkdowns.errors
3837
)
3938
return
4039
}
4140

42-
const posts = resultAllMarkdowns.data.allMarkdownRemark.nodes
41+
const posts = resultContentfulMarkdowns.data.allContentfulMarkdownArticle.edges
4342

4443
// Create blog posts pages
4544
// But only if there's at least one markdown file found at "content/blog" (defined in gatsby-config.js)
4645
// `context` is available in the template as a prop and as a variable in GraphQL
4746

4847
if (posts.length > 0) {
4948
posts.forEach((post, index) => {
50-
const previousPostId = index === 0 ? null : posts[index - 1].id
51-
const nextPostId = index === posts.length - 1 ? null : posts[index + 1].id
52-
53-
const { slug } = post.fields
49+
const { slug } = post.node
5450
if (!slug) return
5551

56-
if (post.frontmatter.writing === "horizontal") {
52+
if (post.node.isVirticalWriting === true) {
5753
createPage({
58-
path: `/articles${slug}`,
59-
component: horizontalArticle,
54+
path: `/articles/${slug}/`,
55+
component: verticalArticle,
6056
context: {
61-
id: post.id,
62-
previousPostId,
63-
nextPostId,
57+
id: post.node.id,
58+
previousPostId: post.previous ? post.previous.id : null,
59+
nextPostId: post.next ? post.next.id : null,
6460
},
6561
})
66-
} else if (post.frontmatter.writing === "vertical") {
62+
} else{
6763
createPage({
68-
path: `/articles${slug}`,
69-
component: verticalArticle,
64+
path: `/articles/${slug}/`,
65+
component: horizontalArticle,
7066
context: {
71-
id: post.id,
72-
previousPostId,
73-
nextPostId,
67+
id: post.node.id,
68+
previousPostId: post.previous ? post.previous.id : null,
69+
nextPostId: post.next ? post.next.id : null,
7470
},
7571
})
7672
}
7773
})
7874
}
7975
}
8076

81-
exports.onCreateNode = ({ node, actions, getNode }) => {
82-
const { createNodeField } = actions
83-
84-
if (node.internal.type === `MarkdownRemark`) {
85-
const value = createFilePath({ node, getNode })
86-
87-
createNodeField({
88-
name: `slug`,
89-
node,
90-
value,
91-
})
92-
}
93-
}
94-
9577
exports.createSchemaCustomization = ({ actions }) => {
9678
const { createTypes } = actions
9779

@@ -103,48 +85,7 @@ exports.createSchemaCustomization = ({ actions }) => {
10385
// article posts are stored inside "content/article" instead of returning an error
10486
createTypes(`
10587
type SiteSiteMetadata {
106-
author: Author
10788
siteUrl: String
108-
social: Social
109-
}
110-
111-
type Author {
112-
name: String
113-
summary: String
114-
}
115-
116-
type Social {
117-
twitter: String
118-
}
119-
120-
type MarkdownRemark implements Node @infer {
121-
frontmatter: Frontmatter
122-
fields: Fields
123-
}
124-
125-
type Frontmatter @infer {
126-
title: String
127-
description: String
128-
author: String
129-
vol: String
130-
align: String
131-
writing: String
132-
profile: String
133-
twitter: String
134-
instagram: String
135-
minnakikeru: String
136-
bandcamp: String
137-
linktree: String
138-
hatena: String
139-
createdAt: Date @dateformat
140-
updatedAt: Date @dateformat
141-
publishedAt: Date @dateformat
142-
disableSideHeader: Boolean
143-
featuredImage: File
144-
}
145-
146-
type Fields {
147-
slug: String
14889
}
14990
`)
15091
}

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@
3838
"gatsby-remark-embedder": "^4.1.0",
3939
"gatsby-remark-external-links": "^0.0.4",
4040
"gatsby-remark-images": "^3.6.0",
41+
"gatsby-remark-images-contentful": "^2.10.0",
4142
"gatsby-remark-images-medium-zoom": "^1.7.0",
42-
"gatsby-remark-normalize-paths": "^1.1.0",
4343
"gatsby-remark-responsive-iframe": "^2.6.0",
4444
"gatsby-remark-smartypants": "^2.5.0",
45+
"gatsby-source-contentful": "^4.6.1",
4546
"gatsby-source-filesystem": "^2.6.0",
4647
"gatsby-transformer-json": "^2.6.0",
47-
"gatsby-transformer-remark": "^2.11.0",
48+
"gatsby-transformer-remark": "^2.16.0",
4849
"gatsby-transformer-sharp": "^2.12.0",
4950
"gsap": "^3.5.1",
5051
"intersection-observer": "^0.12.0",

0 commit comments

Comments
 (0)