-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
88 lines (85 loc) · 2.07 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
const rssParser = require('rss-parser')
function getJsonFeed(userName) {
const parser = new rssParser({
customFields: {
item: [
['content:encoded', 'content']
],
},
})
return parser.parseURL(`https://medium.com/feed/${userName}`).then(feed => {
const parsedFeeds = feed.items.map(item => {
const thumbnail = item.content.match(
/(?<=(<img[^>]+src="))([^"\s]+)(?!"[^>]*\/z)/g
)[0]
const {
title,
isoDate: date,
creator: author,
link,
content
} = item
const slug = item.title
.replace(/[^a-zA-Z0-9\s]+/g, '')
.toLowerCase()
.split(' ')
.join('-')
return {
title,
date,
author,
link,
content,
thumbnail,
slug
}
})
return parsedFeeds
})
.catch(error => {
return [{
title: 'No feed found',
date: new Date(),
author: 'No author',
link: '',
content: '',
thumbnail: 'https://loremflickr.com/640/360',
slug: ''
}]
})
}
function sourceNodes({
actions,
createNodeId,
createContentDigest,
reporter
}, {
userName,
name
}) {
const {
createNode
} = actions
if (!userName || !name) {
const missingOption = !userName && !name ? 'userName and name' : !userName ? 'userName' : 'name'
reporter.panic(`${missingOption} has to defined in plugin configuration.`)
}
return getJsonFeed(userName).then(feed => {
feed.forEach(item => {
const id = createNodeId(item.link)
createNode({
...item,
id,
parent: null,
children: [],
internal: {
contentDigest: createContentDigest(feed),
type: name,
mediaType: 'application/json',
content: JSON.stringify(item),
}
})
})
})
}
exports.sourceNodes = sourceNodes