-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
172 lines (156 loc) · 4.44 KB
/
gatsby-node.js
File metadata and controls
172 lines (156 loc) · 4.44 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const path = require('path');
const utils = require('./src/utils/node');
exports.createPages = async ({ graphql, actions, reporter }) => {
const { createPage } = actions;
const episodePage = path.resolve(`src/components/episode-page.js`);
const episodesGQL = await graphql(`
{
allFeedSafareigFm(sort: { isoDate: DESC }) {
nodes {
title
content
contentSnippet
isoDate(formatString: "YYYY / MM / DD")
itunes {
episode
}
enclosure {
url
}
}
totalCount
}
}
`);
if (episodesGQL.errors) {
reporter.panicOnBuild(`Error loading episodes:`, episodesGQL.errors);
return;
}
const episodes = episodesGQL.data.allFeedSafareigFm.nodes;
const episodesLength = parseInt(episodes.length);
const episodesCount = parseInt(episodesGQL.data.allFeedSafareigFm.totalCount);
// Create a page for each episode through path
if (episodesLength > 0 && episodesLength === episodesCount) {
episodes.forEach((e, i) => {
// Get episode information from XML
const {
title,
isoDate: date,
itunes: { episode: episodeNumber },
enclosure: { url: audioFile },
} = e;
// Generate show description and show notes
const { showDescription } = utils.trimDescriptions(
e.contentSnippet,
e.content,
);
const { showNotes } = utils.trimDescriptions(e.contentSnippet, e.content);
// Find previous and next episode
const previous = i === episodesLength - 1 ? null : episodes[i + 1];
const next = i === 0 ? null : episodes[i - 1];
// Generate related episodes
let relatedEpisodes = [];
// Use getRandom function to use with commonJS
// The ES6 version doesn't require the function
const relatedEpisodesNumbers = utils.getRelatedEpisodes(
2,
episodesCount,
episodeNumber,
utils.getRandom,
);
relatedEpisodesNumbers.forEach((relatedEpisodeNumber) => {
const relatedEpisode = episodes.find(
(e) => e.itunes.episode == relatedEpisodeNumber,
);
const relatedEpisodeTitle = relatedEpisode.title;
relatedEpisodes = [
...relatedEpisodes,
{ title: relatedEpisodeTitle, episodeNumber: relatedEpisodeNumber },
];
});
createPage({
component: episodePage,
context: {
audioFile,
showDescription,
showNotes,
date,
episodeNumber,
next,
previous,
relatedEpisodes,
title,
},
path: `${episodeNumber}/`,
});
});
}
// Fetch markdowns
const postPage = path.resolve(`src/components/post-page.js`);
// Fetch all markdown posts
const postsGQL = await graphql(`
{
allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/content/articles/" } }
sort: { frontmatter: { date: DESC } }
) {
edges {
node {
frontmatter {
author
date(formatString: "YYYY / MM / DD")
featured
meta
path
published
tags
title
}
html
id
timeToRead
wordCount {
words
}
}
}
}
}
`);
if (postsGQL.errors) {
reporter.panicOnBuild(`Error loading posts:`, postsGQL.errors);
return;
}
// posts -> [{ node }, { node }, ..., { node }]
const posts = postsGQL.data.allMarkdownRemark.edges;
// Create a page for each post through path
posts.forEach((post, index) => {
const { author, date, featured, meta, path, published, tags, title } =
post.node.frontmatter;
const { html, timeToRead } = post.node;
// Get the previous and next post
const prev =
index === posts.length - 1 ? posts[index].node : posts[index + 1].node;
const next = index === 0 ? posts[index].node : posts[index - 1].node;
// Get a random post for the last post since there's no "next" for it
const rdm = posts[utils.getRandom(0, posts.length - 1)].node;
createPage({
component: postPage,
context: {
author,
date,
featured,
html,
meta,
next,
prev,
published,
rdm,
tags,
timeToRead,
title,
},
path: path,
});
});
};