forked from appsody/website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
82 lines (73 loc) · 2.09 KB
/
gatsby-node.js
File metadata and controls
82 lines (73 loc) · 2.09 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
const path = require(`path`)
const fetch = require('node-fetch');
const fs = require('file-system');
// Add URLs to v2 indexes to this array for them to be rendered on the website
// If the index does not need to be downloaded, place the index.yaml in src/data/indexes
// Note: this is only for developement add URLs to /ci/get-indexes.sh to affect production
const indexURLs = [
'https://github.com/appsody/stacks/releases/latest/download/stable-index.yaml',
'https://github.com/appsody/stacks/releases/latest/download/incubator-index.yaml',
'https://github.com/appsody/stacks/releases/latest/download/experimental-index.yaml'
]
const { createFilePath } = require(`gatsby-source-filesystem`)
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const slug = createFilePath({ node, getNode })
if (slug == '/overview/') {
createNodeField({
node,
name: `slug`,
value: `/docs`,
})
} else {
createNodeField({
node,
name: `slug`,
value: `/docs${slug}`,
})
}
}
}
exports.onPreInit = () => {
indexURLs.forEach(url => {
fetch(url)
.then(res => res.text())
.then(body => {
const fileName = url.split('/').reverse()[0];
fs.writeFile(`src/data/indexes/${fileName}`, body);
})
})
}
exports.createPages = ({ actions, graphql }) => {
const { createPage, createRedirect } = actions
const docTemplate = path.resolve(`src/templates/docTemplate.js`)
return graphql(`
{
allMarkdownRemark {
edges {
node {
fields {
slug
}
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors)
}
return result.data.allMarkdownRemark.edges.forEach(({ node }) => {
if (node.fields.slug == '/docs/overview/') {
node.fields.slug = '/docs'
}
createPage({
path: node.fields.slug,
component: docTemplate,
context: {
},
})
})
})
}