mirror of
https://github.com/juherr/kill-the-news.git
synced 2026-06-21 06:13:48 +00:00
feat: parse <author> from From header in RSS items
Parse the From header into name + email parts so the feed library renders proper RFC 2822 format (email (Name)) in <author> elements. Also passes feedId to the generator so item links can point to the upcoming /entries/:feedId/:receivedAt route. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
+1
-1
@@ -62,7 +62,7 @@ export async function handle(c: Context): Promise<Response> {
|
|||||||
|
|
||||||
// Generate the RSS feed XML
|
// Generate the RSS feed XML
|
||||||
const baseUrl = `https://${env.DOMAIN}`;
|
const baseUrl = `https://${env.DOMAIN}`;
|
||||||
const rssXml = generateRssFeed(feedConfig, emailsData, baseUrl);
|
const rssXml = generateRssFeed(feedConfig, emailsData, baseUrl, feedId);
|
||||||
|
|
||||||
// Return the RSS feed with appropriate content type
|
// Return the RSS feed with appropriate content type
|
||||||
return new Response(rssXml, {
|
return new Response(rssXml, {
|
||||||
|
|||||||
@@ -1,6 +1,18 @@
|
|||||||
import { Feed } from "feed";
|
import { Feed } from "feed";
|
||||||
import { FeedConfig, EmailData } from "../types";
|
import { FeedConfig, EmailData } from "../types";
|
||||||
|
|
||||||
|
function parseFromAddress(from: string): { name: string; email?: string } {
|
||||||
|
const match = from.match(/^(.*?)\s*<([^>]+)>\s*$/);
|
||||||
|
if (match) {
|
||||||
|
return { name: match[1].trim() || match[2], email: match[2].trim() };
|
||||||
|
}
|
||||||
|
const emailOnly = from.match(/^[^\s@]+@[^\s@]+\.[^\s@]+$/);
|
||||||
|
if (emailOnly) {
|
||||||
|
return { email: from.trim(), name: from.trim() };
|
||||||
|
}
|
||||||
|
return { name: from.trim() };
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate an RSS feed from a list of emails
|
* Generate an RSS feed from a list of emails
|
||||||
*/
|
*/
|
||||||
@@ -8,6 +20,7 @@ export function generateRssFeed(
|
|||||||
feedConfig: FeedConfig,
|
feedConfig: FeedConfig,
|
||||||
emails: EmailData[],
|
emails: EmailData[],
|
||||||
baseUrl: string,
|
baseUrl: string,
|
||||||
|
feedId?: string,
|
||||||
): string {
|
): string {
|
||||||
// Create a new feed
|
// Create a new feed
|
||||||
const feed = new Feed({
|
const feed = new Feed({
|
||||||
@@ -34,18 +47,17 @@ export function generateRssFeed(
|
|||||||
for (const email of emails) {
|
for (const email of emails) {
|
||||||
const date = new Date(email.receivedAt);
|
const date = new Date(email.receivedAt);
|
||||||
const uniqueId = `${email.receivedAt}-${Buffer.from(email.subject).toString("base64").substring(0, 10)}`;
|
const uniqueId = `${email.receivedAt}-${Buffer.from(email.subject).toString("base64").substring(0, 10)}`;
|
||||||
|
const entryLink = feedId
|
||||||
|
? `${baseUrl}/entries/${feedId}/${email.receivedAt}`
|
||||||
|
: `${baseUrl}/emails/${uniqueId}`;
|
||||||
|
|
||||||
feed.addItem({
|
feed.addItem({
|
||||||
title: email.subject,
|
title: email.subject,
|
||||||
id: uniqueId,
|
id: uniqueId,
|
||||||
link: `${baseUrl}/emails/${uniqueId}`,
|
link: entryLink,
|
||||||
description: email.content,
|
description: email.content,
|
||||||
content: email.content,
|
content: email.content,
|
||||||
author: [
|
author: [parseFromAddress(email.from)],
|
||||||
{
|
|
||||||
name: email.from,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
date: date,
|
date: date,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user