Первый блог + простой генератор RSS-фида (работает в Thunderbird)

This commit is contained in:
Иван Кузьменко 2025-09-19 04:51:27 +03:00
parent 6a60ce55e5
commit 8e09b8f0d9
4 changed files with 72 additions and 0 deletions

View file

@ -0,0 +1,32 @@
import { fetchPostsSorted } from "$src/lib/util/Blogs";
function escapeXml(unsafe: string): string {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&apos;");
}
export async function GET({ setHeaders }) {
setHeaders({
'Cache-Control': 'max-age=0, s-maxage=3600',
'Content-Type': 'application/rss+xml',
});
const posts = await fetchPostsSorted();
return new Response(String(`<?xml version="1.0" encoding="UTF-8" ?>
<rss xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:atom="https://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Блог Tea Sanctuary</title>
<link>https://teasanctuary.ru/blog</link>
<ttl>1800</ttl>
${posts.map((post) => `<item>
<title>${escapeXml(post.title)}</title>
<description>${escapeXml(post.description)}</description>
<link>https://teasanctuary.ru/blog/${post.slug}</link>
<pubDate>${(new Date(post.date)).toUTCString()}</pubDate>
</item>`).join("\n")}
</channel>
</rss>`))
}