inject sakura css into cached html
This commit is contained in:
@@ -5,9 +5,7 @@ import { ulid } from "ulid"
|
||||
import { db } from "~/database.ts"
|
||||
import { HttpError } from "~/error.ts"
|
||||
import type { User } from "~/user/user.ts"
|
||||
import { JSDOM } from "jsdom"
|
||||
import { Readability } from "@mozilla/readability"
|
||||
import { findBookmarkHtml } from "./bookmark.ts"
|
||||
import { LinkUnreachable, UnsupportedLink, findBookmarkHtml, cacheWebsite } from "./bookmark.ts"
|
||||
|
||||
const BOOKMARK_PAGINATION_LIMIT = 100
|
||||
|
||||
@@ -66,17 +64,19 @@ async function addBookmark(request: Bun.BunRequest<"/api/bookmarks">, user: User
|
||||
throw new HttpError(400)
|
||||
}
|
||||
|
||||
const websiteResponse = await fetch(body.url).catch(() => {
|
||||
if (body.force) {
|
||||
return null
|
||||
const cachedWebsite = await cacheWebsite(body.url).catch((error) => {
|
||||
if (error instanceof LinkUnreachable) {
|
||||
if (body.force) {
|
||||
return null
|
||||
}
|
||||
throw new HttpError(400, "LinkUnreachable")
|
||||
}
|
||||
throw new HttpError(400, "WebsiteUnreachable")
|
||||
if (error instanceof UnsupportedLink) {
|
||||
throw new HttpError(400, "UnsupportedLink")
|
||||
}
|
||||
console.error(error)
|
||||
throw new HttpError(500)
|
||||
})
|
||||
const websiteText = websiteResponse
|
||||
? await websiteResponse.text().catch(() => {
|
||||
throw new HttpError(400, "UnsupportedWebsite")
|
||||
})
|
||||
: null
|
||||
|
||||
const bookmark: LinkBookmark = {
|
||||
kind: "link",
|
||||
@@ -88,24 +88,8 @@ async function addBookmark(request: Bun.BunRequest<"/api/bookmarks">, user: User
|
||||
|
||||
if (body.title) {
|
||||
bookmark.title = body.title
|
||||
}
|
||||
|
||||
let contentHtml: string
|
||||
if (websiteText) {
|
||||
const dom = new JSDOM(websiteText, {
|
||||
url: body.url,
|
||||
})
|
||||
const reader = new Readability(dom.window.document)
|
||||
const article = reader.parse()
|
||||
if (!bookmark.title) {
|
||||
bookmark.title = article?.title || "Untitled"
|
||||
}
|
||||
contentHtml = article?.content || ""
|
||||
} else {
|
||||
contentHtml = ""
|
||||
if (!bookmark.title) {
|
||||
bookmark.title = "Untitled"
|
||||
}
|
||||
} else if (cachedWebsite?.title) {
|
||||
bookmark.title = cachedWebsite.title
|
||||
}
|
||||
|
||||
const query = db.query(`
|
||||
@@ -118,7 +102,7 @@ VALUES ($id, $userId, $kind, $title, $url, $html)
|
||||
kind: bookmark.kind,
|
||||
title: bookmark.title,
|
||||
url: bookmark.url,
|
||||
html: contentHtml,
|
||||
html: cachedWebsite?.readableHtml ?? "",
|
||||
})
|
||||
|
||||
const insertTagQuery = db.query(`
|
||||
|
Reference in New Issue
Block a user