This repository was archived by the owner on Mar 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgatsby-node.js
More file actions
85 lines (71 loc) · 2.03 KB
/
gatsby-node.js
File metadata and controls
85 lines (71 loc) · 2.03 KB
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
const path = require('path');
const { createFilePath } = require('gatsby-source-filesystem');
const yaml = require('js-yaml');
const fs = require('fs');
exports.onCreateNode = ({node, getNode, actions}) => {
const {createNodeField} = actions;
if (node.internal.type === 'MarkdownRemark') {
const slug = createFilePath({node, getNode, basePath: 'pages'});
createNodeField({
node,
name: 'slug',
value: slug,
});
}
};
exports.onCreatePage = ({ page }) => {
return new Promise(resolve => {
if (page.path == '/') {
page.context = {
toc: yaml.safeLoad(fs.readFileSync(path.resolve('./content/v2/nav.yaml'), 'utf8'))
};
}
resolve();
});
};
exports.createPages = ({graphql, actions}) => {
const {createPage, createRedirect} = actions;
createRedirect({ fromPath: '/v2', toPath: '/v2/introduction', isPermanent: true });
createRedirect({ fromPath: '/v1', toPath: '/v1/introduction', isPermanent: true });
const specSections = graphql(`
{
allMarkdownRemark {
edges {
node {
frontmatter {
id
url
version
}
fields {
slug
}
}
}
}
}
`);
return new Promise((resolve, reject) => {
specSections.then(result => {
const toc = {
v1: yaml.safeLoad(fs.readFileSync(path.resolve('./content/v1/nav.yaml'), 'utf8')),
v2: yaml.safeLoad(fs.readFileSync(path.resolve('./content/v2/nav.yaml'), 'utf8')),
};
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
const { version, url, id } = node.frontmatter;
createPage({
path: url,
component: path.resolve('./src/templates/spec-section.js'),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
toc: toc[version],
version: version,
id: id,
},
});
});
resolve();
});
});
};