-
Notifications
You must be signed in to change notification settings - Fork 37
/
atom.ts
79 lines (73 loc) · 2.25 KB
/
atom.ts
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
import type { TextResponse } from '../download.ts'
import type { OriginPost } from '../post.ts'
import { createPostsList } from '../posts-list.ts'
import type { Loader } from './index.ts'
import {
findAnchorHrefs,
findImageByAttr,
findLinksByType,
isHTML,
toTime
} from './utils.ts'
function parsePosts(text: TextResponse): OriginPost[] {
let document = text.parseXml()
if (!document) return []
return [...document.querySelectorAll('entry')]
.filter(entry => entry.querySelector('id')?.textContent)
.map(entry => {
let content = entry.querySelector('content')
return {
full: content?.textContent ?? undefined,
intro: entry.querySelector('summary')?.textContent ?? undefined,
media: findImageByAttr('src', content?.querySelectorAll('img')),
originId: entry.querySelector('id')!.textContent!,
publishedAt: toTime(
entry.querySelector('published')?.textContent ??
entry.querySelector('updated')?.textContent
),
title: entry.querySelector('title')?.textContent ?? undefined,
url:
entry
.querySelector('link[rel=alternate], link:not([rel])')
?.getAttribute('href') ?? undefined
}
})
}
export const atom: Loader = {
getMineLinksFromText(text) {
if (!isHTML(text)) return []
let links = [
...findLinksByType(text, 'application/atom+xml'),
...findAnchorHrefs(text, /feeds\.|feed\.|\.atom|\/atom/i, /feed|atom/i)
]
if (links.length > 0) {
return links
} else {
return [...findAnchorHrefs(text, /\.xml/i)]
}
},
getPosts(task, url, text) {
if (text) {
return createPostsList(parsePosts(text), undefined)
} else {
return createPostsList(undefined, async () => {
return [parsePosts(await task.text(url)), undefined]
})
}
},
getSuggestedLinksFromText(text) {
let { origin } = new URL(text.url)
return [new URL('/feed', origin).href, new URL('/atom', origin).href]
},
isMineText(text) {
let document = text.parseXml()
if (document?.firstElementChild?.nodeName === 'feed') {
return document.querySelector(':root > title')?.textContent ?? ''
} else {
return false
}
},
isMineUrl() {
return undefined
}
}