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

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];
}