-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (42 loc) · 1.15 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
import { parseHTML } from 'linkedom'
import got from 'got'
import { getPosts, getFeeds, getIcons } from './util/elements.js'
import { getPostContent, microformatsParser } from './util/parser.js'
let pageInfo = {}
function parseSiteContent(rawContent) {
return parseHTML(rawContent).document
}
function getSiteContent(document) {
return {
posts: getPosts(document, pageInfo),
feeds: getFeeds(document, pageInfo.url),
meta: {
title: document.title.trim(),
description: document.head
.querySelector('[name="description"]')
?.getAttribute('content'),
icons: getIcons(document, pageInfo.url),
},
}
}
function parsePosts(posts) {
const { useMicroformats } = pageInfo
return Promise.all(
[...posts].map(async (post) =>
useMicroformats
? microformatsParser(post)
: await getPostContent(post, pageInfo),
),
)
}
export async function getBlog(infos) {
pageInfo = infos
let data = await got.get(infos.url).text()
const document = parseSiteContent(data)
const { posts, meta, feeds } = getSiteContent(document)
return {
posts: await parsePosts(posts),
meta,
feeds,
}
}