-
Notifications
You must be signed in to change notification settings - Fork 185
/
gatsby-node.js
131 lines (117 loc) · 3.72 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
131
const path = require("path");
const cheerio = require("cheerio");
const { createFilePath } = require("gatsby-source-filesystem");
const stackshare = require("./src/utils/stackshare");
const github = require("./src/utils/github");
// to support relative paths in sass files
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, "src"), "node_modules"],
},
})
}
exports.onCreateNode = async ({ node,
actions,
getNode,
loadNodeContent }) => {
const { createNodeField } = actions
if (node.internal.type !== `Mdx`) {
return
}
// create a queryable sourceName field
const parent = getNode(node.parent);
if (parent.internal.type === "File") {
createNodeField({
name: `sourceName`,
node,
value: parent.sourceInstanceName
});
}
// set the slug b/c outside /src/pages
// https://gatsby-mdx.netlify.com/guides/programmatically-creating-pages
const slugValue = createFilePath({ node, getNode });
createNodeField({
name: "slug",
node,
value: `${parent.sourceInstanceName === `content-docs` ? `/docs` : ``}${slugValue}`
});
// only process front matter for stacks
if (parent.sourceInstanceName !== `content-stacks`) {
return
}
const contributors = node.frontmatter.contributors;
if (contributors) {
const contributorsLoaded = await Promise.all(contributors.map(github.getGitHubUser)).then(users => users.filter(user => user));
createNodeField({
name: "contributors",
node,
value: contributorsLoaded
});
}
// add a field for the list of tools used in the mdx
const nodeContent = await loadNodeContent(node);
const githubs = (nodeContent.match(/<GitHub [^>]+>/g) || []).map((toolTag) => {
const nameWithOwner = (cheerio.load(toolTag))("GitHub").attr('name');
const [owner, name] = nameWithOwner.split('/');
return { owner, name };
});
const githubsLoaded = await Promise.all(githubs.map(github.getGitHubTool)).then(tools => tools.filter(tool => tool));
createNodeField({
name: "gitHubTools",
node,
value: githubsLoaded
});
const stackshares = (nodeContent.match(/<StackShare [^>]+>/g) || []).map((toolTag) => {
const name = (cheerio.load(toolTag))("StackShare").attr('name');
const url = `https://stackshare.io/${name}`;
return { name, url };
});
// fetch the data from stackshare for each tool
// filter out any tools that aren't found
const stacksharesLoaded = await Promise.all(stackshares.map(stackshare.getStackShareTool)).then(tools => tools.filter(tool => tool && tool.name));
createNodeField({
name: "stackShareTools",
node,
value: stacksharesLoaded
});
};
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return new Promise((resolve, reject) => {
resolve(
graphql(
`
{
allMdx(filter: { fields: { sourceName: { in: ["content", "content-docs", "content-stacks"] } } }) {
edges {
node {
id
fields {
slug
sourceName
}
}
}
}
}
`
).then(result => {
if (result.errors) {
console.error(result.errors);
reject(result.errors);
}
result.data.allMdx.edges.forEach(({ node }) => {
createPage({
path: node.fields.slug,
component: path.resolve(`./src/components/pages/${node.fields.sourceName}-page.js`),
context: { id: node.id }
});
});
}).catch(err => {
console.error("Building pages failed");
console.error(err);
})
);
});
};