См. https://validator.w3.org/feed/check.cgi?url=https%3A%2F%2Fteasanctuary.ru%2Fblog%2Frss.xml
67 lines
No EOL
2.3 KiB
TypeScript
67 lines
No EOL
2.3 KiB
TypeScript
import { fetchPostsSorted, resolveBlogPath } from "$src/lib/util/Blogs";
|
||
|
||
export const prerender = true;
|
||
|
||
const feedUpdated = new Date();
|
||
|
||
function escapeXml(unsafe: string): string {
|
||
return unsafe
|
||
.replace(/&/g, "&")
|
||
.replace(/</g, "<")
|
||
.replace(/>/g, ">")
|
||
.replace(/"/g, """)
|
||
.replace(/'/g, "'");
|
||
}
|
||
|
||
function makeThumbnail(post: App.BlogPost): string {
|
||
if (!post.thumbnail) return '';
|
||
|
||
const alt = !!post.thumbnailAlt ? ` alt="${escapeXml(post.thumbnailAlt)}"` : '';
|
||
|
||
return `<br><br><img src="https://teasanctuary.ru${resolveBlogPath(post.slug, post.thumbnail)}"${alt}>`;
|
||
}
|
||
|
||
function makeAuthors(post: App.BlogPost): string {
|
||
const authors =
|
||
(post.authors == null
|
||
? []
|
||
: typeof post.authors === 'string'
|
||
? [post.authors]
|
||
: post.authors)
|
||
.map(a => escapeXml(a));
|
||
|
||
if (authors.length === 0) return '';
|
||
|
||
let authorsString = authors[0];
|
||
if (authors.length > 1) {
|
||
const lastAuthor = authors.pop();
|
||
authorsString = `${authors.join(', ')} и ${lastAuthor}`;
|
||
}
|
||
|
||
return `\n<author>${authorsString}</author>`;
|
||
}
|
||
|
||
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 version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">
|
||
<channel>
|
||
<title>Блог Tea Sanctuary</title>
|
||
<description>Лента с новостями, заметками и событиями Tea Sanctuary</description>
|
||
<link>https://teasanctuary.ru/blog</link>
|
||
<atom:link href="https://teasanctuary.ru/blog/rss.xml" rel="self" type="application/rss+xml" />
|
||
<ttl>1800</ttl>
|
||
${posts.map((post) => `<item>
|
||
<title>${escapeXml(post.title)}</title>
|
||
<description><![CDATA[${escapeXml(post.description)}${makeThumbnail(post)}]]></description>${makeAuthors(post)}
|
||
<guid isPermaLink="true">https://teasanctuary.ru/blog/${post.slug}</guid>
|
||
<link>https://teasanctuary.ru/blog/${post.slug}</link>
|
||
<pubDate>${(new Date(post.date!)).toUTCString()}</pubDate>
|
||
</item>`).join("\n")}
|
||
</channel>
|
||
</rss>`))
|
||
} |