-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
168 lines (145 loc) · 5.04 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
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
// load in the built in path module from Node.js
// note: no need to install this from something external because this is a node core module
const path = require('path')
// gatsby node configuration file
// this file is use to tap into the node api that gatsby exposes
// documentation: https://www.gatsbyjs.com/docs/reference/config-files/gatsby-node/
// function runs when a new node is created
module.exports.onCreateNode = () => {
// the arguments you can interact with are node and actions
exports.onCreateNode = ({ node, actions }) => {
// Transform the new node here and create a new node or
// create a new node field.
const { createNodeField } = actions
if (node.internal.type === 'MarkdownRemark') {
// try to get the slug
// reference: https://nodejs.org/dist/latest-v14.x/docs/api/path.html#path_path_basename_path_ext
const slug = path.basename(node.fileAbsolutePath, '.md')
// adding a bunch of '@' signs to make it easier to find in the terminal as first argument
// add slug as second argument to run the function
//console.log ('@@@@@@@@@@', slug)
//console.log(JSON.stringify(node, undefined, 4))
// gatsby createNodeField function
// reference: https://www.gatsbyjs.com/docs/reference/config-files/actions/
createNodeField ({
node,
name: 'slug',
value: slug
})
}
// makes the string look prettier in the console (with the terminal)
// note: internal": { "type": "SitePage", ... } SitePage is an internal Gatsby "type", specific for new pages
// console.log(JSON.stringify(node, undefined, 4))
}
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return new Promise((resolve, reject) => {
const blogPage = path.resolve("src/templates/blog.js");
const projectPage = path. resolve("src/templates/project.js");
resolve(
graphql(
`
{
allMarkdownRemark {
edges {
node {
frontmatter {
title
posttype
draft
}
fields {
slug
}
}
}
}
}
`
).then(result => {
if (result.errors) {
/* eslint no-console: "off" */
console.log(result.errors);
reject(result.errors);
}
result.data.allMarkdownRemark.edges.forEach(edge => {
if (edge.node.frontmatter.posttype === 'project') {
createPage({
path: `/project/${edge.node.fields.slug}`,
component: projectPage,
context: {
slug: edge.node.fields.slug,
}
});
} else if (edge.node.frontmatter.posttype === 'blog') { // blog post
createPage({
path: `/blog/${edge.node.fields.slug}`,
component: blogPage,
context: {
slug: edge.node.fields.slug,
}
});
}
});
})
);
});
};
// module.exports.createPages = async ({ graphql, actions }) => {
// const { createPage } = actions
// // resolve will create everything from the absolute path of the hard drive
// const blogTemplate = path.resolve('./src/templates/blog.js')
// const projectTemplate = path.resolve('./src/templates/project.js')
// // this functions creates a promise
// const res = await graphql(`
// query {
// allMarkdownRemark {
// edges {
// node {
// frontmatter {
// title
// posttype
// description
// draft
// }
// fields {
// slug
// }
// }
// }
// }
// }
// `)
// // iterate all blogs and run createPage function for each post
// res.data.allMarkdownRemark.edges.forEach((edge) => {
// if (edge.node.frontmatter.posttype === 'blog') {
// createPage ({
// path: `/blog${edge.node.fields.slug}`,
// component: blogTemplate,
// context: {
// slug: edge.node.fields.slug,
// }
// // component: blogTemplate,
// // path: `/blog/${edge.node.fields.slug}`,
// // context: {
// // slug: edge.node.fields.slug
// // }
// });
// } else {
// // iterate all projects and run createPage function for each post
// createPage ({
// path: `/project/${edge.node.fields.slug}`,
// component: projectTemplate ,
// context: {
// slug: edge.node.fields.slug,
// }
// // component: projectTemplate,
// // path: `/work/${edge.node.fields.slug}`,
// // context: {
// // slug: edge.node.fields.slug
// // }
// })
// }
// });
// }