All checks were successful
Docker Deploy / build-and-push (push) Successful in 4m20s
51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
import rss from '@astrojs/rss';
|
|
import { getCollection } from 'astro:content';
|
|
|
|
function formatPubDate(date) {
|
|
const timezone = process.env.PUBLIC_RSS_TIMEZONE
|
|
? process.env.PUBLIC_RSS_TIMEZONE
|
|
: import.meta.env.PUBLIC_RSS_TIMEZONE;
|
|
|
|
if (!timezone) {
|
|
return date;
|
|
}
|
|
|
|
try {
|
|
const year = date.getUTCFullYear();
|
|
const month = String(date.getUTCMonth() + 1).padStart(2, '0');
|
|
const day = String(date.getUTCDate()).padStart(2, '0');
|
|
|
|
const formatter = new Intl.DateTimeFormat('en-US', {
|
|
timeZone: timezone,
|
|
timeZoneName: 'longOffset',
|
|
});
|
|
|
|
const parts = formatter.formatToParts(date);
|
|
const offsetPart = parts.find(p => p.type === 'timeZoneName');
|
|
const offset = offsetPart ? offsetPart.value.replace('GMT', '') : '+00:00';
|
|
|
|
const dateStr = `${year}-${month}-${day}T00:00:00${offset}`;
|
|
|
|
return new Date(dateStr);
|
|
} catch (e) {
|
|
console.warn(`Invalid timezone "${timezone}":`, e.message);
|
|
return date;
|
|
}
|
|
}
|
|
|
|
export async function GET(context) {
|
|
const posts = await getCollection('posts');
|
|
|
|
return rss({
|
|
title: 'Atridad Lahiji',
|
|
description: 'Recent posts from Atridad Lahiji',
|
|
site: context.site,
|
|
items: posts.map((post) => ({
|
|
title: post.data.title,
|
|
pubDate: formatPubDate(post.data.pubDate),
|
|
description: post.data.description || '',
|
|
link: `/post/${post.slug}/`,
|
|
})),
|
|
customData: `<language>en-us</language>`,
|
|
});
|
|
}
|