forked from carbon-design-system/carbon-website-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
130 lines (124 loc) · 3.37 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
const path = require('path');
const { createFilePath } = require(`gatsby-source-filesystem`);
// 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 { createRedirect, createPage } = actions;
return new Promise((resolve, reject) => {
graphql(`
{
allMarkdownRemark {
edges {
node {
fields {
slug
currentPage
}
frontmatter {
tabs
}
}
}
}
}
`).then(result => {
result.data.allMarkdownRemark.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().replace(' ', '-');
currentPath = currentPath.slice(0, currentPath.lastIndexOf(current));
createRedirect({
fromPath: `${currentPath}`,
redirectInBrowser: true,
toPath: `${currentPath}${current}`,
});
createRedirect({
fromPath: `${currentPath.slice(0, currentPath.length - 1)}`,
redirectInBrowser: true,
toPath: `${currentPath}${current}`,
});
}
resolve();
});
});
});
};
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,
},
},
],
},
});
};