Files
markone/packages/server/src/bookmark/bookmark.ts

90 lines
2.3 KiB
TypeScript
Raw Normal View History

2025-05-07 15:47:08 +01:00
import type { User } from "@markone/core/user"
2025-05-13 11:55:50 +01:00
import { Readability } from "@mozilla/readability"
import { JSDOM } from "jsdom"
2025-05-07 15:47:08 +01:00
import { db } from "~/database.ts"
import { DEMO_BOOKMARKS } from "./demo-bookmarks.ts"
2025-05-13 11:55:50 +01:00
class LinkUnreachable {}
class UnsupportedLink {}
interface CachedPage {
title: string
readableHtml: string
}
2025-05-07 15:47:08 +01:00
function insertDemoBookmarks(user: User) {
const query = db.query(`
INSERT OR IGNORE INTO bookmarks (id, user_id, kind, title, url)
VALUES ($id, $userId, $kind, $title, $url)
`)
const insert = db.transaction((bookmarks) => {
for (const bookmark of bookmarks) {
query.run({
id: bookmark.id,
userId: user.id,
kind: bookmark.kind,
title: bookmark.title,
url: bookmark.url,
})
}
})
insert(DEMO_BOOKMARKS)
}
2025-05-08 15:51:17 +01:00
function findBookmarkHtml(id: string, user: User): string | null {
const query = db.query("SELECT content_html FROM bookmarks WHERE id = $id AND user_id = $userId")
const row = query.get({ id, userId: user.id })
if (!row) {
return null
}
const { content_html } = row as { content_html: string }
return content_html
2025-05-07 23:09:14 +01:00
}
2025-05-13 11:55:50 +01:00
async function cacheWebsite(url: string): Promise<CachedPage | null> {
const websiteText = await fetch(url)
.catch(() => {
throw new LinkUnreachable()
})
.then((res) => res.text())
.catch(() => {
throw new UnsupportedLink()
})
const dom = new JSDOM(websiteText, { url })
const reader = new Readability(dom.window.document)
const article = reader.parse()
if (!article) {
return null
}
if (article.content) {
const newDom = new JSDOM(article.content, { url })
const doc = newDom.window.document
const lightStyleLink = doc.createElement("link")
lightStyleLink.rel = "stylesheet"
lightStyleLink.href = "/reader-styles/sakura.css"
lightStyleLink.media = "screen"
const darkStyleLink = doc.createElement("link")
darkStyleLink.rel = "stylesheet"
darkStyleLink.href = "/reader-styles/sakura-dark.css"
darkStyleLink.media = "screen and (prefers-color-scheme: dark)"
doc.head.appendChild(lightStyleLink)
doc.head.appendChild(darkStyleLink)
article.content = newDom.serialize()
}
return {
title: article.title || "Untitled",
readableHtml: article.content || "",
}
}
export { insertDemoBookmarks, findBookmarkHtml, cacheWebsite }
export { LinkUnreachable, UnsupportedLink }