mirror of
https://github.com/juherr/kill-the-news.git
synced 2026-06-20 22:03:48 +00:00
fix(feed): correct feed link, canonical id, and strip html wrapper from content
- link: computed as /admin/feeds/:id/emails instead of stale site_url from KV - id: computed dynamically from baseUrl instead of stale feed_url from KV - item description/content: strip <html><head><body> wrapper via extractBodyContent() so feed readers receive a body fragment, not a full HTML document Fixes RSS validator warnings: SelfDoesntMatchLocation (stale KV domain) and InvalidHTML (full HTML document inside <description>/<content:encoded>). Adds 8 tests covering extractBodyContent and the new feed/atom link assertions. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,13 @@ function parseFromAddress(from: string): { name: string; email?: string } {
|
||||
return { name: from.trim() };
|
||||
}
|
||||
|
||||
// Email content is stored as a full HTML document. Feed readers expect only
|
||||
// the body fragment in <description>/<content:encoded>, not a full document.
|
||||
export function extractBodyContent(html: string): string {
|
||||
const match = html.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
|
||||
return match ? match[1] : html;
|
||||
}
|
||||
|
||||
function buildFeed(
|
||||
feedConfig: FeedConfig,
|
||||
emails: EmailData[],
|
||||
@@ -22,11 +29,14 @@ function buildFeed(
|
||||
const feed = new Feed({
|
||||
title: feedConfig.title,
|
||||
description: feedConfig.description || "",
|
||||
id: feedConfig.feed_url,
|
||||
link: feedConfig.site_url,
|
||||
// Computed dynamically so the id is always canonical regardless of what
|
||||
// was stored in KV at feed-creation time (which may have used a stale domain).
|
||||
id: `${baseUrl}/rss/${feedId}`,
|
||||
// Link points to the admin emails page — the "website" this feed represents.
|
||||
link: `${baseUrl}/admin/feeds/${feedId}/emails`,
|
||||
language: feedConfig.language,
|
||||
updated: new Date(),
|
||||
generator: "Email-to-RSS",
|
||||
generator: "kill-the-news",
|
||||
copyright: `Copyright © ${new Date().getFullYear()} ${feedConfig.title}`,
|
||||
feedLinks: {
|
||||
rss: `${baseUrl}/rss/${feedId}`,
|
||||
@@ -35,7 +45,7 @@ function buildFeed(
|
||||
author: feedConfig.author
|
||||
? {
|
||||
name: feedConfig.author,
|
||||
email: `noreply@${new URL(feedConfig.site_url).hostname}`,
|
||||
email: `noreply@${new URL(baseUrl).hostname}`,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
@@ -43,12 +53,13 @@ function buildFeed(
|
||||
for (const email of emails) {
|
||||
const uniqueId = `${email.receivedAt}-${Buffer.from(email.subject).toString("base64").substring(0, 10)}`;
|
||||
const firstAttachment = email.attachments?.[0];
|
||||
const bodyContent = extractBodyContent(email.content);
|
||||
feed.addItem({
|
||||
title: email.subject,
|
||||
id: uniqueId,
|
||||
link: `${baseUrl}/entries/${feedId}/${email.receivedAt}`,
|
||||
description: email.content,
|
||||
content: email.content,
|
||||
description: bodyContent,
|
||||
content: bodyContent,
|
||||
author: [parseFromAddress(email.from)],
|
||||
date: new Date(email.receivedAt),
|
||||
enclosure: firstAttachment
|
||||
|
||||
Reference in New Issue
Block a user