forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.ts
More file actions
190 lines (175 loc) · 5.13 KB
/
common.ts
File metadata and controls
190 lines (175 loc) · 5.13 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import type { FeedValue } from '../feed.ts'
import type { DownloadTask, TextResponse } from '../lib/download.ts'
import type { PostMedia } from '../post.ts'
import type { PostsList } from '../posts-list.ts'
export type Loader = {
/**
* Find feeds URLs from HTML or HTTP headers.
*/
getMineLinksFromText(response: TextResponse): string[]
/**
* Extracts the feed's posts.
*
* If the URL’s document was already downloaded during the feed’s search.
*
* Task is a way to combine multiple HTTP requests (for instance, during
* the feed search/preview) to cancel all of them fast.
*/
getPosts(task: DownloadTask, url: string, text?: TextResponse): PostsList
/**
* Get source data of post from loader's API for debug purposes.
*/
getPostSource(feed: FeedValue, originId: string): Promise<unknown>
/**
* Try to suggest feed for given URL/document.
*
* For instance, by parsing <meta> or guessing URLs like `/rss` for RSS.
*/
getSuggestedLinksFromText(response: TextResponse): string[]
/**
* Detects that document is a loader’s feed.
*
* Return feed’s title if true.
*/
isMineText(response: TextResponse): false | string
/**
* It detects that URL is 100% for this loader.
*
* For instance, YouTube loader will return true for youtube.com links.
*
* It is not used right now because there is no way to detect RSS/Atom link
* just by URL.
*/
isMineUrl(url: URL): false | string | undefined
}
export function isString(attr: null | string): attr is string {
return typeof attr === 'string' && attr.length > 0
}
/** Detects that the server responded with a HTML document */
export function isHTML(text: TextResponse): boolean {
return text.text.toLocaleLowerCase().includes('<html')
}
/**
* Returns full URL for link HTML element, which includes not only
* the explicitly provided base URL, but also the base URL specified
* in the document.
*/
export function buildFullURL(
link: HTMLAnchorElement | HTMLLinkElement,
baseUrl: string
): string {
let href = link.getAttribute('href')!
let urlSegments: string[] = [href]
let parent: Element | null = link.parentElement
while (parent) {
let path = parent.getAttribute('xml:base') || ''
urlSegments.push(path)
parent = parent.parentElement
if (path.startsWith('/') || path.startsWith('http')) {
break
}
}
return urlSegments.reduceRight(
(base, url) => new URL(url, base).href,
baseUrl
)
}
/**
* Returns full URLs found in the document’s `<link>` elements.
*/
export function findDocumentLinks(text: TextResponse, type: string): string[] {
let document = text.parseXml()
if (!document) return []
return [...document.querySelectorAll('link')]
.filter(
link =>
link.getAttribute('type') === type &&
isString(link.getAttribute('href'))
)
.map(link => buildFullURL(link, text.url))
}
/**
* Returns full URLs found in the document’s `<a>` elements.
*/
export function findAnchorHrefs(
text: TextResponse,
hrefPattern: RegExp,
textPattern?: RegExp
): string[] {
let document = text.parseXml()
if (!document) return []
return [...document.querySelectorAll('a')]
.filter(a => {
let href = a.getAttribute('href')
if (!href) return false
if (textPattern && a.textContent && textPattern.test(a.textContent)) {
return true
}
return hrefPattern.test(href)
})
.map(a => buildFullURL(a, text.url))
}
/**
* Returns an array of full URL found in the `Link` HTTP header like:
*
* ```
* <http://blog.com/?feed=rss2>; rel="alternate"; type="application/rss+xml"
* ```
*
* URLs in this header can also be multiple, comma-separated.
* And possibly relative (the method will convert them to absolute).
*/
export function findHeaderLinks(
response: TextResponse,
type: string
): string[] {
let linkHeader = response.headers.get('Link')
if (!linkHeader) {
return []
}
return linkHeader.split(/,\s?/).reduce<string[]>((urls, link) => {
let [, url] = link.match(/<(.*)>/) || []
let attributes = link.split(/;\s?/)
let matchesType = attributes.includes(`type="${type}"`)
let isAlternate = attributes.includes('rel="alternate"')
if (url && matchesType && isAlternate) {
let fullUrl = /^https?/.test(url) ? url : new URL(url, response.url).href
urls.push(fullUrl)
}
return urls
}, [])
}
/**
* Returns the UNIX timestamp of a date.
*/
export function toTime(date: null | string | undefined): number | undefined {
if (!date) return undefined
let time = new Date(date).getTime() / 1000
if (isNaN(time)) {
return undefined
} else {
return time
}
}
/**
* Find all images in HTML node.
*/
export function findMediaInText(
text: Element | null | string | undefined
): PostMedia[] {
if (!text) return []
let parsed: Document | Element
if (typeof text === 'string') {
parsed = new DOMParser().parseFromString(text, 'text/html')
} else {
parsed = text
}
let images = parsed.querySelectorAll('img[src]')
return [...images].map(img => {
return {
fromText: true,
type: 'image',
url: img.getAttribute('src')!
} satisfies PostMedia
})
}