Skip to content

Commit b911d5c

Browse files
committed
chore(gatsby): add JS gatsby-node for runtime (compiled from TS)
1 parent bccd576 commit b911d5c

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

gatsby-node.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
const { createFilePath } = require('gatsby-source-filesystem');
4+
5+
exports.createPages = async ({ graphql, actions }) => {
6+
const { createPage } = actions;
7+
8+
const authorJson = require('./content/authors.json');
9+
const authorPage = path.resolve('./src/templates/author-page.js');
10+
11+
for (let author of Object.keys(authorJson)) {
12+
try {
13+
await fs.promises.access(`content/assets/authors/${author}.jpg`);
14+
} catch (error) {
15+
const githubUsername = authorJson[author].github;
16+
const response = await fetch(`https://github.com/${githubUsername}.png?size=250`);
17+
if (!response.ok) {
18+
throw new Error(`Unexpected response: ${response.statusText}`);
19+
}
20+
const arrayBuf = await response.arrayBuffer();
21+
const uint8 = new Uint8Array(arrayBuf);
22+
await fs.promises.writeFile(`content/assets/authors/${author}.jpg`, uint8);
23+
}
24+
25+
createPage({
26+
path: `/author/${author}`,
27+
component: authorPage,
28+
context: {
29+
author: author,
30+
limit: 10,
31+
},
32+
});
33+
}
34+
35+
const tagTemplate = path.resolve('./src/templates/tag-page.js');
36+
const blogPost = path.resolve('./src/templates/blog-post.js');
37+
const result = await graphql(`
38+
{
39+
allMdx(sort: {frontmatter: {date: DESC}}) {
40+
edges {
41+
node {
42+
fields {
43+
slug
44+
postPath
45+
}
46+
frontmatter {
47+
title
48+
tags
49+
}
50+
internal {
51+
contentFilePath
52+
}
53+
}
54+
}
55+
}
56+
tagsGroup: allMdx(limit: 2000) {
57+
group(field: {frontmatter: {tags: SELECT}}) {
58+
fieldValue
59+
}
60+
}
61+
}
62+
`);
63+
64+
if (result.errors) {
65+
throw result.errors;
66+
}
67+
68+
if (!result.data) {
69+
throw new Error('Error retrieving blog posts');
70+
}
71+
72+
const posts = result.data.allMdx.edges;
73+
74+
posts.forEach((post, index) => {
75+
const previous = index === posts.length - 1 ? null : posts[index + 1].node;
76+
const next = index === 0 ? null : posts[index - 1].node;
77+
78+
createPage({
79+
path: `${post.node.fields.postPath}`,
80+
component: `${blogPost}?__contentFilePath=${post.node.internal.contentFilePath}`,
81+
context: {
82+
slug: post.node.fields.slug,
83+
postPath: post.node.fields.postPath,
84+
previous,
85+
next,
86+
},
87+
});
88+
});
89+
90+
const tags = result.data.tagsGroup.group;
91+
92+
tags.forEach((tag) => {
93+
createPage({
94+
path: `/tags/${tag.fieldValue}/`,
95+
component: tagTemplate,
96+
context: {
97+
tag: tag.fieldValue,
98+
},
99+
});
100+
});
101+
102+
const postsPerPage = 10;
103+
const numPages = Math.ceil(posts.length / postsPerPage);
104+
Array.from({ length: numPages }).forEach((_, index) => {
105+
const currentPageNumber = index + 1;
106+
const previousPageNumber = currentPageNumber === 1 ? null : currentPageNumber - 1;
107+
const nextPageNumber = currentPageNumber === numPages ? null : currentPageNumber + 1;
108+
109+
createPage({
110+
path: `/page/${index + 1}`,
111+
component: path.resolve('./src/templates/blog-page.js'),
112+
context: {
113+
limit: postsPerPage,
114+
skip: index * postsPerPage,
115+
numPages,
116+
currentPageNumber,
117+
previousPageNumber,
118+
nextPageNumber,
119+
},
120+
});
121+
});
122+
};
123+
124+
exports.onCreateNode = async ({ node, actions, getNode }) => {
125+
const { createNodeField } = actions;
126+
127+
if (node.internal.type === 'Mdx') {
128+
const slug = createFilePath({ node, getNode });
129+
const date = new Date(node.frontmatter.date);
130+
const year = date.getFullYear();
131+
const zeroPaddedMonth = `${date.getMonth() + 1}`.padStart(2, '0');
132+
133+
createNodeField({
134+
name: 'slug',
135+
node,
136+
value: slug,
137+
});
138+
createNodeField({
139+
name: 'postPath',
140+
node,
141+
value: `/${year}/${zeroPaddedMonth}${slug}`,
142+
});
143+
}
144+
};

0 commit comments

Comments
 (0)