Первый блог + простой генератор RSS-фида (работает в Thunderbird)
This commit is contained in:
parent
6a60ce55e5
commit
8e09b8f0d9
4 changed files with 72 additions and 0 deletions
10
src/blogs/hello_world.md
Normal file
10
src/blogs/hello_world.md
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
title: 'Наш первый блог'
|
||||
thumbnail: 'wasd_perelesoq_game_jam_2025.png'
|
||||
date: '2025-09-19'
|
||||
description: 'Немного о самом сайте'
|
||||
---
|
||||
|
||||
# ПРИВЕТ !
|
||||
|
||||
Добро пожаловать на наш первый блог-пост!
|
||||
30
src/lib/util/Blogs.ts
Normal file
30
src/lib/util/Blogs.ts
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import path from 'path';
|
||||
|
||||
export async function fetchPostsSorted() {
|
||||
const allPosts = await fetchPosts();
|
||||
|
||||
const sortedPosts = allPosts.sort((a, b) => {
|
||||
return new Date(b.date).valueOf() - new Date(a.date).valueOf();
|
||||
});
|
||||
|
||||
return sortedPosts;
|
||||
};
|
||||
|
||||
export async function fetchPosts() {
|
||||
const allPostFiles = import.meta.glob('/src/blogs/*.md');
|
||||
const iterablePostFiles = Object.entries(allPostFiles);
|
||||
|
||||
const allPosts: App.BlogPost[] = await Promise.all(
|
||||
iterablePostFiles.map(async ([filePath, resolver]) => {
|
||||
const { metadata }: any = await resolver();
|
||||
const { name } = path.parse(filePath);
|
||||
|
||||
return {
|
||||
slug: name,
|
||||
...metadata
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return allPosts;
|
||||
};
|
||||
32
src/routes/blog/rss.xml/+server.ts
Normal file
32
src/routes/blog/rss.xml/+server.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import { fetchPostsSorted } from "$src/lib/util/Blogs";
|
||||
|
||||
function escapeXml(unsafe: string): string {
|
||||
return unsafe
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
|
||||
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 xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:atom="https://www.w3.org/2005/Atom" version="2.0">
|
||||
<channel>
|
||||
<title>Блог Tea Sanctuary</title>
|
||||
<link>https://teasanctuary.ru/blog</link>
|
||||
<ttl>1800</ttl>
|
||||
${posts.map((post) => `<item>
|
||||
<title>${escapeXml(post.title)}</title>
|
||||
<description>${escapeXml(post.description)}</description>
|
||||
<link>https://teasanctuary.ru/blog/${post.slug}</link>
|
||||
<pubDate>${(new Date(post.date)).toUTCString()}</pubDate>
|
||||
</item>`).join("\n")}
|
||||
</channel>
|
||||
</rss>`))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue