1
0

feat: add rss feed

This commit is contained in:
dusk 2024-09-19 15:46:31 +03:00
parent df3d0a95f7
commit cfb3baa79d
Signed by: dusk
SSH Key Fingerprint: SHA256:Abmvag+juovVufZTxyWY8KcVgrznxvBjQpJesv071Aw
5 changed files with 70 additions and 37 deletions

View File

@ -1 +1,36 @@
export const prerender = true;
import convertDate from "$lib/convertDate";
export const prerender = true;
export interface PostData {
path: string,
published: string,
metadata: Record<string, string>,
}
const allPostFiles: Record<string, any> = import.meta.glob('./*/+page.md', { eager: true });
const allPosts: PostData[] = Object.entries(allPostFiles).map(([path, post]) => {
const postPath = path.slice(2, -8);
return {
metadata: post.metadata,
path: postPath,
published: convertDate(post.metadata.date)
};
}).map((post) => {
if (!("excerpt" in post.metadata)) {
post.metadata.excerpt = ""
}
return post;
}).toSorted((post, opost) => {
const date = new Date(post.metadata.date);
const odate = new Date(opost.metadata.date);
return odate.getTime() - date.getTime()
});
export const _allPosts = allPosts;
export async function load({}) {
if (!allPosts.length) {
return { status: 404 };
}
return { posts: allPosts };
}

View File

@ -1,6 +1,6 @@
<script lang="ts">
import Window from '../../components/window.svelte';
import type { PostData } from './+page.ts';
import type { PostData } from './+layout';
export let data;

View File

@ -1,34 +0,0 @@
import convertDate from "$lib/convertDate";
export interface PostData {
path: string,
published: string,
metadata: Record<string, string>,
}
export async function load({ params }) {
const allPostFiles: Record<string, any> = import.meta.glob('./*/+page.md', {eager: true});
let allPosts: PostData[] = Object.entries(allPostFiles).map(([path, post]) => {
const postPath = path.slice(2, -8);
return {
metadata: post.metadata,
path: postPath,
published: convertDate(post.metadata.date)
};
});
allPosts = allPosts.map((post) => {
if (!("excerpt" in post.metadata)) {
post.metadata.excerpt = ""
}
return post;
});
allPosts = allPosts.toSorted((post, opost) => {
const date = new Date(post.metadata.date);
const odate = new Date(opost.metadata.date);
return odate.getTime() - date.getTime()
});
if (!allPosts.length) {
return { status: 404 };
}
return { posts: allPosts };
}

View File

@ -0,0 +1,31 @@
import { PUBLIC_BASE_URL } from '$env/static/public';
import { _allPosts, type PostData } from '../+layout.ts';
const entriesUrl = `${PUBLIC_BASE_URL}/entries`;
export const GET = async ({ }) => {
return new Response(render(_allPosts), {
headers: {
'content-type': 'application/xml',
'cache-control': 'no-cache',
}
})
};
const render = (posts: PostData[]) => `<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="${entriesUrl}/_rss" rel="self" type="application/rss+xml" />
<title>gaze.systems</title>
<link>${PUBLIC_BASE_URL}</link>
<description>dusk's personal website</description>
${posts.map((post) => `<item>
<guid>${entriesUrl}/${post.path}</guid>
<title>${post.metadata.title}</title>
<link>${entriesUrl}/${post.path}</link>
<description>${post.metadata.excerpt}</description>
<pubDate>${new Date(post.metadata.date).toUTCString()}</pubDate>
</item>`).join('')}
</channel>
</rss>
`;

View File

@ -9,7 +9,8 @@
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"moduleResolution": "bundler"
"moduleResolution": "bundler",
"allowImportingTsExtensions": true
}
// Path aliases are handled by https://kit.svelte.dev/docs/configuration#alias
// except $lib which is handled by https://kit.svelte.dev/docs/configuration#files