-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
144 lines (132 loc) · 3.45 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const path = require("path")
const getNavLinks = require("./src/utils/get-nav-links")
const slugifyDashes = require("./src/utils/slugify-dashes")
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const result = await graphql(`
{
navigationQuery: prismicNavigation {
data {
nav {
... on PrismicNavigationNavNavItem {
primary {
primary_link_label
}
items {
sub_link_label {
document {
... on PrismicTag {
tagData: data {
content_type
content_type_label
}
}
}
}
}
}
}
}
}
tagsQuery: allPrismicTag {
nodes {
data {
content_type
content_type_label
}
prismicId
}
}
}
`)
const {
navigationQuery: {
data: { nav: navigationItems },
},
tagsQuery: { nodes: tags },
} = result.data
const navLinks = getNavLinks(navigationItems)
createContentTypePages(navLinks, createPage)
createPodcastPages(tags, createPage)
}
/**
* Create a page for each podcast type (e.g. سجلات, قصيد الفهد). Podcast types are all tags coming from the CMS.
* @param {Array} tags Tags coming from the CMS
* @param {Function} createPage Function coming from Gatsby
*/
function createPodcastPages(tags, createPage) {
const podcastTemplate = path.resolve("./src/templates/podcast.js")
/**
* Organize tags in this way:
[ { name: main_tag_name, subTags: [ { id, name }, ... ] }, ... ]
*/
let allTags = []
tags.forEach(tag => {
const {
prismicId,
data: { content_type, content_type_label },
} = tag
const exists = allTags.find(
existingTag => existingTag.name === content_type
)
if (exists) {
exists.subTags.push({
id: prismicId,
name: content_type_label,
})
} else {
allTags.push({
name: content_type,
subTags: [
{
id: prismicId,
name: content_type_label,
},
],
})
}
})
/**
* Pages creation phase
*/
allTags.forEach(tag => {
const { name, subTags } = tag
const path = `/podcast/${slugifyDashes(name)}`
const subTagIDs = subTags.map(subTag => subTag.id)
const subTagNames = subTags
.map(subTag => subTag.name)
.filter(subTagName => subTagName !== null)
createPage({
path,
component: podcastTemplate,
context: {
podcastSubTags: subTagNames,
subTagIDs,
meta: {
title: name,
},
},
})
})
}
/**
* Create a page for each content type (e.g. إذاعة البترول، ديوان الفهد)
* @param {Array} navLinks Navigation links as modified by the getNavLinks utility function
* @param {Function} createPage createPage function coming from Gatsby
*/
function createContentTypePages(navLinks, createPage) {
const contentTypeTemplate = path.resolve("./src/templates/content-type.js")
navLinks.forEach(navLink => {
const path = `/${navLink.dest}`
createPage({
path,
component: contentTypeTemplate,
context: {
subLinks: navLink.subLinks,
meta: {
title: navLink.name,
},
},
})
})
}