forked from apollographql/gatsby-theme-apollo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
234 lines (209 loc) · 5.57 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const jsYaml = require('js-yaml');
const path = require('path');
const {createFilePath} = require('gatsby-source-filesystem');
const {getVersionBasePath, getSpectrumUrl} = require('./src/utils');
async function onCreateNode({node, actions, getNode, loadNodeContent}) {
if (configPaths.includes(node.relativePath)) {
const value = await loadNodeContent(node);
actions.createNodeField({
name: 'raw',
node,
value
});
}
if (['MarkdownRemark', 'Mdx'].includes(node.internal.type)) {
let version = 'default';
let slug = createFilePath({
node,
getNode
});
const parent = getNode(node.parent);
if (parent.gitRemote___NODE) {
const gitRemote = getNode(parent.gitRemote___NODE);
version = gitRemote.sourceInstanceName;
slug = slug.replace(/^\/docs\/source/, getVersionBasePath(version));
}
actions.createNodeField({
name: 'version',
node,
value: version
});
actions.createNodeField({
name: 'slug',
node,
value: slug
});
}
}
exports.onCreateNode = onCreateNode;
function getPageFromEdge({node}) {
return node.childMarkdownRemark || node.childMdx;
}
function getSidebarContents(sidebarCategories, edges, version, contentDir) {
return Object.keys(sidebarCategories).map(key => ({
title: key === 'null' ? null : key,
pages: sidebarCategories[key]
.map(linkPath => {
const match = linkPath.match(/^\[(.+)\]\((https?:\/\/.+)\)$/);
if (match) {
return {
anchor: true,
title: match[1],
path: match[2]
};
}
const edge = edges.find(edge => {
const {relativePath} = edge.node;
const {fields} = getPageFromEdge(edge);
return (
fields.version === version &&
relativePath
.slice(0, relativePath.lastIndexOf('.'))
.replace(new RegExp(`^${contentDir}/`), '') === linkPath
);
});
if (!edge) {
return null;
}
const {frontmatter, fields} = getPageFromEdge(edge);
return {
title: frontmatter.title,
path: fields.slug
};
})
.filter(Boolean)
}));
}
const configPaths = [
'docs/gatsby-config.js', // new gatsby config
'docs/_config.yml' // old hexo config
];
function getVersionSidebarCategories(gatsbyConfig, hexoConfig) {
if (gatsbyConfig) {
const trimmed = gatsbyConfig.slice(
gatsbyConfig.indexOf('sidebarCategories')
);
const json = trimmed
.slice(0, trimmed.indexOf('}'))
// wrap object keys in double quotes
.replace(/['"]?(\w[\w\s]+)['"]?:/g, '"$1":')
// replace single-quoted array values with double quoted ones
.replace(/'([\w-/.]+)'/g, '"$1"')
// remove trailing commas
.trim()
.replace(/,\s*\]/g, ']')
.replace(/,\s*$/, '');
const {sidebarCategories} = JSON.parse(`{${json}}}`);
return sidebarCategories;
}
const {sidebar_categories} = jsYaml.load(hexoConfig);
return sidebar_categories;
}
const pageFragment = `
internal {
type
}
frontmatter {
title
}
fields {
slug
version
}
`;
exports.createPages = async ({actions, graphql}, options) => {
const {data} = await graphql(`
{
allFile(filter: {extension: {in: ["md", "mdx"]}}) {
edges {
node {
id
relativePath
childMarkdownRemark {
${pageFragment}
}
childMdx {
${pageFragment}
}
}
}
}
}
`);
const {
contentDir = 'docs/source',
githubRepo,
sidebarCategories,
spectrumHandle,
spectrumPath,
typescriptApiBox,
versions = {},
defaultVersion,
baseUrl
} = options;
const {edges} = data.allFile;
const sidebarContents = {
default: getSidebarContents(sidebarCategories, edges, 'default', contentDir)
};
const versionKeys = [];
for (const version in versions) {
versionKeys.push(version);
// grab the old config files for each older version
const configs = await Promise.all(
configPaths.map(async configPath => {
const response = await graphql(`
{
file(
relativePath: {eq: "${configPath}"}
gitRemote: {sourceInstanceName: {eq: "${version}"}}
) {
fields {
raw
}
}
}
`);
const {file} = response.data;
return file && file.fields.raw;
})
);
sidebarContents[version] = getSidebarContents(
getVersionSidebarCategories(...configs),
edges,
version,
contentDir
);
}
const [owner, repo] = githubRepo.split('/');
const template = require.resolve('./src/components/template');
edges.forEach(edge => {
const {id, relativePath} = edge.node;
const {fields} = getPageFromEdge(edge);
actions.createPage({
path: fields.slug,
component: template,
context: {
id,
sidebarContents: sidebarContents[fields.version],
githubUrl:
'https://' +
path.join(
'github.com',
owner,
repo,
'tree',
'master',
contentDir,
relativePath
),
spectrumUrl:
spectrumHandle &&
getSpectrumUrl(spectrumHandle) + (spectrumPath || `/${repo}`),
typescriptApiBox,
versions: versionKeys, // only need to send version labels to client
defaultVersion,
baseUrl
}
});
});
};