forked from eclipse-iofog/iofog.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
146 lines (131 loc) · 4 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
const path = require('path');
const { createFilePath } = require('gatsby-source-filesystem');
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
switch (node.internal.type) {
case 'ConfigJson': {
const fileNode = getNode(node.parent);
createNodeField({
name: 'path',
node,
value: `/${fileNode.relativeDirectory}/`
});
break;
}
case 'MarkdownRemark': {
const { relativePath } = getNode(node.parent);
createFilePath({ node, getNode });
createNodeField({
node,
name: 'slug',
value: `/${relativePath.replace('.md', '.html')}`
});
break;
}
default:
}
};
exports.createPages = ({ graphql, actions }) => {
const { createPage, createRedirect } = actions;
return new Promise((resolve, reject) => {
const docsComponent = path.resolve('src/templates/post.jsx');
const releasesComponent = path.resolve('src/templates/releases.jsx');
resolve(
graphql(
`
{
allConfigJson {
edges {
node {
version
menus {
subMenus {
entry {
relativePath
absolutePath
}
}
}
fields {
path
}
}
}
}
allMarkdownRemark {
edges {
node {
fields {
slug
}
fileAbsolutePath
}
}
}
}
`
).then(result => {
if (result.errors) {
/* eslint no-console: "off" */
console.log(result.errors);
reject(result.errors);
}
// We want to find the first docs file in the latest version so that we
// can have /docs redirect to it
const configs = result.data.allConfigJson.edges
.slice()
.sort((a, b) => b.node.version.localeCompare(a.node.version));
const latestConfig = configs[0].node;
const latestVersionBasePath = latestConfig.fields.path;
const latestDocsStartFile =
latestConfig.menus[0].subMenus[0].entry.absolutePath;
let foundLatestDocsStartFile = false;
result.data.allMarkdownRemark.edges.forEach(edge => {
const { slug } = edge.node.fields;
if (slug.startsWith('/docs/')) {
createPage({
path: slug,
component: docsComponent,
context: { slug }
});
} else if (slug === '/releases.html') {
createPage({
path: slug,
component: releasesComponent,
context: { slug }
});
} else {
throw new Error(`Unrecognized slug ${slug}`);
}
if (slug.startsWith(latestVersionBasePath)) {
createRedirect({
fromPath: `/docs/${slug.slice(latestVersionBasePath.length)}`,
isPermanent: true,
redirectInBrowser: true,
toPath: slug
});
}
// The latest version changes, so we want to do this dynamically for
// each build
if (edge.node.fileAbsolutePath === latestDocsStartFile) {
if (foundLatestDocsStartFile)
throw new Error(
`latestDocsStartFile was already found ${latestDocsStartFile}`
);
foundLatestDocsStartFile = true;
createRedirect({
fromPath: '/docs/',
isPermanent: true,
redirectInBrowser: true,
toPath: slug
});
}
});
if (!foundLatestDocsStartFile)
throw new Error(
`no redirect for /docs setup, didn't find ${latestDocsStartFile}`
);
})
);
});
};