-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
93 lines (85 loc) · 2.38 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
const { isFuture } = require("date-fns");
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
exports.createSchemaCustomization = ({ actions, schema }) => {
actions.createTypes([
schema.buildObjectType({
name: "SanityPost",
interfaces: ["Node"],
fields: {
isPublished: {
type: "Boolean!",
resolve: source => new Date(source.publishedAt) <= new Date()
}
}
})
]);
};
async function createLandingPages(pathPrefix = "/", graphql, actions, reporter) {
const { createPage } = actions;
const result = await graphql(`
{
allSanityRoute(filter: { slug: { current: { ne: null } }, page: { id: { ne: null } } }) {
edges {
node {
id
slug {
current
}
}
}
}
}
`);
if (result.errors) throw result.errors;
const routeEdges = (result.data.allSanityRoute || {}).edges || [];
routeEdges.forEach(edge => {
const { id, slug = {} } = edge.node;
const path = [pathPrefix, slug.current, "/"].join("");
reporter.info(`Creating landing page: ${path}`);
createPage({
path,
component: require.resolve("./src/templates/page.js"),
context: { id }
});
});
}
async function createBlogPostPages(pathPrefix = "/blog", graphql, actions, reporter) {
const { createPage } = actions;
const result = await graphql(`
{
allSanityPost(filter: { slug: { current: { ne: null } }, isPublished: { eq: true } }) {
edges {
node {
id
publishedAt
slug {
current
}
}
}
}
}
`);
if (result.errors) throw result.errors;
const postEdges = (result.data.allSanityPost || {}).edges || [];
postEdges
.filter(edge => !isFuture(edge.node.publishedAt))
.forEach(edge => {
const { id, slug = {} } = edge.node;
const path = `${pathPrefix}/${slug.current}/`;
reporter.info(`Creating blog post page: ${path}`);
createPage({
path,
component: require.resolve("./src/templates/blog-post.js"),
context: { id }
});
});
}
exports.createPages = async ({ graphql, actions, reporter }) => {
await createLandingPages("/", graphql, actions, reporter);
await createBlogPostPages("/blog", graphql, actions, reporter);
};