-
Notifications
You must be signed in to change notification settings - Fork 20
/
gatsby-node.js
148 lines (136 loc) · 3.48 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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 === 'MarkdownRemark') {
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 { createPage, createRedirect } = actions;
createRedirect({
fromPath: '/practices.shtml',
toPath: 'http://www.ibm.com/design/practices/',
isPermanent: true,
});
createRedirect({
fromPath: '/studio.shtml',
toPath: 'http://www.ibm.com/design/teams/',
isPermanent: true,
});
createRedirect({
fromPath: '/work.shtml',
toPath: 'http://www.ibm.com/design/impact/',
isPermanent: true,
});
createRedirect({
fromPath: '/careers.shtml',
toPath: 'http://www.ibm.com/design/teams#find-your-team/',
isPermanent: true,
});
createRedirect({
fromPath: '/social.shtml',
toPath: 'https://medium.com/design-ibm',
isPermanent: true,
});
return graphql(`
{
allMarkdownRemark {
edges {
node {
fields {
slug
currentPage
}
}
}
}
}
`).then(result => {
result.data.allMarkdownRemark.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 }) => {
const config = getConfig();
const { module } = config;
const { rules } = module;
actions.replaceWebpackConfig({
...config,
module: {
...module,
rules: [
...rules.map(item => {
const { use } = item;
if (
!use ||
use.every(({ loader }) => !/babel-loader\.js$/i.test(loader))
) {
return item;
}
return {
...item,
exclude: /(node_modules|bower-components)[\/\\](?!(ansi-regex)[\/\\]).*/,
};
}),
{
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);
};