forked from viva-la-vita/wiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
80 lines (72 loc) · 1.72 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
const axios = require('axios');
const { resolve } = require('path');
require('dotenv').config({
path: `.env`,
});
exports.sourceNodes = async ({
actions: {createNode},
createNodeId,
createContentDigest
}) => {
const { data } = await axios.get(`/navigation/render/1`, {
baseURL: 'https://api.viva-la-vita.org/api',
headers: {
'Authorization': `Bearer ${process.env.STRAPI_TOKEN}`,
},
params: {
type: 'TREE'
}
});
function preprocess(object) {
return {
sid: object.related.id,
path: object.path,
title: object.related.title,
items: object.items.map(preprocess)
};
}
function flatten(object) {
return [{
sid: object.sid,
path: object.path,
title: object.title
}].concat(...object.items.map(flatten));
}
const seriesList = data.map(preprocess);
const indices = [].concat(...seriesList.map(flatten));
seriesList.map(series => createNode({
...series,
id: createNodeId(`Series-${series.path}`),
internal: {
type: `StrapiSeries`,
contentDigest: createContentDigest(series)
}
}));
indices.map(index => createNode({
...index,
id: createNodeId(index.path),
internal: {
type: `StrapiIndex`,
contentDigest: createContentDigest(index)
}
}));
};
exports.createPages = async ({ actions, graphql }) => {
const { data } = await graphql(`
query Indices {
allStrapiIndex {
nodes {
sid
path
}
}
}
`);
data.allStrapiIndex.nodes.forEach(({ sid, path }) => {
actions.createPage({
path: path,
component: resolve("./src/templates/article.js"),
context: { sid: sid, key: `/${path.split('/')[1]}` },
});
});
}