forked from reactjs/ru.react.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreatePages.js
157 lines (137 loc) · 4.04 KB
/
createPages.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
149
150
151
152
153
154
155
156
157
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* @emails react-core
*/
'use strict';
const {resolve} = require('path');
module.exports = async ({graphql, actions}) => {
const {createPage, createRedirect} = actions;
// Used to detect and prevent duplicate redirects
const redirectToSlugMap = {};
const blogTemplate = resolve(__dirname, '../src/templates/blog.js');
const communityTemplate = resolve(__dirname, '../src/templates/community.js');
const docsTemplate = resolve(__dirname, '../src/templates/docs.js');
const tutorialTemplate = resolve(__dirname, '../src/templates/tutorial.js');
// Redirect /index.html to root.
createRedirect({
fromPath: '/index.html',
redirectInBrowser: true,
toPath: '/',
});
const allMarkdown = await graphql(
`
{
allMarkdownRemark(limit: 1000) {
edges {
node {
fields {
redirect
slug
}
}
}
}
}
`,
);
if (allMarkdown.errors) {
console.error(allMarkdown.errors);
throw Error(allMarkdown.errors);
}
allMarkdown.data.allMarkdownRemark.edges.forEach(edge => {
const slug = edge.node.fields.slug;
if (slug === 'docs/error-decoder.html') {
// No-op so far as markdown templates go.
// Error codes are managed by a page in src/pages
// (which gets created by Gatsby during a separate phase).
} else if (
slug.includes('blog/') ||
slug.includes('community/') ||
slug.includes('contributing/') ||
slug.includes('docs/') ||
slug.includes('tutorial/') ||
slug.includes('warnings/')
) {
let template;
if (slug.includes('blog/')) {
template = blogTemplate;
} else if (slug.includes('community/')) {
template = communityTemplate;
} else if (
slug.includes('contributing/') ||
slug.includes('docs/') ||
slug.includes('warnings/')
) {
template = docsTemplate;
} else if (slug.includes('tutorial/')) {
template = tutorialTemplate;
}
const createArticlePage = path =>
createPage({
path: path,
component: template,
context: {
slug,
},
});
// Register primary URL.
createArticlePage(slug);
// Register redirects as well if the markdown specifies them.
if (edge.node.fields.redirect) {
let redirect = JSON.parse(edge.node.fields.redirect);
if (!Array.isArray(redirect)) {
redirect = [redirect];
}
redirect.forEach(fromPath => {
if (redirectToSlugMap[fromPath] != null) {
console.error(
`Duplicate redirect detected from "${fromPath}" to:\n` +
`* ${redirectToSlugMap[fromPath]}\n` +
`* ${slug}\n`,
);
process.exit(1);
}
// 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({
fromPath: `/${fromPath}`,
redirectInBrowser: true,
toPath,
});
});
}
}
});
const newestBlogEntry = await graphql(
`
{
allMarkdownRemark(
limit: 1
filter: {fileAbsolutePath: {regex: "/blog/"}}
sort: {fields: [fields___date], order: DESC}
) {
edges {
node {
fields {
slug
}
}
}
}
}
`,
);
const newestBlogNode = newestBlogEntry.data.allMarkdownRemark.edges[0].node;
// Blog landing page should always show the most recent blog entry.
['/blog/', '/blog'].map(slug => {
createRedirect({
fromPath: slug,
redirectInBrowser: true,
toPath: newestBlogNode.fields.slug,
});
});
};