From 36d3a2c4c6dbfcc74271d0956eff9b1e454fc138 Mon Sep 17 00:00:00 2001 From: Ariel Costas Guerrero Date: Sat, 7 Jun 2025 18:29:19 +0200 Subject: Fix RSS feed --- src/pages/blog.xml.ts | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'src/pages/blog.xml.ts') diff --git a/src/pages/blog.xml.ts b/src/pages/blog.xml.ts index 44233b3..2b111d0 100644 --- a/src/pages/blog.xml.ts +++ b/src/pages/blog.xml.ts @@ -1,18 +1,42 @@ import rss from "@astrojs/rss"; +import type { APIContext, APIRoute } from "astro"; import { getCollection } from "astro:content"; -export async function GET(context: any) { - const collection = await getCollection("blog"); +const spanishPubDate = (date: Date): string => { + const options: Intl.DateTimeFormatOptions = { + year: "numeric", + month: "long", + day: "numeric", + weekday: "long", + }; + + return date.toLocaleDateString("es-ES", options); +} + +export const GET: APIRoute = async (context) => { + const collection = (await getCollection("blog")) + .filter((post) => post.data.publishedAt && post.data.title && post.data.description) + .sort((a, b) => { + return new Date(b.data.publishedAt).getTime() - new Date(a.data.publishedAt).getTime(); + }) + .slice(0, 20); // Limit to the latest 20 posts return rss({ title: "Blog de Ariel Costas", description: "Artículos del blog de Ariel Costas", - site: context.site, - items: collection.map((post: any) => ({ + site: context.site!, + customData: ``, + xmlns: { + "cdrss": "urn:costas.dev#rss", + }, + items: collection.map((post) => ({ title: post.data.title, - link: `${context.site}blog/${post.slug}`, - description: post.data.metaDescription, + link: `${context.url.origin}/blog/${post.id}`, + description: post.data.description, pubDate: post.data.publishedAt, + categories: post.data.tags, + customData: `${spanishPubDate(post.data.publishedAt)}`, })), + stylesheet: "/rss/styles.xsl" }); } -- cgit v1.3