Skip to content

Commit e8c8412

Browse files
committed
feat: add RSS feed generation and update layout for RSS link
1 parent 51cd837 commit e8c8412

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@
2020
"tailwindcss": "^3.4.3",
2121
"typescript": "^5.4.5"
2222
},
23-
"packageManager": "pnpm@9.12.2"
23+
"packageManager": "pnpm@9.12.2",
24+
"dependencies": {
25+
"@astrojs/rss": "^4.0.12"
26+
}
2427
}

pnpm-lock.yaml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/home/writings.astro

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import Button from "../button.astro";
33
import PostsLoop from "../posts-loop.astro";
44
5-
const feed = "https://feed.miantiao.me/";
5+
// Point to generated RSS feed (see /src/pages/feed.xml.js)
6+
const feed = (Astro.site ? new URL('/feed.xml', Astro.site).href : '/feed.xml');
67
---
78

89
<section class="max-w-4xl mx-auto px-7 lg:px-0">
@@ -52,6 +53,17 @@ const feed = "https://feed.miantiao.me/";
5253
>
5354
Subscribe my blog
5455
</h2>
56+
<a href={feed} aria-label="RSS feed" class="text-neutral-900 hover:text-neutral-600 dark:text-neutral-100 dark:hover:text-neutral-300 transition-colors">
57+
<svg
58+
class="w-5 h-5"
59+
viewBox="0 0 24 24"
60+
fill="currentColor"
61+
xmlns="http://www.w3.org/2000/svg"
62+
aria-hidden="true"
63+
>
64+
<path d="M6.18 17.82a1.82 1.82 0 1 1 0-3.64 1.82 1.82 0 0 1 0 3.64Zm4.55.55a7.27 7.27 0 0 0-7.27-7.27v-2.73a10 10 0 0 1 10 10h-2.73Zm5.45.09c0-7.34-5.96-13.27-13.27-13.27V2c8.84 0 16 7.16 16 16h-2.73Z" />
65+
</svg>
66+
</a>
5567
</div>
5668
<p class="mt-2 text-sm text-neutral-600 dark:text-neutral-400">
5769
Get my blog updates via <a

src/layouts/main.astro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const { title } = Astro.props;
3030
}
3131
</style>
3232
<link rel="icon" type="image/x-icon" href="../assets/images/favicon.png" />
33+
<link rel="alternate" type="application/rss+xml" title="Gautam's Blog RSS" href="/feed.xml" />
3334
<script src="../assets/css/main.css"></script>
3435
<Fragment set:html={import.meta.env.HEADER_INJECT} />
3536
</head>

src/pages/feed.xml.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import rss from '@astrojs/rss';
2+
import { getCollection } from 'astro:content';
3+
4+
export async function GET(context) {
5+
const posts = await getCollection('post');
6+
7+
// Sort newest first
8+
posts.sort((a, b) => b.data.date - a.data.date);
9+
10+
return rss({
11+
title: "Gautam's Blog",
12+
description: 'Personal blog of Gautam Mehta',
13+
site: context.site, // defined in astro.config.mjs
14+
items: posts.map((post) => ({
15+
title: post.data.title,
16+
description: post.data.description,
17+
pubDate: post.data.date,
18+
link: `/post/${post.slug}/`,
19+
})),
20+
customData: `<language>en-us</language>`
21+
});
22+
}

0 commit comments

Comments
 (0)