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:
2025-11-20 17:59:56 +08:00
parent dd3f553282
commit f994301fbb
8 changed files with 513 additions and 0 deletions

63
app/feed.xml/route.ts Normal file
View 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, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
}

View File

@@ -34,6 +34,11 @@ export const metadata: Metadata = {
},
icons: {
icon: '/favicon.png'
},
alternates: {
types: {
'application/rss+xml': `${siteConfig.url}/feed.xml`
}
}
};

14
app/robots.ts Normal file
View File

@@ -0,0 +1,14 @@
import { MetadataRoute } from 'next';
export default function robots(): MetadataRoute.Robots {
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000';
return {
rules: {
userAgent: '*',
allow: '/',
disallow: ['/api/', '/_next/', '/admin/'],
},
sitemap: `${siteUrl}/sitemap.xml`,
};
}

68
app/sitemap.ts Normal file
View File

@@ -0,0 +1,68 @@
import { MetadataRoute } from 'next';
import { allPosts, allPages } from 'contentlayer2/generated';
export default function sitemap(): MetadataRoute.Sitemap {
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000';
// Homepage
const homepage = {
url: siteUrl,
lastModified: new Date(),
changeFrequency: 'daily' as const,
priority: 1,
};
// Blog listing page
const blogPage = {
url: `${siteUrl}/blog`,
lastModified: new Date(),
changeFrequency: 'daily' as const,
priority: 0.9,
};
// Tags page
const tagsPage = {
url: `${siteUrl}/tags`,
lastModified: new Date(),
changeFrequency: 'weekly' as const,
priority: 0.7,
};
// All blog posts
const posts = allPosts
.filter((post) => post.status === 'published')
.map((post) => ({
url: `${siteUrl}${post.url}`,
lastModified: new Date(post.updated_at || post.published_at || post.created_at || Date.now()),
changeFrequency: 'weekly' as const,
priority: 0.8,
}));
// All pages
const pages = allPages
.filter((page) => page.status === 'published')
.map((page) => ({
url: `${siteUrl}${page.url}`,
lastModified: new Date(page.updated_at || page.published_at || page.created_at || Date.now()),
changeFrequency: 'monthly' as const,
priority: 0.6,
}));
// All unique tags
const allTags = Array.from(
new Set(
allPosts
.filter((post) => post.status === 'published' && post.tags)
.flatMap((post) => post.tags || [])
)
);
const tagPages = allTags.map((tag) => ({
url: `${siteUrl}/tags/${encodeURIComponent(tag.toLowerCase().replace(/\s+/g, '-'))}`,
lastModified: new Date(),
changeFrequency: 'weekly' as const,
priority: 0.5,
}));
return [homepage, blogPage, tagsPage, ...posts, ...pages, ...tagPages];
}