-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgatsby-node.js
More file actions
138 lines (126 loc) · 3.75 KB
/
gatsby-node.js
File metadata and controls
138 lines (126 loc) · 3.75 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
129
130
131
132
133
134
135
136
137
138
const path = require("path");
const linkSchema = require("./src/components/Link/schema");
const DocsPageTemplate = path.resolve("./src/templates/Docs/index.jsx");
// Whether or not to print verbose debug messages to stdout
const verbose = true;
const ifVerbose = func => (verbose ? func() : void 0);
const debug = (reporter, text, mode = "info") =>
ifVerbose(() =>
({
info: content => reporter.info(content),
success: content => reporter.success(content)
}[mode](text))
);
const isDefined = obj => !(obj == null);
// Define custom graphql schema to enforce rigid type structures
exports.sourceNodes = ({ actions, reporter }) => {
activity = reporter.activityTimer("implementing custom graphql schema");
activity.start();
const { createTypes } = actions;
const typeDefs = `
type Frontmatter {
links: [Link]
title: String
shortTitle: String
noTOC: Boolean
noBreadcrumb: Boolean
}
type MarkdownRemark implements Node {
frontmatter: Frontmatter
html: String
}
type Mdx implements Node {
frontmatter: Frontmatter
}
type File implements Node {
childMarkdownRemark: MarkdownRemark
childMdx: Mdx
}
`;
createTypes(linkSchema);
createTypes(typeDefs);
activity.end();
};
// Dynamically create documentation pages
exports.createPages = ({ graphql, actions, reporter }) => {
let activity = reporter.activityTimer(`loading docs pages via graphql`);
activity.start();
const { createPage } = actions;
return graphql(
`
query loadPagesQuery($limit: Int!) {
allFile(
limit: $limit
filter: {
sourceInstanceName: { eq: "docs" }
extension: { regex: "/^(?:md)|(?:mdx)$/" }
}
) {
edges {
node {
relativePath
childMarkdownRemark {
id
}
childMdx {
id
}
}
}
}
}
`,
{ limit: 1000 }
).then(result => {
if (result.errors) {
activity.end();
throw result.errors;
}
activity.end();
activity = reporter.activityTimer(`dynamically generating docs pages`);
activity.start();
// Flatmap function that tags whether a node is md or mdx while validating
// that it has content at all
const tagOrCull = ({ childMarkdownRemark: md, childMdx: mdx, ...rest }) => {
const isMd = isDefined(md);
const isMdx = isDefined(mdx);
if (isMd || isMdx) return { ...rest, isMdx, id: isMdx ? mdx.id : md.id };
else {
// Log error and cull by returning empty array
reporter.error(`node ${rest.name} has no valid md or mdx content`);
return [];
}
};
// Trims a path to be the proper local path
const trimPath = path =>
path
.replace(".mdx", "")
.replace(".md", "")
.replace("index", "")
.replace(/\/$/, "");
// Create docs pages from both md and mdx
result.data.allFile.edges
.flatMap(({ node }) => tagOrCull(node))
.forEach(({ id, relativePath, isMdx }) => {
// Create final URL as trimmed filepath
const trimmedPath = trimPath(relativePath);
createPage({
path: `/${trimmedPath}`,
component: DocsPageTemplate,
context: { id, isMdx }
});
// Log debug message
debug(reporter, `docs page @ '/${trimmedPath}' => ${id}`);
});
activity.end();
});
};
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
// Allow relative imports like "import Foo from 'components/Foo'"
resolve: {
modules: [path.resolve(__dirname, "src"), "node_modules"],
extensions: [".js", ".jsx", ".json", ".mdx"]
}
});
};