-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
63 lines (53 loc) · 1.64 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
const { createFilePath } = require(`gatsby-source-filesystem`);
const path = require("path");
exports.onCreatePage = ({ page, actions }) => {
const { createPage, deletePage } = actions;
// Disables the dev 404 page. I find it mildly annoying.
// @see https://github.com/gatsbyjs/gatsby/issues/16112
if (process.env.NODE_ENV !== 'production' && page.path === '/404/') {
// Make the 404 page match everything client side.
// This will be used as fallback if more specific pages are not found
page.matchPath = '/*';
createPage(page);
}
// NOTE(dabrady) Markdown content must be explicitly published.
if (page.context.frontmatter && !page.context.frontmatter.published) {
deletePage(page);
}
};
exports.onCreateNode = ({ node, getNode, actions }) => {
var { createNodeField } = actions;
var fileNode = getNode(node.parent);
/** Processing for Markdown and MDX */
if (
['MarkdownRemark', 'Mdx'].includes(node.internal.type)
&& fileNode.internal.type == 'File'
) {
// Generate page slug from filename if not provided in frontmatter.
createNodeField(makeSlugField(node, getNode));
// Extract and store the post excerpt, if present.
createNodeField(makeExcerptField(node));
}
};
/**** Helpers ****/
function makeSlugField(node, getNode) {
var slug;
if (node.frontmatter?.slug) {
slug = node.frontmatter.slug;
} else {
slug = createFilePath({ node, getNode });
}
return {
node,
name: 'slug',
value: slug,
};
}
function makeExcerptField(node) {
var [_, rawExcerpt] = node.body.split('{/* / */}');
return {
node,
name: 'excerpt',
value: rawExcerpt,
};
}