-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.ts
71 lines (65 loc) · 1.65 KB
/
gatsby-node.ts
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
import path from "path";
import { userGuidePath, home } from "./src/components/UserGuide/userGuidePath";
// including __typename on the ContentfulAsset is critical, for some reason
const userGuidesQuery = `
{
allContentfulList(filter: {type: {eq: "userGuide"}}) {
nodes {
name
type
items {
... on ContentfulUserGuide {
title
slug
app
body {
raw
references {
... on ContentfulAsset {
contentful_id
__typename
gatsbyImageData
}
}
}
}
}
}
}
}
`;
const guidePageComponentPath = path.resolve("src/templates/user-guides.js");
export const createPages = async ({ graphql, actions: { createPage } }) => {
const result = await graphql(userGuidesQuery);
const nodes = result.data.allContentfulList.nodes;
const guides = nodes.map(({ items }) => items).reduce((result, list) => result.concat(list), []);
const guideIndex = guides.reduce((result, guide) => {
const { title, app } = guide;
result[app][title] = guide;
return result;
}, { EMS: {}, Hospital: {} });
const firstGuides = Object.values(guideIndex)
.map((guides) => Object.values(guides)[0])
function createGuidePage(
path,
guide)
{
createPage({
path,
component: guidePageComponentPath,
context: {
guide,
guideIndex
}
});
}
// create pages at /user-guides, /user-guides/[app], and then one for every
// /user-guides/[app]/[guide]
createGuidePage(home, firstGuides[0]);
firstGuides.forEach(guide => {
createGuidePage(userGuidePath(guide.app), guide);
});
guides.forEach(guide => {
createGuidePage(userGuidePath(guide.app, guide.slug), guide);
});
};