forked from build-trust/ockam-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
101 lines (89 loc) · 2.5 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
const path = require("path");
const { createFilePath } = require(`gatsby-source-filesystem`);
const { startCase } = require("lodash");
const getDependedRepos = require('./scripts/get-depended-repos');
const learnIndices = require('./scripts/get-algolia-learn-indices');
const isNodeBlogPage = (node) => node.fields.slug && node.fields.slug.split("/")[1] === 'blog';
const mapReadmeSlug = (slug) => {
return slug.replace(/\/readme/gi, '')
};
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const results = await graphql(`
{
allMdx {
edges {
node {
fields {
id
slug
}
tableOfContents
}
}
}
}
`);
if (results.errors) {
console.log(results.errors); // eslint-disable-line no-console
}
const dependedRepos = await getDependedRepos();
results.data.allMdx.edges.forEach(({ node }) => {
const isBlog = isNodeBlogPage(node);
const page = {
path: node.fields.slug ? node.fields.slug : "/",
component: isBlog ? path.resolve("./src/templates/BlogTemplate.js") : path.resolve("./src/templates/LearnTemplate.js"),
context: {
id: node.fields.id,
pageType: isBlog ? 'blog' : 'doc',
dependedRepos,
},
};
createPage(page);
});
};
exports.onCreatePage = ({ page, actions }) => {
const { createPage } = actions;
const isRootBlog = page.path && page.path.split("/")[1] === 'blog';
const algoliaIndexes = {
learn: learnIndices,
};
createPage({
...page,
context: {
...page.context,
algoliaIndexes,
...(isRootBlog && { pageType: `blog`}),
},
});
};
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, "src"), "node_modules"],
alias: { $components: path.resolve(__dirname, "src/components") },
},
});
};
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `Mdx`) {
const parent = getNode(node.parent);
const value = createFilePath({ node, getNode, basePath: `content` });
createNodeField({
name: `slug`,
node,
value: mapReadmeSlug(value),
});
createNodeField({
name: "id",
node,
value: node.id,
});
createNodeField({
name: "title",
node,
value: node.frontmatter.title || startCase(parent.name),
});
}
};