feat: rss page component
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import Parser from "rss-parser"
|
||||
|
||||
import { cacheGet } from "~/lib/redis.server"
|
||||
import { getQuery, NextServerResponse } from "~/lib/server-helper"
|
||||
|
||||
const parser = new Parser()
|
||||
|
||||
export async function GET(req: Request): Promise<Response> {
|
||||
let { url } = getQuery(req)
|
||||
const res = new NextServerResponse()
|
||||
|
||||
if (!url) {
|
||||
return res.status(400).json({ error: "Bad request." })
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = (await cacheGet({
|
||||
key: `rss-parser/${url}`,
|
||||
expireTime: 60 * 60 * 1,
|
||||
getValueFun: async () => {
|
||||
let feed: Parser.Output<{
|
||||
[key: string]: any
|
||||
}> | null = null
|
||||
try {
|
||||
feed = await parser.parseURL(url)
|
||||
} catch (error) {}
|
||||
return feed
|
||||
},
|
||||
})) as Parser.Output<{
|
||||
[key: string]: any
|
||||
}> | null
|
||||
return res.status(parsed ? 200 : 404).json({ data: parsed ?? null })
|
||||
} catch (error: any) {
|
||||
console.error(error)
|
||||
return res.status(400).json({ error: error?.message ?? "Bad request." })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import Parser from "rss-parser"
|
||||
|
||||
import { Image } from "~/components/ui/Image"
|
||||
import dayjs from "~/lib/dayjs"
|
||||
import { SITE_URL } from "~/lib/env"
|
||||
|
||||
function extractImgSrc(htmlString: string) {
|
||||
const regex = /<img[^>]+src="([^">]+)"/g
|
||||
const matches = htmlString.match(regex)
|
||||
if (matches?.[0]) {
|
||||
const src = matches[0].match(/src="([^">]+)"/)?.[1]
|
||||
return src
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export default async function RSS({
|
||||
src,
|
||||
limit,
|
||||
}: {
|
||||
src: string
|
||||
limit?: number
|
||||
}) {
|
||||
let feed: Parser.Output<{
|
||||
[key: string]: any
|
||||
}> | null = null
|
||||
|
||||
if (src) {
|
||||
try {
|
||||
feed = (
|
||||
await (await fetch(`${SITE_URL}/api/rss-parser?url=${src}`)).json()
|
||||
).data
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
if (limit && feed?.items) {
|
||||
feed = {
|
||||
...feed,
|
||||
items: feed.items.slice(0, +limit),
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="grid gap-3 grid-cols-1 sm:grid-cols-3 sm:gap-6">
|
||||
{feed?.items?.map((item) => {
|
||||
const img = extractImgSrc(item.content || item.description || "")
|
||||
console.log(img, img?.startsWith("https://"))
|
||||
|
||||
return (
|
||||
<a
|
||||
key={item.guid || item.link}
|
||||
href={item.link}
|
||||
className="rounded-2xl border aspect-square relative flex overflow-hidden group text-sm"
|
||||
>
|
||||
{img?.startsWith("https://") && (
|
||||
<Image
|
||||
src={img}
|
||||
alt={item.title || item.description || ""}
|
||||
width={300}
|
||||
height={300}
|
||||
className="rounded-2xl object-cover"
|
||||
/>
|
||||
)}
|
||||
<span className="text-white text-center absolute bottom-0 bg-gradient-to-b from-transparent to-black/70 w-full h-16 flex flex-col items-center justify-center group-hover:h-full transition-[height] hover:backdrop-blur">
|
||||
<span className="font-bold">{item.title}</span>
|
||||
<span className="hidden group-hover:block mt-4">
|
||||
{item.contentSnippet}
|
||||
</span>
|
||||
<span className="text-white">
|
||||
{dayjs
|
||||
.duration(
|
||||
dayjs(item.isoDate).diff(dayjs(), "minute"),
|
||||
"minute",
|
||||
)
|
||||
.humanize()}{" "}
|
||||
ago
|
||||
</span>
|
||||
</span>
|
||||
</a>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -60,6 +60,7 @@ const GithubRepo = dynamic(() => import("~/components/ui/GithubRepo"))
|
||||
const XLogPost = dynamic(() => import("~/components/ui/XLogPost"))
|
||||
const APlayer = dynamic(() => import("~/components/ui/APlayer"))
|
||||
const DPlayer = dynamic(() => import("~/components/ui/DPlayer"))
|
||||
const RSS = dynamic(() => import("~/components/ui/RSS"))
|
||||
|
||||
refractor.alias("html", ["svelte", "vue"])
|
||||
refractor.register(langTsx)
|
||||
@@ -169,6 +170,7 @@ export const renderPageContent = (content: string, strictMode?: boolean) => {
|
||||
"xlog-post": XLogPost,
|
||||
// @ts-expect-error
|
||||
style: Style,
|
||||
rss: RSS,
|
||||
},
|
||||
ignoreInvalidStyle: true,
|
||||
jsx,
|
||||
|
||||
@@ -14,6 +14,7 @@ const scheme = deepmerge(defaultSchema, {
|
||||
"source",
|
||||
"mermaid",
|
||||
"aside",
|
||||
"rss",
|
||||
...["svg", "path", "circle"],
|
||||
],
|
||||
attributes: {
|
||||
@@ -45,6 +46,7 @@ const scheme = deepmerge(defaultSchema, {
|
||||
],
|
||||
path: ["d", "fill"],
|
||||
circle: ["cx", "cy", "r", "fill"],
|
||||
rss: ["src", "limit"],
|
||||
},
|
||||
protocols: {
|
||||
href: ["magnet", "ed2k"],
|
||||
|
||||
Reference in New Issue
Block a user