Hides unpublished articles from the RSS
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
import rss from '@astrojs/rss';
|
||||
import { getCollection } from 'astro:content';
|
||||
import { SITE_TITLE, SITE_DESCRIPTION } from '../consts';
|
||||
|
||||
export async function GET(context) {
|
||||
const posts = await getCollection('blog');
|
||||
return rss({
|
||||
title: SITE_TITLE,
|
||||
description: SITE_DESCRIPTION,
|
||||
site: context.site,
|
||||
items: posts.map((post) => ({
|
||||
...post.data,
|
||||
link: `/blog/${post.id}/`,
|
||||
})),
|
||||
});
|
||||
}
|
||||
25
src/pages/rss.xml.ts
Normal file
25
src/pages/rss.xml.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import rss from "@astrojs/rss";
|
||||
import { SITE_TITLE, SITE_DESCRIPTION } from "../consts";
|
||||
import { getCollection, type CollectionEntry } from "astro:content";
|
||||
import type { AstroUserConfig } from "astro";
|
||||
|
||||
function publishedOnly(
|
||||
p: CollectionEntry<"blog">,
|
||||
): p is CollectionEntry<"blog"> & { data: { pubDate: Date } } {
|
||||
return p.data.pubDate !== undefined;
|
||||
}
|
||||
|
||||
export async function GET(context: AstroUserConfig) {
|
||||
const posts = (await getCollection("blog", publishedOnly)).sort(
|
||||
(a, b) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf(),
|
||||
);
|
||||
return rss({
|
||||
title: SITE_TITLE,
|
||||
description: SITE_DESCRIPTION,
|
||||
site: context.site as string,
|
||||
items: posts.map((post) => ({
|
||||
...post.data,
|
||||
link: `/blog/${post.id}/`,
|
||||
})),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user