implement bookmark tagging

This commit is contained in:
2025-05-21 13:18:16 +01:00
parent f048dee6e2
commit b0d458e5ca
20 changed files with 826 additions and 362 deletions

View File

@@ -1,107 +1,171 @@
import type { Bookmark, BookmarkTag } from "@markone/core/bookmark"
import type { User } from "@markone/core/user"
import { Readability } from "@mozilla/readability"
import { JSDOM } from "jsdom"
import { db } from "~/database.ts"
import { DEMO_BOOKMARKS } from "./demo-bookmarks.ts"
import type { Bookmark, BookmarkTag } from "@markone/core/bookmark"
class LinkUnreachable {}
class UnsupportedLink {}
interface CachedPage {
interface CachedContent {
title: string
readableHtml: string
mimeType: string
data: Buffer | null
}
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)
INSERT OR IGNORE INTO bookmarks (id, user_id, title, url, mime_type, content)
VALUES ${Array(DEMO_BOOKMARKS.length).fill("(?,?,?,?,NULL,NULL)").join(",")}
`)
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)
const args: Parameters<typeof query.run> = []
for (const bookmark of DEMO_BOOKMARKS) {
args.push(bookmark.id, user.id, bookmark.title, bookmark.url)
}
query.run(...args)
}
function findBookmarkHtml(id: string, user: User): string | null {
const query = db.query("SELECT content_html FROM bookmarks WHERE id = $id AND user_id = $userId")
function insertBookmark(bookmark: Bookmark, cachedContent: CachedContent | null, user: User) {
const query = db.query(`
INSERT INTO bookmarks (id, user_id, title, url, mime_type, content)
VALUES ($id, $userId, $title, $url, $mimeType, $content)
`)
query.run({
id: bookmark.id,
userId: user.id,
title: bookmark.title,
url: bookmark.url,
mimeType: cachedContent?.mimeType || null,
content: cachedContent?.data ?? null,
})
}
function findBookmarkCachedContent(id: string, user: User): Buffer | null {
const query = db.query<{ content: Buffer | null }, { id: string; userId: string }>(
"SELECT content 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
return row?.content ?? null
}
function findBookmark(id: string, user: User): Bookmark | null {
const bookmarkQuery = db.query<Bookmark, { id: string; userId: string }>(
"SELECT id, kind, title, url FROM bookmarks WHERE id = $id AND user_id = $userId",
"SELECT id, title, url FROM bookmarks WHERE id = $id AND user_id = $userId",
)
const bookmark = bookmarkQuery.get({ id, userId: user.id })
if (!bookmark) {
return null
}
const tagsQuery = db.query<BookmarkTag, { id: string }>("SELECT id, name FROM tags WHERE bookmark_id = $id")
const tags = tagsQuery.all({ id })
const tagsQuery = db.query<BookmarkTag, { bookmarkId: string }>(`
SELECT tags.id, tags.name FROM tags
INNER JOIN bookmark_tags
ON bookmark_tags.tag_id = tags.id AND bookmark_tags.bookmark_id = $bookmarkId
`)
const tags = tagsQuery.all({ bookmarkId: id })
bookmark.tags = tags
return bookmark
}
async function cacheWebsite(url: string): Promise<CachedPage | null> {
const websiteText = await fetch(url)
.catch(() => {
throw new LinkUnreachable()
})
.then((res) => res.text())
.catch(() => {
async function cacheContent(url: string): Promise<CachedContent | null> {
const res = await fetch(url).catch(() => {
throw new LinkUnreachable()
})
let contentType = res.headers.get("Content-Type")
if (!contentType) {
contentType = "application/octet-stream"
}
if (contentType.startsWith("text/html")) {
const matches = contentType.match(/charset=([^; ]+)/gi)
let charset: string
if (!matches || matches.length <= 2) {
charset = "utf-8"
} else {
charset = matches[1] || "utf-8"
}
const websiteText = await res.text().catch(() => {
throw new UnsupportedLink()
})
const dom = new JSDOM(websiteText, { url })
const reader = new Readability(dom.window.document)
const article = reader.parse()
const dom = new JSDOM(websiteText, { url })
const reader = new Readability(dom.window.document)
const article = reader.parse()
if (!article) {
return null
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",
mimeType: "text/html",
data: article.content ? Buffer.from(article.content, charset as BufferEncoding) : 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 || "",
}
throw new UnsupportedLink()
}
export { insertDemoBookmarks, findBookmark, findBookmarkHtml, cacheWebsite }
function assignTagsToBookmark(tags: BookmarkTag[], bookmark: Bookmark) {
const query = db.query(`
INSERT INTO bookmark_tags (tag_id, bookmark_id)
VALUES ${Array(tags.length).fill("(?,?)").join(",")}
`)
const args: Parameters<typeof query.run> = []
for (const tag of tags) {
args.push(tag.id)
args.push(bookmark.id)
}
query.run(...args)
}
function findBookmarkTags(bookmark: Bookmark): BookmarkTag[] {
const query = db.query<BookmarkTag, { bookmarkId: string }>(`
SELECT tags.name as name, tags.id as id FROM bookmark_tags
INNER JOIN tags
ON tags.id = bookmark_tags.tag_id
WHERE bookmark_tags.bookmark_id = $bookmarkId
`)
const tags = query.all({ bookmarkId: bookmark.id })
return tags
}
export {
insertDemoBookmarks,
insertBookmark,
findBookmark,
findBookmarkCachedContent,
cacheContent,
assignTagsToBookmark,
findBookmarkTags,
}
export { LinkUnreachable, UnsupportedLink }