This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gatsby-node.js
executable file
·105 lines (98 loc) · 2.47 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
const path = require('path');
const { createFilePath } = require(`gatsby-source-filesystem`);
const { copy } = require('fs-extra');
// Method that creates nodes based on the file system that we can use in our templates
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
// If the node type (file) is a markdown file
if (node.internal.type === 'Mdx') {
const dir = path.resolve(__dirname, '');
const fileNode = getNode(node.parent);
const slug = createFilePath({
node,
getNode,
basePath: `content`,
trailingSlash: false,
});
const currentPage = slug.split('/').pop();
// example
createNodeField({
node,
name: `slug`,
value: slug,
});
// example: react
createNodeField({
node,
name: `currentPage`,
value: currentPage,
});
}
};
// Method that creates the pages for our website
exports.createPages = ({ actions, graphql }) => {
const { createRedirect, createPage } = actions;
return graphql(`
{
allMdx {
edges {
node {
fields {
slug
currentPage
}
}
}
}
}
`).then(result => {
result.data.allMdx.edges.forEach(({ node }) => {
const slug = node.fields.slug;
const currentPage = node.fields.currentPage;
let currentPath = slug.slice(0, slug.lastIndexOf(currentPage));
createPage({
path: currentPath,
component: path.resolve(`./src/templates/page.js`),
context: {
slug,
currentPage,
},
});
});
});
};
exports.onCreateWebpackConfig = ({ actions, getConfig, stage, loaders }) => {
actions.setWebpackConfig({
module: {
rules: [
{
test: /\.js/,
include: path.dirname(require.resolve('ansi-regex')),
use: [loaders.js()],
},
{
test: /\.md$/,
loaders: ['html-loader', 'markdown-loader'],
},
{
test: /\.html$/,
loader: 'html-loader',
options: {
minimize: false,
},
},
],
},
});
};
exports.onPostBuild = () => {
let src;
try {
src = path.resolve(path.dirname(require.resolve('carbon-icons')), 'svg');
} catch (err) {
console.error('Error locating the icons directory', err.stack);
return;
}
const dst = path.resolve(__dirname, 'public/assets/icons');
return copy(src, dst);
};