/
pocket.ts
33 lines (30 loc) · 1.31 KB
/
pocket.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
import { capitalize } from "$lib/utils"
export const parsePocketBookmarks = (doc: Document) => {
const dataElements = doc.querySelectorAll("body ul > li") as NodeListOf<
HTMLElement | HTMLDListElement
>
if (!dataElements) return []
return Array.from(dataElements)
.map((element) => {
// @ts-expect-error No DOM interface for DT, MDN says its a `HTMLElement`s
if (element.tagName === "LI" && element.firstElementChild.attributes.href) {
const title = element.textContent?.replaceAll("\n", "").trim().substring(0, 190)
// @ts-expect-error No DOM interface for DT, MDN says its a `HTMLElement`s
const url = element.firstElementChild?.attributes?.href?.value?.trim()
// @ts-expect-error No DOM interface for DT, MDN says its a `HTMLElement`s
const date = element.firstElementChild?.attributes?.time_added?.value?.trim()
// @ts-expect-error No DOM interface for DT, MDN says its a `HTMLElement`s
const tags = element.firstElementChild?.attributes?.tags?.value
.trim()
.split(",")
.map((tag: string) => capitalize(tag.trim()))
return {
title,
url,
createdAt: parseInt(date) ? new Date(parseInt(date) * 1000).toISOString() : 0,
tags,
}
}
})
.filter(Boolean)
}