Repository navigation
Expand file tree
/
Copy pathgatsby-node.js
More file actions
128 lines (114 loc) · 3.63 KB
/
Copy pathgatsby-node.js
File metadata and controls
128 lines (114 loc) · 3.63 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
122
123
124
125
126
127
128
const fs = require("fs");
const path = require(`path`);
exports.createPages = async ({graphql, actions, reporter}) => {
const {createPage} = actions;
let files = fs.readdirSync("./content");
files.forEach(file => {
if (file.endsWith(".json")) {
let contents = JSON.parse(
fs.readFileSync("./content/" + file, "utf-8")
);
let page_link = "/plugin/" + contents.info.name;
createPage({
path: page_link,
component: require.resolve("./src/templates/pluginTemplate.js"),
context: {
pageContent: contents
}
});
}
});
const articleTemplatePath = path.resolve(`./src/templates/articlesTemplate.js`);
// Articles live in ./articles. The build should still succeed if there are none.
// We can safely query frontmatter once there is at least one markdown node.
const countResult = await graphql(`
{
allMarkdownRemark(limit: 1) {
totalCount
}
}
`);
if (countResult.errors) {
reporter.warn(
`Skipping article page creation: MarkdownRemark query failed. (${countResult.errors[0].message})`
);
return;
}
const total = countResult.data?.allMarkdownRemark?.totalCount || 0;
if (total === 0) {
reporter.info(`No articles found in ./articles, skipping article page creation.`);
return;
}
const result = await graphql(`
{
allMarkdownRemark(sort: { frontmatter: { date: ASC } }, limit: 500) {
nodes {
id
frontmatter {
slug
}
}
}
}
`);
if (result.errors) {
reporter.warn(
`Skipping article page creation: could not load article slugs. (${result.errors[0].message})`
);
return;
}
const posts = result.data?.allMarkdownRemark?.nodes || [];
posts
.filter(p => p?.frontmatter?.slug)
.forEach(post => {
createPage({
path: post.frontmatter.slug,
component: articleTemplatePath,
context: {
id: post.id
}
});
});
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
// Ensure MarkdownRemark.frontmatter exists even if there are 0 markdown files.
// This keeps builds succeeding when ./articles is empty.
createTypes(`
type MarkdownRemark implements Node {
frontmatter: Frontmatter
}
type Frontmatter {
slug: String
title: String
description: String
date: Date @dateformat
}
`);
};
exports.sourceNodes = ({actions, createNodeId, createContentDigest}) => {
let files = require("fs");
let examples = [];
examples = JSON.parse(files.readFileSync("./manim_examples.json", "utf8"));
examples.forEach(example => {
let visible = "";
if (example.visible === true) {
visible = "visible";
} else {
visible = "hidden";
}
const node = {
name: example.name,
code: example.code,
output: example.output,
visible: visible,
type: example.type,
id: createNodeId(`ManimExample-${example.name}`),
internal: {
type: "ManimExample",
contentDigest: createContentDigest(examples)
}
};
actions.createNode(node);
});
};