Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/
.next
.vercel
next-env.d.ts
tsconfig.tsbuildinfo
2 changes: 1 addition & 1 deletion src/app/api/merge/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe("GET /api/merge - Multiple feeds with one returning 403", () => {
// Verify response is valid RSS
expect(response.status).toBe(200);
expect(response.headers.get("content-type")).toContain(
"application/rss+xml"
"application/rss+xml",
);

// Verify the merged feed title is present
Expand Down
6 changes: 3 additions & 3 deletions src/app/api/merge/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export async function GET(request: NextRequest) {
"The payload you've pasted is all lowercase, which is a common issue with Safari copy/paste. Please try again with a different browser.",
payload: compressedFeeds,
},
{ status: 400 }
{ status: 400 },
);
}
return NextResponse.json(
{
error: `${GENERATOR} cannot parse that payload. Are you sure you copied/pasted it correctly?`,
payload: compressedFeeds,
},
{ status: 400 }
{ status: 400 },
);
}
} else {
Expand All @@ -60,7 +60,7 @@ export async function GET(request: NextRequest) {
if (!urls || urls.length === 0) {
return NextResponse.json(
{ error: "No RSS feed URLs provided" },
{ status: 400 }
{ status: 400 },
);
}

Expand Down
3 changes: 1 addition & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ export default function Home() {
);
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text, "text/xml");
console.log(xmlDoc);

const items = Array.from(xmlDoc.querySelectorAll("item"))
.slice(0, 25)
Expand All @@ -137,7 +136,7 @@ export default function Home() {
item.querySelector("source")?.textContent || undefined,
image:
parser
.parseFromString(getTextContent("encoded") || "", "text/html")
.parseFromString(getTextContent("content") || "", "text/html")
.querySelector("img")
?.getAttribute("src") || undefined,
};
Expand Down
2 changes: 1 addition & 1 deletion src/app/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default function sitemap(): MetadataRoute.Sitemap {
return [
{
url: "https://rssrssrssrss.com",
lastModified: new Date(),
lastModified: new Date("2024-01-01"),
changeFrequency: "weekly",
priority: 1,
},
Expand Down
2 changes: 1 addition & 1 deletion src/lib/faqs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const FAQS: FAQ[] = [
{
question: "Who built this?",
answer:
"Your friends at Buttondown (https://buttondown.com), and they even made it open source (https://github.com/buttondown/rssrssrssrss).",
"Your friends at Buttondown (https://buttondown.com?utm_source=rss4), and they even made it open source (https://github.com/buttondown/rssrssrssrss).",
answerJsx: (
<>
Your friends at{" "}
Expand Down
108 changes: 54 additions & 54 deletions src/lib/rss.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { encodeContent } from "@/lib/encoding";
import { CustomFeed, CustomItem, JSONFeed } from "@/lib/types";
import { Category, CustomFeed, CustomItem, JSONFeed } from "@/lib/types";
import Parser from "rss-parser";

const parser = new Parser({
Expand All @@ -9,13 +9,17 @@ const parser = new Parser({
["dc:creator", "creator"],
],
},
headers: {
// Not registered with Cloudflare yet, but hnrss.org returns a 429 if we don't provide any UA.
"User-Agent": "rssrssrssrss (https://rssrssrssrss.com)",
},
});

const GENERATOR = "rssrssrssrss";
const FEED_TITLE = "Merged Feed";

// Helper functions for JSON Feed detection and parsing
async function isJSONFeed(url: string): Promise<boolean> {
// Helper function to try parsing as JSON Feed, returns null if not a JSON Feed
async function tryParseAsJSONFeed(url: string): Promise<CustomFeed | null> {
try {
const response = await fetch(url, {
headers: { Accept: "application/json, application/feed+json, */*" },
Expand All @@ -27,45 +31,41 @@ async function isJSONFeed(url: string): Promise<boolean> {
contentType.includes("application/json")
) {
const text = await response.text();
const data = JSON.parse(text);
return data.version && data.version.includes("jsonfeed.org");
const jsonFeed: JSONFeed = JSON.parse(text);

if (!jsonFeed.version || !jsonFeed.version.includes("jsonfeed.org")) {
return null;
}

// Convert JSON Feed items to CustomItem format
const items: CustomItem[] = jsonFeed.items.map((item) => ({
title: item.title,
link: item.url || item.external_url,
pubDate: item.date_published,
content: item.content_html,
contentSnippet: item.content_text || item.summary,
creator: item.author?.name,
isoDate: item.date_published,
guid: item.id,
categories: item.tags,
sourceFeedTitle: jsonFeed.title,
sourceFeedUrl: url,
}));

return {
title: jsonFeed.title,
description: jsonFeed.description,
link: jsonFeed.home_page_url,
items,
};
}

return false;
return null;
} catch {
return false;
return null;
}
}

async function parseJSONFeed(url: string): Promise<CustomFeed> {
const response = await fetch(url, {
headers: { Accept: "application/json, application/feed+json, */*" },
});
const jsonFeed: JSONFeed = await response.json();

// Convert JSON Feed items to CustomItem format
const items: CustomItem[] = jsonFeed.items.map((item) => ({
title: item.title,
link: item.url || item.external_url,
pubDate: item.date_published,
content: item.content_html,
contentSnippet: item.content_text || item.summary,
creator: item.author?.name,
isoDate: item.date_published,
guid: item.id,
categories: item.tags,
sourceFeedTitle: jsonFeed.title,
sourceFeedUrl: url,
}));

return {
title: jsonFeed.title,
description: jsonFeed.description,
link: jsonFeed.home_page_url,
items,
};
}

/**
* Parse a feed from a URL (RSS or JSON Feed)
*/
Expand All @@ -74,9 +74,10 @@ export async function parseFeedFromUrl(url: string): Promise<{
error: string | null;
}> {
try {
// Check if it's a JSON Feed first
if (await isJSONFeed(url)) {
return { feed: await parseJSONFeed(url), error: null };
// Check if it's a JSON Feed first (single fetch)
const jsonFeed = await tryParseAsJSONFeed(url);
if (jsonFeed) {
return { feed: jsonFeed, error: null };
} else {
// Fall back to RSS parsing
const feed = await parser.parseURL(url);
Expand Down Expand Up @@ -106,7 +107,7 @@ export async function parseFeedFromUrl(url: string): Promise<{
*/
export async function parseFeedFromXml(
xml: string,
sourceUrl?: string
sourceUrl?: string,
): Promise<{
feed: CustomFeed | null;
error: string | null;
Expand Down Expand Up @@ -138,7 +139,7 @@ export async function parseFeedFromXml(
*/
export async function parseFeedFromJsonFeed(
jsonFeedString: string,
sourceUrl?: string
sourceUrl?: string,
): Promise<{
feed: CustomFeed | null;
error: string | null;
Expand Down Expand Up @@ -195,7 +196,7 @@ export function mergeFeeds(
error: string | null;
url?: string;
}>,
requestUrl?: string
requestUrl?: string,
): CustomFeed {
// Combine all items into a single array, and collect failed feeds
const allItems: CustomItem[] = [];
Expand Down Expand Up @@ -270,7 +271,7 @@ function wrapCDATA(content: string): string {
*/
export function generateRSS(
mergedFeed: CustomFeed,
requestUrl?: string
requestUrl?: string,
): string {
const items = mergedFeed.items
.map((item) => {
Expand All @@ -290,7 +291,7 @@ export function generateRSS(

// GUID
itemXml += ` <guid>${escapeXml(
item.guid || item.link || ""
item.guid || item.link || "",
)}</guid>\n`;

// Publication date
Expand All @@ -303,7 +304,7 @@ export function generateRSS(
// Creator (DC namespace)
if (item.creator) {
itemXml += ` <dc:creator>${wrapCDATA(
item.creator
item.creator,
)}</dc:creator>\n`;
}

Expand All @@ -312,31 +313,30 @@ export function generateRSS(
// Note that we don't need to encode this because we're wrapping it in CData.
// Per #11, encoding it just removes smart quotes and things of that nature unnecessarily.
itemXml += ` <content:encoded>${wrapCDATA(
item.content
item.content,
)}</content:encoded>\n`;
} else if (item.contentSnippet) {
itemXml += ` <description>${escapeXml(
encodeContent(item.contentSnippet)
encodeContent(item.contentSnippet),
)}</description>\n`;
}

// Categories
if (item.categories && item.categories.length > 0) {
item.categories.forEach((category) => {
item.categories.forEach((category: Category) => {
// Handle categories that may be objects with _ property (from rss-parser when they have attributes)
const categoryValue =
typeof category === "string"
? category
: // @ts-expect-error - category._ is not typed
category._ || String(category);
: category._ || String(category);
itemXml += ` <category>${escapeXml(categoryValue)}</category>\n`;
});
}

// Source information
if (item.sourceFeedTitle && item.sourceFeedUrl) {
itemXml += ` <source url="${escapeXml(
item.sourceFeedUrl
item.sourceFeedUrl,
)}">${escapeXml(item.sourceFeedTitle)}</source>\n`;
}

Expand All @@ -350,7 +350,7 @@ export function generateRSS(
<channel>
<title>${escapeXml(mergedFeed.title || FEED_TITLE)}</title>
<description>${escapeXml(
mergedFeed.description || "Combined feed from multiple sources"
mergedFeed.description || "Combined feed from multiple sources",
)}</description>
<link>${escapeXml(mergedFeed.link || requestUrl || "")}</link>
<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
Expand All @@ -364,7 +364,7 @@ ${items} </channel>
*/
export function generateJSONFeed(
mergedFeed: CustomFeed,
requestUrl?: string
requestUrl?: string,
): string {
const jsonFeed: JSONFeed = {
version: "https://jsonfeed.org/version/1.1",
Expand All @@ -382,7 +382,7 @@ export function generateJSONFeed(
author: item.creator ? { name: item.creator } : undefined,
tags: item.categories
? item.categories.map((cat: any) =>
typeof cat === "string" ? cat : cat._ || String(cat)
typeof cat === "string" ? cat : cat._ || String(cat),
)
: undefined,
})),
Expand Down
5 changes: 4 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Category can be a string or an object with _ property (when RSS category has attributes)
export type Category = string | { _: string; [key: string]: any };

// Types for RSS items
export type CustomItem = {
title?: string;
Expand All @@ -8,7 +11,7 @@ export type CustomItem = {
creator?: string;
isoDate?: string;
guid?: string;
categories?: string[];
categories?: Category[];
// Source tracking
sourceFeedTitle?: string;
sourceFeedUrl?: string;
Expand Down
1 change: 0 additions & 1 deletion tsconfig.tsbuildinfo

This file was deleted.