generated from tweag/project
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gatsby-node.js
130 lines (116 loc) · 4.52 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const path = require('path')
exports.onCreateWebpackConfig = ({ _stage, actions, _loaders }) => {
actions.setWebpackConfig({
experiments: {
// This was necessary to have the Nickel WASM REPL work with Webpack
asyncWebAssembly: true,
},
})
};
/* Create redirects from `/user-manual/xxx.md` to `/user-manual/xxx`,
to make inter-manual links work both as stand-alone Markdown files
and when embedded in the website. Also create a redirect from `/user-manual(/?)`
to the introduction.
*/
exports.createPages = async ({ actions, graphql }) => {
const result = await graphql(`
query {
allMarkdownRemark {
edges {
node {
frontmatter {
slug
}
}
}
}
}`
);
result.data.allMarkdownRemark.edges.forEach(edge => {
const page = edge.node.frontmatter.slug;
let page_path = `/user-manual/${page}`;
actions.createRedirect({
fromPath: `${page_path}.md`,
toPath: page_path,
redirectInBrowser: true,
isPermanent: true
});
});
let redirectToIntro = fromPath => (
actions.createRedirect({
fromPath,
toPath: `/user-manual/introduction`,
redirectInBrowser: true,
isPermanent: true,
})
);
redirectToIntro("/user-manual/");
redirectToIntro("/user-manual");
let redirectToStdlibStd = fromPath => (
actions.createRedirect({
fromPath,
toPath: `/stdlib/std`,
redirectInBrowser: true,
isPermanent: true,
})
);
redirectToStdlibStd("/stdlib/");
redirectToStdlibStd("/stdlib");
};
// Make a StdlibSection node in the format expected by the page generation code
// We pass the original content as JSON so we can query the entire thing at once instead of havin gatsby integrate it into the GraphQL schema
const makeStdlibSection = ({ actions, createNodeId, createContentDigest, node, slug, name, content }) => {
newNode = {
id: createNodeId(`${node.id} >>> Nickel Stdlib Doc ${slug}`),
parent: node.id,
slug,
name,
internal: {
contentDigest: createContentDigest(content),
content: JSON.stringify(content),
type: "StdlibSection"
}
};
actions.createNode(newNode);
actions.createParentChildLink({ parent: node, child: newNode })
}
exports.onCreateNode = ({ node, getNode, createNodeId, createContentDigest, actions }) => {
// We make a new node type "UserManualMarkdown" to make the filesystem query in `src/pages/user-manual/{UserManualMarkdown.slug}.js` more specific
if (node.internal.type === "MarkdownRemark" && getNode(node.parent).sourceInstanceName === "user-manual") {
newNode = {
id: createNodeId(`Nickel User Manual ${node.id}`),
parent: node.id,
slug: node.frontmatter.slug,
internal: {
contentDigest: node.internal.contentDigest,
type: "UserManualMarkdown",
},
};
actions.createNode(newNode);
actions.createParentChildLink({ parent: node, child: newNode })
}
// Preprocess the JSON stdlib documentation data that we get from Nickel into a format that makes it easier to handle
if (node.internal.type === "NickelStdlibDocJson") {
const toplevelName = getNode(node.parent).name;
docsContent = JSON.parse(getNode(node.parent).internal.content);
// Since we don't want to have every module displayed under the `std` namespace on the website, we split out those top-level entries which have subfields.
toplevelFields = Object.fromEntries(Object.entries(docsContent).filter(([key, value]) => !value.fields));
makeStdlibSection({
actions, createNodeId, createContentDigest,
node,
slug: toplevelName,
name: toplevelName,
content: toplevelFields,
})
// What's left are the stdlib functions living in the top-level `std` namespace
Object.entries(docsContent).filter(([key, value]) => !!value.fields).forEach(([slug, value]) => {
makeStdlibSection({
actions, createNodeId, createContentDigest,
node,
slug: `${toplevelName}-${slug}`,
name: `${toplevelName}.${slug}`,
content: value.fields,
})
});
}
}