-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
110 lines (103 loc) · 2.62 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
const path = require(`path`);
const {createFilePath} = require(`gatsby-source-filesystem`);
exports.onCreateNode = ({node, getNode, actions}) => {
const {createNodeField} = actions;
// General MDX pages
if (node.internal.type === `Mdx` && node.frontmatter.category === `journal`) {
const slug = createFilePath({node, getNode, basePath: `content`});
createNodeField({
name: `slug`,
node,
value: slug,
});
}
// Episode MDX pages
else if (
node.internal.type === `Mdx` &&
node.frontmatter.category === `episode`
) {
const slug = createFilePath({
node,
getNode,
basePath: `content`,
});
createNodeField({
name: `slug`,
node,
value: slug,
});
}
};
exports.createPages = async ({graphql, actions, reporter}) => {
// Destructure the createPage function from the actions object
const {createPage} = actions;
const pages = await graphql(`
query {
allMdx(filter: {frontmatter: {category: {eq: "journal"}}}) {
edges {
node {
id
fields {
slug
}
frontmatter {
path
}
}
}
}
}
`);
if (pages.errors) {
reporter.panicOnBuild(
'🚨 ERROR: Loading "createPages [default pages]" query'
);
}
pages.data.allMdx.edges.forEach(({node}) => {
createPage({
path: node.frontmatter.path || node.fields.slug,
component: path.resolve(`./src/templates/journal.js`),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
slug: node.fields.slug,
},
});
});
const episodes = await graphql(`
query {
allMdx(filter: {frontmatter: {category: {eq: "episode"}}}) {
edges {
node {
id
fields {
slug
}
frontmatter {
path
}
}
}
}
}
`);
if (episodes.errors) {
reporter.panicOnBuild(
'🚨 ERROR: Loading "createPages [episodes pages]" query'
);
}
// Create episode pages.
// you'll call `createPage` for each episode
episodes.data.allMdx.edges.forEach(({node}, index) => {
createPage({
// This is the slug you created before
// (or `node.frontmatter.slug`)
path: node.frontmatter.path || node.fields.slug,
// This component will wrap our MDX content
component: path.resolve(`./src/templates/episodes.js`),
// You can use the values in this context in
// our page layout component
context: {id: node.id},
});
});
};