This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
197 lines (169 loc) · 5.82 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const fs = require('fs');
const path = require('path');
const { kebabCase } = require('lodash');
const moment = require('moment');
const { singular } = require('pluralize');
const siteConfig = require('./data/SiteConfig');
const projectPath = path.resolve(fs.realpathSync(process.cwd()), '.');
const srcPath = path.resolve(fs.realpathSync(process.cwd()), 'src');
const { useDatesInSlugs } = siteConfig;
// Create pages of any other type.
const makePages = ({ actions, pages }) => {
const { createPage } = actions;
if (pages) {
pages.edges.forEach((edge) => {
createPage({
path: edge.node.fields.slug,
component: path.resolve(`src/templates/${edge.node.fields.component}.js`),
context: {
id: edge.node.id,
slug: edge.node.fields.slug,
},
});
});
}
};
const onCreateNode = ({ actions, node, getNode }) => {
const { createNodeField } = actions;
let slug;
if (['MarkdownRemark', 'Mdx'].includes(node.internal.type)) {
const fileNode = getNode(node.parent);
const parsedFilePath = path.parse(fileNode.relativePath);
// If this node contains date info, load the date and set it in the fields.
const dateMatch = parsedFilePath.name.match(/^(\d{4}-\d{2}-\d{2})-(.*)/);
let date;
if (dateMatch) {
date = moment.utc(dateMatch[1]);
if (!date || !date.isValid) {
// eslint-disable-next-line no-console
console.warn(`WARNING: Invalid date for ${parsedFilePath.name}`);
}
createNodeField({
node,
name: 'date',
value: date.toISOString(),
});
createNodeField({
node,
name: 'timestamp',
value: parseInt(date.format('X'), 10),
});
createNodeField({
node,
name: 'updated',
value: node.frontmatter.updated ? node.frontmatter.updated : null,
});
}
const rootFolder = parsedFilePath.dir.split('/')[0];
// "Page" is the default, fallback component if nothing else can be found.
let component = 'Page';
if (node.frontmatter && node.frontmatter.component) {
component = node.frontmatter.component;
} else if (rootFolder) {
try {
fs.statSync(`src/templates/${rootFolder}.js`);
} catch (error) {
// This means we don't have a template file that matches the name
// of the component's root folder, which is fine. We'll use the `Page`
// component default defined above.
if (error.code !== 'ENOENT') {
throw error;
}
}
component = singular(kebabCase(rootFolder));
component = `${component.charAt(0).toUpperCase()}${component.slice(1)}`;
}
createNodeField({
node,
name: 'component',
value: component,
});
// We try to create slugs automatically to reduce the amount of frontmatter
// authors need to write. Frontmatter support, however, still exists for
// overrides, if the user desires it.
//
// For pages, we use:
//
// 1. The page's path + `slug` field in frontmatter. If the `slug` field
// were set to `"hello"` in `pages/foo.md` the slug would be `/hello`.
// If it were in `pages/something/foo.md` the slug would be
// `/something/hello`.
// 2. The page's path + filename; eg. `pages/about.md` -> `/about`,
// `pages/projects/nautilus.md` -> `/projects/nautilus`.
const datePrefix = date && useDatesInSlugs ? `${dateMatch[1]}-` : '';
const fileName = date ? dateMatch[2] : parsedFilePath.name;
if (parsedFilePath.dir.match(/^pages/)) {
const pathWithoutPagesFolder = parsedFilePath.dir.replace(/^pages\/?/, '');
if (node.frontmatter && node.frontmatter.slug) {
slug = `/${pathWithoutPagesFolder}/${node.frontmatter.slug}`;
} else {
slug = `/${pathWithoutPagesFolder}/${fileName}`;
}
} else if (node.frontmatter && node.frontmatter.slug) {
slug = `/${parsedFilePath.dir}/${datePrefix}${node.frontmatter.slug}`;
} else if (parsedFilePath.name !== 'index' && parsedFilePath.dir !== '') {
slug = `/${parsedFilePath.dir}/${datePrefix}${fileName}`;
} else {
slug = `/${parsedFilePath.dir}`;
}
// Create the slug, changing `/index` to `/` and removing any double
// slashes in slugs.
createNodeField({
node,
name: 'slug',
value: slug.replace(/\/index$/, '/').replace(/\/{2,}/g, '/'),
});
// Create fields for every other frontmatter prop; this makes it easier to
// query for fields instead of needing to know what's in `node.fields` and
// what's in `node.frontmatter`.
Object.keys(node.frontmatter)
.filter((key) => {
return ['component', 'date', 'slug'].indexOf(key) === -1;
})
.forEach((key) => {
createNodeField({
node,
name: key,
value: node.frontmatter[key],
});
});
}
};
const createPages = async ({ actions, graphql }) => {
const markdownQueryResult = await graphql(`
query {
pages: allMdx(filter: { fileAbsolutePath: { regex: "//content/(?!blog|portfolio).+?/" } }) {
edges {
node {
id
fields {
component
slug
title
}
}
}
}
}
`);
if (markdownQueryResult.errors) {
// eslint-disable-next-line no-console
console.error(markdownQueryResult.errors);
throw markdownQueryResult.errors;
}
const { pages } = markdownQueryResult.data;
makePages({ actions, pages });
};
const onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
node: {
fs: 'empty',
// path: 'empty',
},
resolve: {
extensions: ['.mjs', '.jsx', '.js', '.json'],
modules: [srcPath, projectPath, 'node_modules'],
},
});
};
module.exports = { createPages, onCreateNode, onCreateWebpackConfig };