Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions sites/svelte.dev/src/lib/server/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ export function get_blog_list(blog_data) {
}));
}

/** @param {import('./types').BlogData} blog_data */
export async function get_rss_contents(blog_data) {
return await Promise.all(
blog_data.map(async ({ slug, title, content, file, author, date, draft }) => ({
slug,
title,
content: await render_content(file, content),
author,
date,
draft,
}))
)
}


/** @param {string} filename */
function get_date_and_slug(filename) {
const match = BLOG_NAME_REGEX.exec(filename);
Expand Down
7 changes: 5 additions & 2 deletions sites/svelte.dev/src/lib/server/blog/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ export interface BlogPost {

export type BlogData = BlogPost[];

export interface BlogPostSummary {
export interface BlogPostRSS {
slug: string;
title: string;
description: string;
content: string;
author: {
name: string;
};
date: string;
draft: boolean;
}
16 changes: 10 additions & 6 deletions sites/svelte.dev/src/routes/blog/rss.xml/+server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { get_blog_data, get_blog_list } from '$lib/server/blog/index.js';
import { get_blog_data, get_rss_contents } from '$lib/server/blog/index.js';

export const prerender = true;

Expand All @@ -18,11 +18,13 @@ function escapeHTML(html) {
'>': 'gt'
};

return html.replace(/["'&<>]/g, (c) => `&${chars[c]};`);
return html
.replace(/<a[^>]*class="permalink"[^>]*>.*?<\/a>/gi, '') // remove anchor permalinks
.replace(/["'&<>]/g, (c) => `&${chars[c]};`);
}

/** @param {import('$lib/server/blog/types').BlogPostSummary[]} posts */
const get_rss = (posts) =>
/** @param {import('$lib/server/blog/types').BlogPostRSS[]} posts */
const get_rss = (posts) =>
`
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
Expand All @@ -43,7 +45,8 @@ const get_rss = (posts) =>
<item>
<title>${escapeHTML(post.title)}</title>
<link>https://svelte.dev/blog/${post.slug}</link>
<description>${escapeHTML(post.description)}</description>
<author>${escapeHTML(post.author.name)}</author>
<description>${escapeHTML(post.content)}</description>
<pubDate>${formatPubdate(post.date)}</pubDate>
</item>
`
Expand All @@ -58,7 +61,8 @@ const get_rss = (posts) =>
.trim();

export async function GET() {
const posts = get_blog_list(await get_blog_data());
const blogData = await get_blog_data();
const posts = await get_rss_contents(blogData);

return new Response(get_rss(posts), {
headers: {
Expand Down