feat: loose self rss check

This commit is contained in:
DIYgod
2023-12-23 02:57:56 +08:00
parent f94bdfcf55
commit 7cbcd059c6
4 changed files with 38 additions and 48 deletions
+15 -23
View File
@@ -1,7 +1,5 @@
import RSSParser from "rss-parser"
import type { RSSData } from "./types"
const rssParser = new RSSParser()
import { parseRSS } from "./utils"
export async function getPageRSS(data: {
html: string
@@ -54,27 +52,17 @@ export async function getPageRSS(data: {
}
if (html) {
let feed
try {
feed = await rssParser.parseString(html)
console.log("feed", feed)
} catch (error) {}
if (feed) {
const result = parseRSS(html)
if (result) {
pageRSS.push({
url: location.href,
title: feed.title,
title: result.title,
image
});
} else {
pageRSS.push({
url: location.href,
title: "",
image
});
}
// skip the following check if this page is an RSS feed
return pageRSS
// skip the following check if this page is an RSS feed
return pageRSS
}
}
// head links
@@ -165,10 +153,14 @@ export async function getPageRSS(data: {
const content = await (await fetch(feed.url, {
mode: "no-cors",
})).text()
const result = await rssParser.parseString(content)
feed.title = result.title;
pageRSS.push(feed);
unique.save(feed.url)
const result = parseRSS(content)
if (result) {
if (result.title) {
feed.title = result.title;
}
pageRSS.push(feed);
unique.save(feed.url)
}
resolve()
} catch (error) {
resolve()
+13 -1
View File
@@ -1,5 +1,6 @@
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
import he from "he"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
@@ -13,4 +14,15 @@ export function removeFunctionFields(obj) {
removeFunctionFields(obj[key]);
}
}
}
}
export function parseRSS(content: string) {
if (content.includes("<rss ")) {
const titleContent = content.match(/<title>(.*)<\/title>/)?.[1]
const title = titleContent ? he.decode(titleContent)?.replace(/<!\[CDATA\[(.*)]]>/, (match, p1) => p1)?.trim() : ""
return {
title,
}
} else {
return null
}
}