-
Notifications
You must be signed in to change notification settings - Fork 34
/
atom.ts
63 lines (57 loc) · 1.89 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
import type { TextResponse } from '../download.js'
import type { OriginPost } from '../post.js'
import { createPostsPage } from '../posts-page.js'
import type { Loader } from './index.js'
import { findAnchorHrefs, findLinksByType, toTime } from './utils.js'
function parsePosts(text: TextResponse): OriginPost[] {
let document = text.parse()
return [...document.querySelectorAll('entry')]
.filter(entry => entry.querySelector('id')?.textContent)
.map(entry => ({
full: entry.querySelector('content')?.textContent ?? undefined,
intro: entry.querySelector('summary')?.textContent ?? undefined,
media: [],
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) {
return [
...findLinksByType(text, 'application/atom+xml'),
...findAnchorHrefs(text, /feed\.|\.atom|\/atom/i)
]
},
getPosts(task, url, text) {
if (text) {
return createPostsPage(parsePosts(text), undefined)
} else {
return createPostsPage(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.parse()
if (document.firstElementChild?.nodeName === 'feed') {
return document.querySelector(':root > title')?.textContent ?? ''
} else {
return false
}
},
isMineUrl() {
return undefined
}
}