Add RSS feed, sitemap, robots.txt, and code syntax highlighting
Implements essential blog features: 1. RSS Feed (/feed.xml) - Latest 20 posts with full content - Proper XML escaping and CDATA sections - Includes tags, authors, and descriptions - Auto-discovery link in HTML head 2. Sitemap (/sitemap.xml) - All posts, pages, and tag pages - Proper lastModified dates and priorities - Automatic generation via Next.js built-in support 3. Robots.txt (/robots.txt) - Allow all crawlers - Disallow API and admin routes - Links to sitemap for better SEO 4. Code Syntax Highlighting - Using rehype-pretty-code + Shiki - GitHub Dark/Light themes based on user preference - Line numbers for all code blocks - Support for highlighted lines - Inline code styling - Code title support 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
63
app/feed.xml/route.ts
Normal file
63
app/feed.xml/route.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { allPosts } from 'contentlayer2/generated';
|
||||
import { siteConfig } from '@/lib/config';
|
||||
|
||||
export async function GET() {
|
||||
const sortedPosts = allPosts
|
||||
.filter((post) => post.status === 'published')
|
||||
.sort((a, b) => {
|
||||
const dateA = a.published_at ? new Date(a.published_at).getTime() : 0;
|
||||
const dateB = b.published_at ? new Date(b.published_at).getTime() : 0;
|
||||
return dateB - dateA;
|
||||
})
|
||||
.slice(0, 20); // Latest 20 posts
|
||||
|
||||
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000';
|
||||
|
||||
const rss = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
|
||||
<channel>
|
||||
<title>${escapeXml(siteConfig.name)}</title>
|
||||
<link>${siteUrl}</link>
|
||||
<description>${escapeXml(siteConfig.description)}</description>
|
||||
<language>${siteConfig.defaultLocale.replace('_', '-')}</language>
|
||||
<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
|
||||
<atom:link href="${siteUrl}/feed.xml" rel="self" type="application/rss+xml"/>
|
||||
${sortedPosts
|
||||
.map((post) => {
|
||||
const postUrl = `${siteUrl}${post.url}`;
|
||||
const pubDate = post.published_at
|
||||
? new Date(post.published_at).toUTCString()
|
||||
: new Date(post.created_at || Date.now()).toUTCString();
|
||||
|
||||
return `
|
||||
<item>
|
||||
<title>${escapeXml(post.title)}</title>
|
||||
<link>${postUrl}</link>
|
||||
<guid isPermaLink="true">${postUrl}</guid>
|
||||
<description>${escapeXml(post.description || post.custom_excerpt || post.title)}</description>
|
||||
${post.body?.html ? `<content:encoded><![CDATA[${post.body.html}]]></content:encoded>` : ''}
|
||||
<pubDate>${pubDate}</pubDate>
|
||||
${post.authors?.map((author) => `<author>${escapeXml(author)}</author>`).join('\n ') || ''}
|
||||
${post.tags?.map((tag) => `<category>${escapeXml(tag)}</category>`).join('\n ') || ''}
|
||||
</item>`;
|
||||
})
|
||||
.join('')}
|
||||
</channel>
|
||||
</rss>`;
|
||||
|
||||
return new Response(rss, {
|
||||
headers: {
|
||||
'Content-Type': 'application/xml; charset=utf-8',
|
||||
'Cache-Control': 'public, max-age=3600, s-maxage=3600',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function escapeXml(unsafe: string): string {
|
||||
return unsafe
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
Reference in New Issue
Block a user