-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (46 loc) · 1.46 KB
/
index.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
const debug = require('debug')('findWebmentionLinks')
const fetch = require('node-fetch')
const is = require('is_js')
const cheerio = require('cheerio')
const discoverWebmentionsToSend = async (sourceUrl, options = {}) => {
try {
// Given a sourceUrl
if (!sourceUrl) throw new Error(`Required parameter not supplied: sourceUrl`)
if (is.not.url(sourceUrl)) throw new Error(`Parameter must be a URL: sourceUrl`)
// get back HTML body
let res = await fetch(sourceUrl, {
'method': 'GET'
}).catch(error => {
throw error
})
if (!res.ok) throw new Error(`Received a bad sourceUrl: ${res.statusCode}`)
let sourceUrlContents = await res.text()
// Parse the HTML source
const root = cheerio.load(sourceUrlContents)
let selectors = [
// '.h-entry a',
'.h-entry a.u-in-reply-to',
'.h-entry a.u-like-of',
'.h-entry a.u-repost-of',
'.h-entry a.u-bookmark-of',
'.h-entry a.u-mention-of',
'.h-entry a.u-rsvp',
].join(', ')
// Select all links with a webmention
let webmentions = root(selectors)
// return array IF there are results
if (webmentions.length > 0) {
let temp = []
for (const item of webmentions) {
temp.push(item.attribs.href)
}
return temp
}
// OTHERWISE throw an error to reject the promise
throw false
} catch (error) {
debug(error)
throw error
}
}
module.exports = discoverWebmentionsToSend