-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
100 lines (85 loc) · 2.61 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
'use strict';
const path = require('path');
exports.modifyWebpackConfig = ({ config, stage }) => {
config._config.node.fs = 'empty'
if (stage === 'build-html') {
config.loader('null', {
test: /react-ace/,
loader: 'null-loader'
})
}
}
exports.createPages = ({ boundActionCreators, graphql }) => {
const { createPage, createRedirect } = boundActionCreators;
const documentationTemplate = path.resolve(`src/templates/documentation.js`);
const redirectTemplate = path.resolve(`src/templates/redirect.js`);
return graphql(`{
allMarkdownRemark(
limit: 1000,
sort: { order:ASC, fields: fileAbsolutePath },
filter: { fileAbsolutePath: { regex: "/\/docs\//" } }
) {
edges {
node {
fileAbsolutePath
html
headings {
value
depth
}
frontmatter {
title
}
}
}
}
}`)
.then(result => {
if (result.errors) {
// tests
// issues
// console.log(result.errors)
// return Promise.reject(result.errors);
}
let nav = [];
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
if (node.fileAbsolutePath.indexOf('index') > 0) {
const parent = { title: node.frontmatter.title, children: [], redirectFrom: getDocPath(node) };
nav.push(parent);
}
else {
const parent = nav[nav.length - 1];
if (!parent.path) {
parent.path = getDocPath(node);
}
parent.children.push({ title: node.frontmatter.title, path: getDocPath(node) });
}
});
result.data.allMarkdownRemark.edges
.forEach(({ node }, i) => {
if (node.fileAbsolutePath.indexOf('index') > 0) {
createPage({
path: getDocPath(node),
component: redirectTemplate,
context: { to: getDocPath(result.data.allMarkdownRemark.edges[i+1].node) }
})
}
else {
createPage({
path: getDocPath(node),
component: documentationTemplate,
context: { page: node, nav }
})
}
});
});
}
function getDocPath({ fileAbsolutePath }) {
const ext = path.extname(fileAbsolutePath);
const file = stripOrderingNumbers(path.basename(fileAbsolutePath, ext));
const dir = stripOrderingNumbers(path.dirname(fileAbsolutePath).split(path.sep).pop());
return `/docs/${dir}${file === 'index' ? '' : `/${file}`}`;
}
function stripOrderingNumbers(str) {
return str.replace(/^(\d+-)/, '');
}