-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
121 lines (110 loc) · 3.08 KB
/
gatsby-node.js
File metadata and controls
121 lines (110 loc) · 3.08 KB
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
const fs = require(`fs`);
const path = require(`path`);
const { groupBy, sortBy } = require(`lodash`);
const { createFilePath } = require(`gatsby-source-filesystem`);
const yaml = require("js-yaml");
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type SiteSiteMetadataCourses implements Node {
id: String
title: String
squareImage: String
summary: String
}
`;
createTypes(typeDefs);
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const ymlDoc = yaml.load(fs.readFileSync(path.join(__dirname, "content", "courses.yml"), "utf-8"));
ymlDoc.forEach((element) => {
console.log(`Creating page: ${element.id}`);
createPage({
path: `/course/${element.id}`,
component: require.resolve("./src/templates/course-page.tsx"),
context: {
title: element.title,
id: element.id,
disabled: element.disabled,
summary: element.summary,
squareImage: element.squareImage,
},
});
});
const blogPost = path.resolve(`./src/templates/blog-post.tsx`);
const result = await graphql(`
{
allMarkdownRemark(sort: { frontmatter: { order: ASC } }, limit: 1000) {
edges {
node {
fields {
slug
}
frontmatter {
title
order
course
isExercise
}
}
}
}
}
`);
if (result.errors) {
throw result.errors;
}
// Create blog posts pages.
const posts = result.data.allMarkdownRemark.edges;
const sorted_posts = groupBy(
sortBy(
posts.map((post) => ({
path: post.node.fields.slug,
component: blogPost,
node: post.node,
context: {
slug: post.node.fields.slug,
title: post.node.frontmatter.title,
course: post.node.frontmatter.course,
order: post.node.frontmatter.order,
},
})),
["context.course", "context.order"]
),
"context.course"
);
Object.keys(sorted_posts).forEach((courseName) => {
const courseArticles = sorted_posts[courseName];
courseArticles.forEach((postData, index) => {
const next = index === courseArticles.length - 1 ? null : courseArticles[index + 1].node;
const previous = index === 0 ? null : courseArticles[index - 1].node;
const pageArg = {
...postData,
context: {
...postData.context,
previous: previous,
next: next,
},
};
createPage(pageArg);
});
});
};
/**
*
* @type {import('gatsby').GatsbyNode<any, any>['onCreateNode']}
*/
exports.onCreateNode = ({ node, actions, getNode }) => {
// console.log(`Node created of type "${node.internal.type}"`);
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const contentPath = createFilePath({ node, getNode }).substr(1);
const value = `/course/${contentPath}`;
createNodeField({
name: `slug`,
node,
value,
});
}
};