This repository has been archived by the owner on Apr 24, 2019. It is now read-only.
forked from carbon-design-system/carbon-website-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
executable file
·143 lines (136 loc) · 3.63 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
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 {
code {
scope
}
fields {
slug
currentPage
}
frontmatter {
tabs
}
}
}
}
}
`).then(result => {
result.data.allMdx.edges.forEach(({ node }) => {
const slug = node.fields.slug;
const currentPage = node.fields.currentPage;
const tabs = node.frontmatter.tabs === null ? [] : node.frontmatter.tabs;
let currentPath =
node.frontmatter.tabs === null
? slug.slice(0, slug.lastIndexOf(currentPage))
: slug;
createPage({
path: currentPath,
component: path.resolve(`./src/templates/page.js`),
context: {
slug,
currentPage,
},
});
if (tabs.length > 1) {
const current = tabs[0]
.toLowerCase()
.split(' ')
.join('-');
const lastIndex = currentPath.lastIndexOf(current);
if (lastIndex >= 0) {
currentPath = currentPath.slice(0, currentPath.lastIndexOf(current));
createPage({
path: currentPath,
component: path.resolve(`./src/templates/page.js`),
context: {
slug,
currentPage: `${currentPath}${current}`,
},
});
createRedirect({
fromPath: `${currentPath}`,
redirectInBrowser: true,
toPath: `${currentPath}${current}`,
});
createRedirect({
fromPath: `${currentPath.slice(0, currentPath.length - 1)}`,
redirectInBrowser: true,
toPath: `${currentPath}${current}`,
});
}
}
});
});
};
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);
};