-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgatsby-node.js
More file actions
173 lines (163 loc) · 3.96 KB
/
gatsby-node.js
File metadata and controls
173 lines (163 loc) · 3.96 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* eslint-disable @typescript-eslint/restrict-plus-operands, @typescript-eslint/no-var-requires */
const path = require('path');
const POSTS_PER_PAGE = 6;
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
const postsResult = await graphql(`
{
allContentfulPost(limit: 100, sort: {
fields: [featured, updatedAt],
order: [DESC, ASC]})
{
edges {
node {
title
slug
featured
updatedAt(formatString: "d MMMM yyyy")
tags {
slug
tagName
}
hero {
fluid(maxWidth: 2540) {
src
}
fixed {
src
}
}
body {
childMarkdownRemark {
htmlAst
excerpt(format: PLAIN, pruneLength: 200)
timeToRead
}
}
author {
name
slug
subtitle
avatar {
fluid(maxWidth: 800) {
src
}
}
}
}
}
}
}`);
const authorsResult = await graphql(`
{
allContentfulAuthor {
edges {
node {
slug
}
}
}
}`);
const tagsResult = await graphql(`
{
allContentfulTag {
edges {
node {
slug
}
}
}
}`);
const pagesResult = await graphql(`
{
allContentfulPage {
edges {
node {
slug
}
}
}
}`);
// Handle errors
if (postsResult.errors || authorsResult.errors || tagsResult.errors || pagesResult.errors) {
reporter.panicOnBuild('Error while running GraphQL query.');
return;
}
const posts = postsResult.data.allContentfulPost.edges;
// Create paginated index
const indexTemplate = path.resolve('./src/templates/index.tsx');
const numPages = Math.ceil(posts.length / POSTS_PER_PAGE);
Array.from({ length: numPages }).forEach((_, i) => {
createPage({
path: i === 0 ? '/' : `/${i + 1}`,
component: indexTemplate,
context: {
limit: POSTS_PER_PAGE,
skip: i * POSTS_PER_PAGE,
numPages,
currentPage: i + 1,
},
});
});
// post creatipon logic
const postTemplate = path.resolve('./src/templates/post.tsx');
posts.forEach(({ node }, index) => {
const prev = index === 0 ? null : posts[index - 1].node;
const next = index === posts.length - 1 ? null : posts[index + 1].node;
createPage({
path: `/${node.slug}/`,
component: postTemplate,
context: {
prev,
next,
slug: node.slug,
primaryTag: node.tags ? node.tags[0].slug : '',
},
});
});
// tag creation logic
const tags = tagsResult.data.allContentfulTag.edges;
const tagTemplate = path.resolve('./src/templates/tags.tsx');
tags.forEach(({ node }) => {
createPage({
path: `/tags/${node.slug}/`,
component: tagTemplate,
context: {
tag: node.slug,
},
});
});
// Create author pages
const authorTemplate = path.resolve('./src/templates/author.tsx');
const authors = authorsResult.data.allContentfulAuthor.edges;
authors.forEach(({ node }) => {
createPage({
path: `/author/${node.slug}/`,
component: authorTemplate,
context: {
author: node.slug,
},
});
});
// create pages
const pageTemplate = path.resolve('./src/templates/page.tsx');
const pages = pagesResult.data.allContentfulPage.edges;
pages.forEach(({ node }) => {
console.log(node);
createPage({
path: `/pages/${node.slug}/`,
component: pageTemplate,
context: {
slug: node.slug,
},
});
});
};
exports.onCreateWebpackConfig = ({ stage, actions }) => {
// adds sourcemaps for tsx in dev mode
if (stage === 'develop' || stage === 'develop-html') {
actions.setWebpackConfig({
devtool: 'eval-source-map',
});
}
};