-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgatsby-node.js
More file actions
178 lines (158 loc) · 4.57 KB
/
gatsby-node.js
File metadata and controls
178 lines (158 loc) · 4.57 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
'use strict';
const {resolve, dirname, basename} = require('path');
const fs = require('fs');
const yaml = require('js-yaml');
exports.createPages = async ({graphql, boundActionCreators}) => {
const {createPage, createRedirect} = boundActionCreators;
// Used to detect and prevent duplicate redirects
const redirectToSlugMap = {};
const template = resolve(__dirname, 'src/templates/docs.js');
// Redirect /index.html to root.
createRedirect({
fromPath: '/index.html',
redirectInBrowser: true,
toPath: '/',
});
const allMarkdown = await graphql(
`
{
allMarkdownRemark(limit: 1000) {
edges {
node {
fields {
redirectFrom
slug
}
}
}
}
}
`
);
if (allMarkdown.errors) {
console.error(allMarkdown.errors);
throw new Error(allMarkdown.errors);
}
allMarkdown.data.allMarkdownRemark.edges.forEach(edge => {
const slug = edge.node.fields.slug;
const createArticlePage = path =>
createPage({
path,
component: template,
context: {
slug,
},
});
// Register primary URL.
createArticlePage(slug);
// Register redirects as well if the markdown specifies them.
if (edge.node.fields.redirectFrom) {
let redirect = JSON.parse(edge.node.fields.redirectFrom);
if (!Array.isArray(redirect)) {
redirect = [redirect];
}
redirect.forEach(each => {
let fromPath;
let redirectInBrowser;
if (typeof each === 'string') {
fromPath = each;
// redirect in browser if not specified
redirectInBrowser = true;
} else {
fromPath = each.path;
redirectInBrowser = each.browser === undefined ? true : each.browser;
}
// Ensure fromPath has a leading slash
fromPath = fromPath.startsWith('/') ? fromPath : `/${fromPath}`;
// eslint-disable-next-line no-eq-null, eqeqeq
if (redirectToSlugMap[fromPath] != null) {
console.error(
`Duplicate redirect detected from "${fromPath}" to:\n` +
`* ${redirectToSlugMap[fromPath]}\n` +
`* ${slug}\n`
);
throw new Error();
}
// A leading "/" is required for redirects to work,
// But multiple leading "/" will break redirects.
// For more context see github.com/reactjs/reactjs.org/pull/194
const toPath = slug.startsWith('/') ? slug : `/${slug}`;
redirectToSlugMap[fromPath] = slug;
createRedirect({
toPath,
fromPath,
redirectInBrowser,
});
});
}
});
};
exports.onCreateNode = ({node, boundActionCreators, getNode}) => {
const {createNodeField} = boundActionCreators;
switch (node.internal.type) {
case 'MarkdownRemark': {
let {slug} = node.frontmatter;
const redirectFrom = node.frontmatter.redirect_from;
const {relativePath} = getNode(node.parent);
const dir = dirname(relativePath);
if (!slug) {
// content/foo/index.md => /foo/
if (basename(relativePath, '.md') === 'index') {
slug = `/${dir}/`;
} else {
// content/foo/bar.md => /foo/bar/
slug = `/${relativePath.replace('.md', '')}`;
}
}
let sectionList;
const navPath = resolve('content', dir, 'nav.yml');
if (fs.existsSync(navPath)) {
sectionList = yaml.safeLoad(fs.readFileSync(navPath));
sectionList.forEach(list => {
list.items.forEach(item => {
if (item.id === 'index') {
item.slug = `/${dir}/`;
} else {
item.slug = `/${dir}/${item.id}`;
}
});
});
} else {
sectionList = [];
}
createNodeField({
node,
name: 'sectionList',
value: JSON.stringify(sectionList),
});
// Used to generate URLs
createNodeField({
node,
name: 'slug',
value: slug,
});
// Used to generate Github edit links
createNodeField({
node,
name: 'path',
value: relativePath,
});
// Used by createPages() above to register redirects.
createNodeField({
node,
name: 'redirectFrom',
value: redirectFrom ? JSON.stringify(redirectFrom) : '',
});
// Used to generate sidebars
createNodeField({
node,
name: 'dir',
value: dir,
});
break;
}
default: {
break;
}
}
};