Skip to content

Commit

Permalink
feat: loose self rss check
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Dec 22, 2023
1 parent f94bdfc commit 7cbcd05
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"async-lock": "^1.4.0",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"he": "1.2.0",
"lodash": "^4.17.21",
"lucide-react": "^0.298.0",
"md5.js": "^1.3.5",
Expand All @@ -32,7 +33,6 @@
"react-hot-toast": "^2.4.1",
"react-router-dom": "^6.21.0",
"route-recognizer": "^0.3.4",
"rss-parser": "^3.13.0",
"tailwind-merge": "^2.1.0",
"tailwindcss-animate": "^1.0.7",
"usehooks-ts": "^2.9.1"
Expand Down
32 changes: 9 additions & 23 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 15 additions & 23 deletions src/lib/rss.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
pageRSS.push({
url: location.href,
title: feed.title,
image
});
} else {
const result = parseRSS(html)
if (result) {
pageRSS.push({
url: location.href,
title: "",
title: result.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
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 13 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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
}
}

0 comments on commit 7cbcd05

Please sign in to comment.