Add new RSS content data util

pull/9085/head
Martín González Gómez 3 years ago
parent e7865e27e8
commit 5c8d3e8ee5

@ -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);

@ -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;
}

@ -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;
@ -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">
@ -43,6 +45,7 @@ const get_rss = (posts) =>
<item>
<title>${escapeHTML(post.title)}</title>
<link>https://svelte.dev/blog/${post.slug}</link>
<author>${escapeHTML(post.author.name)}</author>
<description>${escapeHTML(post.content)}</description>
<pubDate>${formatPubdate(post.date)}</pubDate>
</item>
@ -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: {

Loading…
Cancel
Save