forked from vojtaholik/gatsby-starter-simplecast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
89 lines (81 loc) · 1.98 KB
/
gatsby-node.js
File metadata and controls
89 lines (81 loc) · 1.98 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
require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`,
})
const axios = require("axios")
const crypto = require("crypto")
const path = require("path")
const slugify = require("@sindresorhus/slugify")
const data = require("./src/data/data.json")
axios.defaults.headers.common.Authorization = `Bearer ${process.env.SIMPLECAST_API_SECRET}`
axios.defaults.headers.common.Accept = "application/json"
exports.sourceNodes = async ({
actions: { createNode, createNodeField },
plugins,
}) => {
// Get data from egghead simplecast api
// const { data } = await axios(
// `https://api.simplecast.com/podcasts/${process.env.PODCAST_ID}/episodes?limit=20`
// )
const packagePodcast = p => {
const nodeContent = JSON.stringify(p)
const nodeContentDigest = crypto
.createHash("md5")
.update(nodeContent)
.digest("hex")
const node = {
...p,
content: nodeContent,
internal: {
type: "Episode",
contentDigest: nodeContentDigest,
},
}
createNode(node)
}
data.collection.map(packagePodcast)
}
exports.createPages = async ({ actions, graphql }) => {
const { data } = await graphql(`
query {
allEpisode {
edges {
node {
id
title
number
}
}
}
allMarkdownRemark {
edges {
node {
id
html
frontmatter {
title
}
}
}
}
}
`)
data.allEpisode.edges.forEach(({ node }) => {
actions.createPage({
path: `bolum/${node.number}/${slugify(node.title)}`,
component: path.resolve(`./src/templates/episode.js`),
context: {
slug: slugify(node.title),
id: node.id,
title: node.title,
},
})
})
}
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
createNodeField({
name: "slug",
node,
value: slugify(`${node.title}`),
})
}