-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
103 lines (88 loc) · 2.66 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
const path = require("path");
const { createFilePath } = require('gatsby-source-filesystem');
const { graphql } = require('gatsby');
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type MarkdownRemarkFrontmatter {
contentTop: String
customPath: String
contentType: String
}
type MarkdownRemark implements Node {
frontmatter: MarkdownRemarkFrontmatter
}
`;
createTypes(typeDefs);
};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === 'MarkdownRemark') {
const slug = createFilePath({ node, getNode, basePath: 'content' });
const { frontmatter } = node;
createNodeField({
node,
name: 'customPath',
value: frontmatter.customPath || ''
});
createNodeField({
node,
name: 'contentType',
value: frontmatter.contentType || ''
});
}
};
exports.onCreatePage = ({ graphql, page, actions }) => {
const { createPage, deletePage } = actions;
delete page.context.path;
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
// Query the necessary data using GraphQL
const result = await graphql(`
query {
allMarkdownRemark {
edges {
node {
rawMarkdownBody
frontmatter {
contentTop
}
fields {
slug
langKey
customPath
contentType
}
}
}
}
}
`);
// Create pages dynamically based on the queried data
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
const isAllowedContentType = ["home", "page"].includes(node.fields.contentType);
const isBodyNotEmpty = node.rawMarkdownBody && node.rawMarkdownBody.trim() !== "";
const isContentTopNotEmpty = !!node.frontmatter.contentTop && node.frontmatter.contentTop.trim() !== "";
// Skip creating the page if the content type is not allowed or if the body or contentTop is empty
if (!isAllowedContentType || (!isBodyNotEmpty && !isContentTopNotEmpty)) {
return;
}
const pagePath =
node.fields.langKey && node.fields.customPath
? `/${node.fields.langKey}/${node.fields.customPath}`
: node.fields.slug;
let template = path.resolve('./src/templates/pages.tsx');
if (node.fields.contentType === 'home') {
template = path.resolve('./src/pages/index.tsx');
}
createPage({
path: pagePath,
component: template,
context: {
slug: pagePath,
langKey: node.fields.langKey,
},
});
});
};