Skip to content

Commit 140dbff

Browse files
authored
feat: Render full posts on RSS feed (#934)
* Render full posts on RSS feed * fix tab
1 parent 3ba6329 commit 140dbff

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

apps/svelte.dev/src/routes/blog/rss.xml/+server.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { index } from '$lib/server/content';
2+
import { render_content } from '$lib/server/renderer';
23

34
export const prerender = false; // TODO
45

@@ -25,8 +26,24 @@ function escapeHTML(html) {
2526
}
2627

2728
/** @param {import('@sveltejs/site-kit').Document[]} posts */
28-
const get_rss = (posts) =>
29+
const get_rss = async (posts) => {
30+
const renderedPosts = await Promise.all(
31+
posts
32+
.filter((post) => !post.metadata.draft)
33+
.map(
34+
async (post) => `
35+
<item>
36+
<title>${escapeHTML(post.metadata.title)}</title>
37+
<link>https://svelte.dev/${post.slug}</link>
38+
<author>${escapeHTML(post.metadata.author)}</author>
39+
<description>${escapeHTML(await render_content(post.file, post.body))}</description>
40+
<pubDate>${formatPubdate(/** @type {string} */ (post.file.split('/').pop()).slice(0, 10))}</pubDate>
41+
</item>
2942
`
43+
)
44+
);
45+
46+
return `
3047
<?xml version="1.0" encoding="UTF-8" ?>
3148
<rss version="2.0">
3249
@@ -39,29 +56,18 @@ const get_rss = (posts) =>
3956
<title>Svelte</title>
4057
<link>https://svelte.dev/blog</link>
4158
</image>
42-
${posts
43-
.filter((post) => !post.metadata.draft)
44-
.map(
45-
(post) => `
46-
<item>
47-
<title>${escapeHTML(post.metadata.title)}</title>
48-
<link>https://svelte.dev/${post.slug}</link>
49-
<description>${escapeHTML(post.metadata.description)}</description>
50-
<pubDate>${formatPubdate(/** @type {string} */ (post.file.split('/').pop()).slice(0, 10))}</pubDate>
51-
</item>
52-
`
53-
)
54-
.join('')}
59+
${renderedPosts.join('')}
5560
</channel>
5661
5762
</rss>
5863
`
5964
.replace(/>[^\S]+/gm, '>')
6065
.replace(/[^\S]+</gm, '<')
6166
.trim();
67+
};
6268

6369
export async function GET() {
64-
return new Response(get_rss(index.blog.children), {
70+
return new Response(await get_rss(index.blog.children), {
6571
headers: {
6672
'Cache-Control': `max-age=${30 * 60 * 1e3}`,
6773
'Content-Type': 'application/rss+xml'

0 commit comments

Comments
 (0)