feat: add RSS xml (#34)

* feat: add RSS xml

---------

Co-authored-by: saicaca <zephyird@gmail.com>
This commit is contained in:
CorrectRoad
2024-04-22 23:42:47 +08:00
committed by GitHub
parent 0676b01b6a
commit e80999669a
4 changed files with 138 additions and 7 deletions

View File

@@ -7,7 +7,7 @@ import ImageWrapper from "@components/misc/ImageWrapper.astro";
import {pathsEqual} from "@utils/url-utils";
import ConfigCarrier from "@components/ConfigCarrier.astro";
import {siteConfig} from "@/config";
import {profileConfig, siteConfig} from "@/config";
interface Props {
title: string;
@@ -84,6 +84,8 @@ if (title) {
<link rel="stylesheet" href="https://cdn.staticfile.org/KaTeX/0.16.9/katex.min.css" integrity="sha384-n8MVd4RsNIU0tAv4ct0nTaAbDJwPJzDEaqSD1odI+WdtXRGWt2kTvGFasHpSy3SV" crossorigin="anonymous">
<link rel="alternate" type="application/rss+xml" title={profileConfig.name} href={`${Astro.site}rss.xml`}/>
<style define:vars={{ configHue }}></style> <!-- defines global css variables. This will be applied to <html> <body> and some other elements idk why -->
</head>

25
src/pages/rss.xml.ts Normal file
View File

@@ -0,0 +1,25 @@
import rss from '@astrojs/rss';
import {siteConfig} from '@/config';
import { getCollection } from 'astro:content';
import sanitizeHtml from 'sanitize-html';
import MarkdownIt from 'markdown-it';
const parser = new MarkdownIt();
export async function GET(context: any) {
const blog = await getCollection('posts');
return rss({
title: siteConfig.title,
description: siteConfig.subtitle || 'No description',
site: context.site,
items: blog.map((post) => ({
title: post.data.title,
pubDate: post.data.published,
description: post.data.description,
link: `/posts/${post.slug}/`,
content: sanitizeHtml(parser.render(post.body), {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img'])
}),
})),
customData: `<language>${siteConfig.lang}</language>`,
});
}