-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
101 lines (92 loc) · 2.43 KB
/
Copy pathgatsby-node.js
File metadata and controls
101 lines (92 loc) · 2.43 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
const path = require('path');
const fs = require('fs');
const { createFileNodeFromBuffer } = require('gatsby-source-filesystem');
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
const result = await graphql(`
query {
allMdx(filter: { frontmatter: { type: { eq: "project" } } }) {
nodes {
id
frontmatter {
slug
dateAdded
}
internal {
contentFilePath
}
}
}
}
`);
const projectTemplate = path.resolve('./src/templates/projectTemplate.js');
if (result.errors) {
reporter.panicOnBuild('Error while running GraphQL query.');
return;
}
result.data.allMdx.nodes.forEach((node) => {
createPage({
path: `/projects/${node.frontmatter?.slug}`,
component: `${projectTemplate}?__contentFilePath=${node.internal.contentFilePath}`,
context: {
id: node.id,
},
});
console.log(node.id);
});
};
exports.onCreateNode = async ({
node,
actions,
store,
cache,
createNodeId,
reporter,
}) => {
const { createNode, createNodeField } = actions;
if (node.internal.type === 'Mdx' && node.frontmatter) {
// If dateAdded is not set, use the file creation date
if (!node.frontmatter.dateAdded) {
const stats = fs.statSync(node.internal.contentFilePath);
createNodeField({
node,
name: 'dateAdded',
value: stats.birthtime.toISOString().split('T')[0], // Format: YYYY-MM-DD
});
}
}
if (node.internal.type === 'TeamJson') {
// Only try to process photo if it exists
if (node.photo) {
const photoPath = path.resolve(
__dirname,
'src/content/people/headshots',
node.photo
);
if (fs.existsSync(photoPath)) {
let fileNode = await createFileNodeFromBuffer({
buffer: fs.readFileSync(photoPath),
store,
cache,
createNode,
createNodeId,
parentNodeId: node.id,
reporter,
});
if (fileNode) {
createNodeField({
node,
name: 'memberImage___NODE',
value: fileNode.id,
});
}
} else {
reporter.warn(
`Image not found at path: ${photoPath} for member: ${node.name}`
);
}
} else {
reporter.info(`No photo specified for team member: ${node.name}`);
}
}
};