forked from diegonvs/gatsby-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createPages.js
82 lines (69 loc) · 1.67 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
'use strict';
const componentWithMDXScope = require('gatsby-mdx/component-with-mdx-scope');
const path = require('path');
const createPages = (actions, edges) => {
const {createPage, createRedirect} = actions;
edges.forEach(({node}, index) => {
const {slug, redirect, mainPage} = node.fields;
const templateKey = slug.split('/')[0];
if (redirect || mainPage) {
const slugWithBar = slug.startsWith('/') ? slug : `/${slug}`;
const fromPath = slugWithBar.endsWith('index.html') ? slugWithBar.replace('index.html', '') : slugWithBar;
const toPath = mainPage ? slugWithBar : redirect;
createRedirect({
fromPath,
isPermanent: true,
redirectInBrowser: true,
toPath: toPath,
});
}
if (!redirect) {
let previous = index === 0 ? false : edges[index - 1].node;
let next = index === edges.length - 1 ? false : edges[index + 1].node;
createPage({
path: slug,
component: componentWithMDXScope(
path.resolve(__dirname, `../src/templates/${templateKey}.js`),
node.code.scope,
__dirname,
),
context: {
slug,
previous,
next,
},
});
}
});
};
module.exports = async ({actions, graphql}) => {
actions.createRedirect({
fromPath: '/index.html',
redirectInBrowser: true,
toPath: '/',
});
return graphql(`
query {
allMdx(sort: {order:ASC, fields: frontmatter___stepNumber}) {
edges {
node {
fields {
redirect
slug
mainPage
}
code {
scope
}
}
}
}
}
`).then(({data, errors}) => {
if (errors) {
return Promise.reject(errors);
}
const {edges} = data.allMdx;
createPages(actions, edges);
});
};